Search as you type and highlighter

Hello,

Env:
ES 7.15.0

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "my_field": {
        "type": "search_as_you_type"
      }
    }
  }
}

PUT my-index-000001/_doc/1?refresh
{
  "my_field": "quick brown fox jump lazy dog"
}

I am trying to accomplish autocomplete and highlight at the same time. Something like algolia autocomplete
image

I took code from Search-as-you-type field type | Elasticsearch Guide [7.15] | Elastic and add a highlight section. The result is that highlighter works just for whole words. I am expecting that even partial prefix matches will be highlighted.

For example from this query:

GET my-index-000001/_search
{
  "query": {
    "multi_match": {
      "query": "brow",
      "type": "bool_prefix",
      "fields": [
        "my_field",
        "my_field._2gram",
        "my_field._3gram"
      ]
    }
  },
  "highlight": {
    "fields": {
      "my_field": {
      }
    }
  }
}

I would expect:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "my_field" : "quick brown fox jump lazy dog"
        },
        "highlight" : {
          "my_field" : [
            "quick <em>brow</em>n fox jump lazy dog"
          ]
        }
      }
    ]
  }
}

Real result:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "my_field" : "quick brown fox jump lazy dog"
        }
      }
    ]
  }
}

  • I also tried "match_phrase_prefix" query but had no success.
  • I also found some solutions with additional mapping with a separated ngram analyzer used just for highlighting, but it doesn't look like the right path to go.
  • If I try to highlight on my_field._index_prefix result is strange
    .
    Is it possible to create it in this way?

Thanks.

1 Like

Having the same question. Running the same type of query and wonder why there is no match for a subword or a single word. Say I am looking for a word "triangle". It won't get highlighted with this type of query for strings "tri", "tria" ... "triangle". For phrase "right triangle" it will highlight though.

Is this a bug or a correct behavior? What is the workaround then? I could try changing the query type for a one word string, but only "bool_prefix" produces results with substrings of the word.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.