jillesvangurp / kt-search

Multi platform kotlin client for Elasticsearch & Opensearch with easily extendable Kotlin DSLs for queries, mappings, bulk, and more.
MIT License
95 stars 23 forks source link

add support for highlighting in QueryDSL #121

Closed larsgraedig closed 3 months ago

larsgraedig commented 3 months ago

Added support for highlighting in SearchDSL according to https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html

Sample query:

{
  query = bool {
      [...]
  }

  highlight {
      boundaryChars = ".,!? \\t\\n"
      boundaryMaxScan = 20
      boundaryScanner = BoundaryScanner.chars
      boundaryScannerLocale = "de-DE"
      encoder = Encoder.default
      fragmenter = Fragmenter.span
      fragmentOffset = 10
      fragmentSize = 5
      highlightQuery =
          bool {
              should(
                  multiMatch(
                      "myQuery",
                      "somefield",
                      "anotherfield",
                  ),
              )
          }
      matchedFields(
          "field1",
          "field2",
      )
      noMatchSize = 2
      numberOfFragments = 3
      order = Order.score
      phraseLimit = 3
      preTags = "<b>"
      postTags = "</b>"
      requireFieldMatch = true
      maxAnalyzedOffset = 2
      tagsSchema = "styled"
      type = Type.unified
      fields(
          field("myField") {
              this.phraseLimit = 5
          },
      )
  }
}

It translates to:

{
  "query": {
    [...]
  },
  "highlight": {
    "boundary_chars": ".,!? \\t\\n",
    "boundary_max_scan": 20,
    "boundary_scanner": "chars",
    "boundary_scanner_locale": "de-DE",
    "encoder": "default",
    "fragmenter": "span",
    "fragment_offset": "10",
    "fragment_size": 5,
    "highlight_query": {
      "bool": {
        "should": {
          "multi_match": {
            "query": "myQuery",
            "fields": [
              "somefield",
              "anotherfield"
            ]
          }
        }
      }
    },
    "matched_fields": [
      "field1",
      "field2"
    ],
    "no_match_size": 2,
    "number_of_fragments": 3,
    "order": "score",
    "phrase_limit": 3,
    "pre_tags": "<b>",
    "post_tags": "</b>",
    "required_field_match": true,
    "max_analyzed_offset": 2,
    "tags_schema": "styled",
    "type": "unified",
    "fields": {
      "myField": {
        "phrase_limit": 5
      }
    }
}
jillesvangurp commented 3 months ago

Thanks for this!

larsgraedig commented 3 months ago

Thanks for accepting! Keep up the good work - we really enjoy using your library in our project 👍