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

Highlighting

KT Search Manual Previous: Join Queries Next: Deleting by query
Github © Jilles van Gurp  

Highlighting allows you to show to your users why particular results are matching a query.

client.search(indexName) {
  query=match(TestDoc::name,"bananana") {
    fuzziness="AUTO"
  }
  // create a highlight on the name field with default settings
  highlight {
    add(TestDoc::name)
  }
}

{ “took”: 36, “_shards”: { “total”: 1, “successful”: 1, “failed”: 0, “skipped”: 0 }, “timed_out”: false, “hits”: { “max_score”: 0.7283795, “total”: { “value”: 1, “relation”: “eq” }, “hits”: [ { “_index”: “docs-term-highlighting-demo”, “_id”: “2”, “_score”: 0.7283795, “_source”: { “id”: “2”, “name”: “Banana”, “tags”: [ “fruit” ], “price”: 0.8 }, “highlight”: { “name”: [ “Banana” ] } } ] } }

Of course you can customize how highlighting works:

client.search(indexName) {
  query=match(TestDoc::name,"bananana") {
    fuzziness="AUTO"
  }
  // create a highlight on the name field with default settings
  highlight {
    // use some alternative tags instead of the defaults
    preTags="<pre>"
    postTags="</pre>"

    add(TestDoc::name) {
      // configure some per field settings
      type = Type.plain
      fragmenter=Fragmenter.span
    }
  }
}

{ “took”: 15, “_shards”: { “total”: 1, “successful”: 1, “failed”: 0, “skipped”: 0 }, “timed_out”: false, “hits”: { “max_score”: 0.7283795, “total”: { “value”: 1, “relation”: “eq” }, “hits”: [ { “_index”: “docs-term-highlighting-demo”, “_id”: “2”, “_score”: 0.7283795, “_source”: { “id”: “2”, “name”: “Banana”, “tags”: [ “fruit” ], “price”: 0.8 }, “highlight”: { “name”: [ “<pre>Banana</pre>” ] } } ] } }


KT Search Manual Previous: Join Queries Next: Deleting by query
Github © Jilles van Gurp