# Search as you type and highlighter

**URL:** <https://discuss.elastic.co/t/search-as-you-type-and-highlighter/287341>\
**Category:** Elasticsearch\
**Created:** [October 21, 2021, 5:20pm UTC](https://discuss.elastic.co/t/search-as-you-type-and-highlighter/287341 "2021-10-21T17:20:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mihalikv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mihalikv/32/26793_2.png) [@mihalikv](https://discuss.elastic.co/u/mihalikv)\
**Post date:** [October 21, 2021, 5:20pm UTC](https://discuss.elastic.co/t/search-as-you-type-and-highlighter/287341/1 "2021-10-21T17:20:17Z")

</div>

Hello,

Env:  
ES 7.15.0

```auto
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](https://us1.discourse-cdn.com/elastic/original/3X/2/3/23f05f1f0ab6693739a3fa6e5fc3b7cf31489a58.png)

I took code from [Search-as-you-type field type | Elasticsearch Guide [7.15] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.15/search-as-you-type.html) 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:

```auto
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:

```auto
{
  "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:

```auto
{
  "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.

---

<div class="post-metadata">

**Author:** ![konman1989](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/konman1989/32/96728_2.png) [@konman1989](https://discuss.elastic.co/u/konman1989)\
**Post date:** [November 4, 2021, 2:49pm UTC](https://discuss.elastic.co/t/search-as-you-type-and-highlighter/287341/2 "2021-11-04T14:49:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 2, 2021, 2:49pm UTC](https://discuss.elastic.co/t/search-as-you-type-and-highlighter/287341/3 "2021-12-02T14:49:32Z")

</div>

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