kt-search

Multi platform kotlin client for Elasticsearch & Opensearch with easily extendable Kotlin DSLs for queries, mappings, bulk, and more.


Project maintained by jillesvangurp Hosted on GitHub Pages — Theme by mattgraham

Term Level Queries

KT Search Manual Previous: Text Queries Next: Compound Queries
Github © Jilles van Gurp  

The most basic queries in Elasticsearch are queries on individual terms.

Term query

client.search(indexName) {
  query = term(TestDoc::tags, "fruit")
}.pretty("Term Query.").let { println(it) }

This prints:

Term Query. Found 2 results:
- 0.4700036 1 Apple
- 0.4700036 2 Banana

You can also do terms queries using numbers or booleans.

client.search(indexName) {
  query = term(TestDoc::price, 0.80)
}.pretty("Term Query.").let { println(it) }

By default term queries are case sensitive. But you can turn that off.

client.search(indexName) {
  query = term(TestDoc::tags, "fRuIt") {
    caseInsensitive = true
  }
}.pretty("Term Query.").let { println(it) }

This prints:

Term Query. Found 2 results:
- 1.0 1 Apple
- 1.0 2 Banana

Terms query

client.search(indexName) {
  query = terms(TestDoc::tags, "fruit", "legumes")
}.pretty("Terms Query.").let { println(it) }

This prints:

Terms Query. Found 3 results:
- 1.0 1 Apple
- 1.0 2 Banana
- 1.0 3 Green Beans

Fuzzy query

client.search(indexName) {
  query = fuzzy(TestDoc::tags, "friut") {
    fuzziness = "auto"
  }
}.pretty("Fuzzy Query.").let { println(it) }

Prefix query

client.search(indexName) {
  query = prefix(TestDoc::tags, "fru")
}.pretty("Prefix Query.").let { println(it) }

This prints:

Prefix Query. Found 2 results:
- 1.0 1 Apple
- 1.0 2 Banana

Wildcard query

client.search(indexName) {
  query = wildcard(TestDoc::tags, "f*")
}.pretty("Wildcard Query.").let { println(it) }

This prints:

Wildcard Query. Found 2 results:
- 1.0 1 Apple
- 1.0 2 Banana

RegExp query

client.search(indexName) {
  query = regExp(TestDoc::tags, "(fruit|legumes)")
}.pretty("RegExp Query.").let { println(it) }

This prints:

RegExp Query. Found 3 results:
- 1.0 1 Apple
- 1.0 2 Banana
- 1.0 3 Green Beans

Ids query

client.search(indexName) {
  query = ids("1", "2")

}.pretty("Ids Query.").let { println(it) }

This prints:

Ids Query. Found 2 results:
- 1.0 1 Apple
- 1.0 2 Banana

Exists query

client.search(indexName) {
  query = ids("1", "2")
}.pretty("Exists Query.").let { println(it) }

This prints:

Exists Query. Found 2 results:
- 1.0 1 Apple
- 1.0 2 Banana

Range query

client.search(indexName) {
  query = range(TestDoc::price) {
    gt=0
    lte=100.0
  }

}.pretty("Range Query.").let { println(it) }

This prints:

Range Query. Found 3 results:
- 1.0 1 Apple
- 1.0 2 Banana
- 1.0 3 Green Beans

Terms Set query

client.search(indexName) {
  query = termsSet(TestDoc::tags, "fruit","legumes","foo") {
    minimumShouldMatchScript = Script.create {
      source = "2"
    }
  }
}.pretty("Terms Set Query").let { println(it) }

This prints:

Terms Set Query Found 0 results:


KT Search Manual Previous: Text Queries Next: Compound Queries
Github © Jilles van Gurp