ES Kotlin - Elasticsearch Query DSL

Elasticsearch Query DSL for Kotlin.

This library aims to minimize the gap between the Elasticsearch JSON query DSL, and the API used when writing kotlin applications. This integrates with the existing java API, only providing a nicer syntax to build the queries.

Github
Bintray

Example:

val query = bool {
    must {
        term { "user" to "kimchy" }
    }
    filter {
        term { "tag" to "tech" }
    }
    must_not {
        range {
            "age" {
                from = 10
                to = 20
            }
        }
    }
    should = listOf(
        term { "tag" to "wow" },
        term { "tag" to "elasticsearch" })
    minimum_should_match = 1
    boost = 1.0f
}

The library is currently getting occasional updates to track the latest Kotlin and Elasticsearch versions.
Contributions are always welcome!

3 Likes

nice… But it look synchronous…

Yes ESKotlin just adds convenience builder functions to the standard Elasticsearch Java API. It’s one of my favourite uses for kotlin and extension functions - giving a library the API you wish it had without completely wrapping or reimplementing it :slight_smile:

Edit: actually looks like the Elasticsearch 6.3 java api is asynchronous:

All operations are completely asynchronous in nature (either accepts a listener, or returns a future).

ok you just build the query =) then its not a problem :+1:

How do you sort using DSL?

I tried

sort {
  "@timestamp" to "asc"
}

But it looks like this has no effect. Changing “asc” to “desc” returns the same result.

I think you’ll need to specify the sorting in the SearchRequestBuilder rather than in the query itself, using

https://javadoc.io/doc/org.elasticsearch/elasticsearch/latest/org/elasticsearch/action/search/SearchRequestBuilder.html#addSort(java.lang.String,org.elasticsearch.search.sort.SortOrder)

Just a side note… maps could be better in Kotlin

val query = bool {
  must {
    term { "user" to "kimchy" }
  }
}
  1. no quotes, 2) no to.
val query = bool {
  must {
    term { user: "kimchy" }
  }
}

that but with quotes is what the Collections Literal Proposal is

2 Likes