Skip to content

goatbytes/ElasticKtDSL

Repository files navigation

ElasticKtDSL

Elastic Search Kotlin Domain System Language (DSL)

Style Guide-Kotlin Gradle Dependency Maven Central JVM

Getting Started

Gradle

Add the following to your build.gradle.kts in your project:

dependencies {
  implementation("io.goatbytes:ElasticKtDSL:1.0.0-alpha+2024050723.c7c422e")
}

Examples and Documentation

Term Query

Kotlin:

val query = term {
  "user" to "goatbytes"
}
Click to expand the generated JSON
{
  "term": {
    "user": "goatbytes"
  }
}

Bool Query

Kotlin:

val query = bool {
  must {
    term { "user" to "goatbytes" }
  }
  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
}
Click to expand the generated JSON
{
  "bool": {
    "must": {
      "term": {
        "user": "goatbytes"
      }
    },
    "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
  }
}

Function Score Query

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
}
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
  }
}

See the src/test directory for more examples.

Contributing

This project is a community-driven fork of the original ES Kotlin library by Michael Buhot, which provides a Kotlin-flavored DSL for constructing Elasticsearch queries. The original library aimed to minimize the gap between the Elasticsearch JSON query DSL and the API used when writing Kotlin applications, integrating seamlessly with the existing Java API to provide a more Kotlin-idiomatic syntax.

The original repository for ES Kotlin is available at: mbuhot/eskotlin. It was previously hosted on Bintray, which has since been discontinued. This fork aims to continue the development and support of ES Kotlin as an open-source project under the same MIT license.

Contributions are welcome! Please read our contributing guide and submit pull requests to our repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

ℹ️ About GoatBytes.IO

GoatBytesLogo

GitHub Twitter LinkedIn Instagram

At GoatBytes.IO, our mission is to develop secure software solutions that empower businesses to transform the world. With a focus on innovation and excellence, we strive to deliver cutting-edge products that meet the evolving needs of businesses across various industries.

Releases

No releases published

Packages

No packages published

Languages