# Search\_as\_you\_type and highlight

**URL:** <https://discuss.elastic.co/t/search-as-you-type-and-highlight/253010>\
**Category:** Elasticsearch\
**Created:** [October 22, 2020, 1:42pm UTC](https://discuss.elastic.co/t/search-as-you-type-and-highlight/253010 "2020-10-22T13:42:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wos/32/77586_2.png) [@wos](https://discuss.elastic.co/u/wos)\
**Post date:** [October 22, 2020, 1:42pm UTC](https://discuss.elastic.co/t/search-as-you-type-and-highlight/253010/1 "2020-10-22T13:42:46Z")

</div>

I am looking for working example of a search\_as\_you\_type and highlight. Could Someone provide me with example? Thanks

---

<div class="post-metadata">

**Author:** ![xeraa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xeraa/32/48181_2.png) [@xeraa](https://discuss.elastic.co/u/xeraa)\
**Post date:** [November 3, 2020, 1:40am UTC](https://discuss.elastic.co/t/search-as-you-type-and-highlight/253010/2 "2020-11-03T01:40:12Z")

</div>

```auto
PUT test
{
  "mappings": {
    "properties": {
      "my_field": {
        "type": "search_as_you_type"
      }
    }
  }
}

PUT test/_doc/1
{
  "my_field": "quick brown fox jump lazy dog"
}

GET test/_search
{
  "query": {
    "multi_match": {
      "query": "brown fox ",
      "type": "bool_prefix",
      "fields": [
        "my_field",
        "my_field._2gram",
        "my_field._3gram"
      ]
    }
  },
  "highlight": {
    "fields": {
      "my_field": {
        "type" : "unified"
      }
    }
  }
}

```

That leads to the result:

```auto
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.8630463,
        "_source" : {
          "my_field" : "quick brown fox jump lazy dog"
        },
        "highlight" : {
          "my_field" : [
            "quick <em>brown</em> <em>fox</em> jump lazy dog"
          ]
        }
      }
    ]

```

It will only highlight matching tokens — if you leave out the space after fox, it won't mark that. Are you expecting partial highlighting instead? Not sure this would make sense for an autocomplete / search as you type field.

---

<div class="post-metadata">

**Author:** ![wos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wos/32/77586_2.png) [@wos](https://discuss.elastic.co/u/wos)\
**Post date:** [November 3, 2020, 6:35pm UTC](https://discuss.elastic.co/t/search-as-you-type-and-highlight/253010/3 "2020-11-03T18:35:48Z")

</div>

> [@xeraa](#):
>
> ```auto
> "highlight": {
> "fields": {
> "my_field": {
> "type" : "unified"
> }
> }
> }
> 
> ```

Thanks. My problem is that I use a copy field. Mapping:  
{  
"properties": {  
"abstract": {  
"type": "text",  
"copy\_to": "freetext"  
},  
"name": {  
"type": "text",  
"copy\_to": "freetext"  
},  
"freetext": {  
"type": "text",  
"fields": {  
"keyword": {  
"type": "keyword"  
},  
"completion": {  
"type": "completion"  
},  
"shingle": {  
"type": "search\_as\_you\_type"  
}  
}  
}  
}  
}

and search:  
{  
"\_source": false,  
"docvalue\_fields": [  
{  
"field": "freetext.keyword"  
}  
],  
"size": 10,  
"query": {  
"multi\_match": {  
"query": "summ",  
"type": "bool\_prefix",  
"fields": [  
"freetext.shingle",  
"freetext.shingle.\_2gram",  
"freetext.shingle.\_3gram"  
]  
}  
},  
"highlight": {  
"fields": {  
"freetext.shingle": {  
"type": "unified"  
}  
}  
}  
}

It looks that the problem is that the freetext is not part of the original document. It is a copy document. I can display it with "docvalue\_fields", but is it possible to get the highligting working for the copy field?

---

<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 1, 2020, 6:35pm UTC](https://discuss.elastic.co/t/search-as-you-type-and-highlight/253010/4 "2020-12-01T18:35:59Z")

</div>

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