# Elasticsearch- highlighting on both “.keyword” and text fields

**URL:** <https://discuss.elastic.co/t/elasticsearch-highlighting-on-both-keyword-and-text-fields/157325>\
**Category:** Elasticsearch\
**Created:** [November 19, 2018, 10:03am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting-on-both-keyword-and-text-fields/157325 "2018-11-19T10:03:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [November 19, 2018, 10:03am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting-on-both-keyword-and-text-fields/157325/1 "2018-11-19T10:03:36Z")

</div>

I am finding issues with highlighting when searching on fields using its complete data.I have used custom analyzers and each field is stored as text and keyword.  
I am using whitespace as search analyzer.  
My custom analyzer is:

```
 "analysis": {
                    "filter": {
                        "indexFilter": {
                            "type": "pattern_capture",
                            "preserve_original": "true",
                            "patterns": [
                                "([@,$,%,&,!,.,#,^,*]+)",
                                "([\\w,.]+)",
                                "([\\w,@]+)",
                                "([-]+)",
                                "(\\w+)"
                            ]
                        }
                    },
                    "analyzer": {
                        "indexAnalyzer": {
                            "filter": [
                                "indexFilter",
                                "lowercase"
                            ],
                            "tokenizer": "whitespace"
                        },
                        "searchAnalyzer": {
                            "filter": [
                                "lowercase"
                            ],
                            "tokenizer": "whitespace"
                        }
                    }

```

My mapping file is :

```
          "field": {
                "type": "text",
                "term_vector": "with_positions_offsets",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                },
                "analyzer": "indexAnalyzer",
                "search_analyzer": "searchAnalyzer"
            }

```

My query is :

```
{"from" : 0, "size" : 24, "query": {
    "bool": {"should": [{
          "multi_match": {
            "query":"monkey business",
            "type":"phrase",
            "slop":"2",
            "fields":[]
}}],"minimum_should_match":1}}, "highlight": {
        "type" : "unified",
        "fields": {
            "*": {}
        }
    }}

```

my highlight results are :

```
"highlight": {
                    "field.keyword": [
                        "<em>monkey business</em>"
                    ],
                    "field": [
                        "<em>monkey</em> <em>business</em>"
                    ]
                }
```

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [November 20, 2018, 7:11am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting-on-both-keyword-and-text-fields/157325/2 "2018-11-20T07:11:59Z")

</div>

@elastic

---

<div class="post-metadata">

**Author:** ![edoardo.zanon](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@edoardo.zanon](https://discuss.elastic.co/u/edoardo.zanon)\
**Post date:** [November 20, 2018, 7:21am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting-on-both-keyword-and-text-fields/157325/3 "2018-11-20T07:21:35Z")

</div>

Yes, the problem is the slop param in phrase match. I've notice the same result. I think that the analyzer in this case work ad an phrase + an match query with some param.

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [November 20, 2018, 7:59am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting-on-both-keyword-and-text-fields/157325/4 "2018-11-20T07:59:07Z")

</div>

Could you please elaborate? How do i over come this issue?

---

<div class="post-metadata">

**Author:** ![Jaspreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaspreet_singh/32/58296_2.png) [@Jaspreet\_Singh](https://discuss.elastic.co/u/Jaspreet_Singh)\
**Post date:** [November 20, 2018, 9:23pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting-on-both-keyword-and-text-fields/157325/5 "2018-11-20T21:23:05Z")

</div>

What you are seeing in highlight.field is exactly the same outcome as a "match\_phrase" query on "field" field, type text.  
And what you are seeing in highlight.field.keyword is exactly the same as "match\_phrase" on "field.keyword", of type keyword. (for keyword fields, match and match\_phrase behave identically, as these aren't analyzed).

What really happens in match\_phrase or a multi\_match with type "phrase" is, query will find (and highlight) all search tokens that occur together. (for slop it is a touch different but concepts apply similarly and lets keep this simple).  
So for text type, your search string "monkey business" translates into 2 separate tokens, "monkey" and "business".  
Now your query finds all documents that has both of these together. That is exactly what you see in highlights - tokens "monkey" and "business" highlighted together, but in same "em" tag since they are separate tokens.  
For keyword field, there is no analysis phase. So your search string is the exact same as search tokens, "monkey business". Any query (not just phrase) on keyword will try and find exact same search strings (include same case) and highlight them under same "em" tags.  
Hope this helps. This may not be technically most accurate but explains what may be happening.

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [November 21, 2018, 5:49am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting-on-both-keyword-and-text-fields/157325/6 "2018-11-21T05:49:50Z")

</div>

Thanks for your response.  
The same issue persists even when "type": "phrase" is removed as well.

```
 {"from" : 0, "size" : 24, "query": {
        "bool": {"should": [{
              "multi_match": {
                "query":"monkey business",
                        "fields":[]
    }}],"minimum_should_match":1}}, "highlight": {
            "type" : "unified",
            "fields": {
                "*": {}
            }
        }}
```

---

<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 19, 2018, 5:49am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting-on-both-keyword-and-text-fields/157325/7 "2018-12-19T05:49:51Z")

</div>

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