mbuhot / eskotlin

Elasticsearch Query DSL for Kotlin
MIT License
136 stars 28 forks source link
dsl elasticsearch kotlin

Build Status

ES Kotlin

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.

Getting Started

Gradle

repositories {
    mavenCentral()
    ...
    maven {
        url  "http://dl.bintray.com/mbuhot/maven"
   }
}
dependencies {
    compile 'mbuhot:eskotlin:0.7.0'
    ...
}

See CHANGELOG for older versions supporting previous elasticsearch client versions.

Maven

Full details on bintray

<dependency>
  <groupId>mbuhot</groupId>
  <artifactId>eskotlin</artifactId>
  <version>0.7.0</version>
  <type>pom</type>
</dependency>

Examples

Term Query

JSON:

{
    "term" : { "user" : "Kimchy" }
}

Kotlin:

val query = term {
    "user" to "Kimchy"
}

Bool Query

JSON:

{
    "bool" : {
        "must" : {
            "term" : { "user" : "kimchy" }
        },
        "filter": {
            "term" : { "tag" : "tech" }
        },
        "must_not" : {
            "range" : {
                "age" : { "from" : 10, "to" : 20 }
            }
        },
        "should" : [
            {
                "term" : { "tag" : "wow" }
            },
            {
                "term" : { "tag" : "elasticsearch" }
            }
        ],
        "minimum_should_match" : 1,
        "boost" : 1.0
    }
}

Kotlin:

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
}

Function Score Query

JSON:

{
    "function_score": {
        "query": {
            "match_all": {}
        },
        "functions": [
            {
                "filter": {
                    "term": {
                        "foo": "bar"
                    }
                },
                "gauss": {
                    "baz": {
                        "scale": 1.0
                    }
                }
            },
            {
                "filter": {
                    "match_all": {}
                },
                "random_score": {
                    "seed": 234
                }
            },
            {
                "exp": {
                    "qux": {
                        "scale": 2.3
                    }
                }
            }
        ],
        "score_mode": "max",
        "boost_mode": "multiply",
        "max_boost": 5.0,
        "boost": 1.2,
        "min_score": 0.001
    }
}

Kotlin:

val query = function_score {
    query = match_all { }
    functions = listOf(
        term { "foo" to "bar" } to gaussDecayFunction("baz", 1.0),
        match_all { } to randomFunction(234L),
        null to exponentialDecayFunction("qux", 2.3))

    boost = 1.2f
    boost_mode = "multiply"
    score_mode = "max"
    max_boost = 5.0f
    min_score = 0.001f
}

See the src/test directory for more examples.

API Coverage

Full Text Queries - Done

Term Queries - Done

Compound Queries - Done

Joining Queries - Done

Geo Queries - Contributions welcome!

Specialized Queries - Contributions welcome!

Span Queries - Contributions welcome!

Aggregations - Contributions welcome!

License

MIT - See LICENSE file for full text.