# Highlight doesn't work if not in first position

**URL:** <https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865>\
**Category:** Elasticsearch\
**Created:** [December 8, 2017, 3:01pm UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865 "2017-12-08T15:01:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sylvain\_Attoumani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_attoumani/32/45401_2.png) [@Sylvain\_Attoumani](https://discuss.elastic.co/u/Sylvain_Attoumani)\
**Post date:** [December 8, 2017, 3:01pm UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/1 "2017-12-08T15:01:19Z")

</div>

I'm working with elasticsearch and my highlight doesn't give me what I expect .My mappings looks like that :

```auto
PUT my_index
{
      "settings": {
           "analysis": {
                "analyzer": {
                     "my_analyzer": {
                          "tokenizer": "my_tokenizer"
                     }
                },
                "tokenizer": {
                     "my_tokenizer": {
                          "type": "ngram",
                          "min_gram": 2,
                          "max_gram": 25,
                          "token_chars": [
                               "letter",
                               "digit"
                          ]
                     }
                }
           }
      }
}

```

I put some product in my index

```auto
PUT index/product/1
{
     "name" : "Kit Guirlande Guinguette 50m Transparent",
     "field2": "foo"
}

PUT index/product/2
{
     "name": "Guirlande Guinguette Blanc 20 Bulbes 10M",
      "field2": "foo"
}

```

The mapping for name and field2 :

```auto
"name": {
    "type": "text",
    "fields": {
      "keyword": {
          "type": "keyword",
          "ignore_above": 256
       }
     },
    "analyzer": "my_analyzer"
},
"fields2": {
    "type": "text",
    "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        }
     },
     "analyzer": "my_analyzer"
},

```

And I'm doing a research :

```auto
GET index/product/_search
{
     "query":{
          "multi_match": {
               "query" : "guirlande gui"
               "fields":[
                    'name','field2'
               ]
              "minimum_should_match" : "100%"
          }
     }
     "highlight" : {
          "fields":{
              "name" : {}
          }
     }
}

```

Response

```auto
{
 "hits": {
      "total": 2,
       "hits": [
             {
                   "_index":"index",
                   "_type": "product",
                   "_id": "1",
                   "_source": {
                         "name": "Guirlande Guinguette Blanc 20 Bulbes 10M"
                    },
                   "highlight": {
                         "name": [
                               " <em>Guirlande Gui</em>nguette Blanc 20 Bulbes 10M"
                         ]
                   }
             },

             {
                   "_index": "index",
                   "_type": "product",
                   "_id": "2",
                   "_score": 1.601195,
                   "_source": {
                         "name": "Kit Guirlande Guinguette 30m Blanche"
                    },
                   "highlight": {
                         "name": [
                               " Kit Guirlande Guinguette 30m Blanche"
                         ]
                   }
             }
       ]
 }
}

```

But for the second hit in highlight I would like to have `" Kit <em>Guirlande Gui</em>nguette 30m Blanche"`

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 8, 2017, 6:40pm UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/2 "2017-12-08T18:40:22Z")

</div>

Could you provide a full recreation script as described in

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

It will help to better understand what you are doing.  
Please, try to keep the example as simple as possible.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Attoumani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_attoumani/32/45401_2.png) [@Sylvain\_Attoumani](https://discuss.elastic.co/u/Sylvain_Attoumani)\
**Post date:** [December 13, 2017, 3:00pm UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/3 "2017-12-13T15:00:09Z")

</div>

Is it better like that @dadoonet ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 19, 2017, 8:18am UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/4 "2017-12-19T08:18:35Z")

</div>

It's better but I had to modify it as some content in not a valid JSON and the index name is wrong. Also I had to manually apply the mapping. This is requiring efforts on our side to make all that run. Providing a fully running script definitely helps and reduces the time needed to answer. So please, provide next time something like:

```auto
DELETE index
PUT index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 2,
          "max_gram": 25,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "product": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "my_analyzer"
        },
        "fields2": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}
PUT index/product/1
{
     "name" : "Kit Guirlande Guinguette 50m Transparent",
     "field2": "foo"
}

PUT index/product/2
{
     "name": "Guirlande Guinguette Blanc 20 Bulbes 10M",
      "field2": "foo"
}
GET index/product/_search
{
     "query":{
          "multi_match": {
               "query" : "guirlande gui",
               "fields":[
                    "name", "field2"
               ],
              "minimum_should_match" : "100%"
          }
     },
     "highlight" : {
          "fields":{
              "name" : {}
          }
     }
}

```

Anyway.

Note first that there is a deprecation notice when running in 6.1. Something you need to be aware of.

```auto
#! Deprecation: Deprecated big difference between max_gram and min_gram in NGram Tokenizer,expected difference must be less than or equal to: [1]

```

When I search with your query, it does not match at all:

```auto
GET index/product/_search
{
     "query":{
          "multi_match": {
               "query" : "guirlande gui",
               "fields":[
                    "name", "field2"
               ],
              "minimum_should_match" : "100%"
          }
     },
     "highlight" : {
          "fields":{
              "name" : {}
          }
     }
}

```

```auto
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

```

May I recommend that you provide a fully working example?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 19, 2017, 8:22am UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/5 "2017-12-19T08:22:41Z")

</div>

Also have a look at what your analyzer is producing:

Here is what your analyzer is doing when processing your text:

```auto
POST index/_analyze
{
  "text": ["Guirlande Guinguette Blanc 20 Bulbes 10M"],
  "analyzer": "my_analyzer"
}

```

That could help may be.

That being said, I doubt highlighting works on ngram based data. If you are searching against a ngram field like `name`, then you probably need to store explicitly this field in order to be able to highlight it? But I'm unsure and I'd prefer to get @jimczi's thoughts.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Attoumani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_attoumani/32/45401_2.png) [@Sylvain\_Attoumani](https://discuss.elastic.co/u/Sylvain_Attoumani)\
**Post date:** [December 19, 2017, 8:32am UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/6 "2017-12-19T08:32:38Z")

</div>

Ok @dadoonet thanks for the suggestion. I changed few things but It still doesn't work. I hope this is better for you like that :

```auto
DELETE my_index
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer",
          "filter": ["lowercase","asciifolding"]
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 2,
          "max_gram": 25,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "product": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            },
            "search": {
              "type": "text",
              "search_analyzer": "standard",
              "analyzer": "my_analyzer"
            }
          },
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}

PUT my_index/product/1
{
 "name" : "Kit Guirlande Guinguette 50m Transparent",
 "field2": "foo"
}

PUT my_index/product/2
{
 "name": "Guirlande Guinguette Blanc 20 Bulbes 10M",
  "field2": "foo"
}

GET my_index/product/_search
{
  "query": {
    "multi_match": {
      "query": "Guirlande Gui",
      "fields": [
        "name",
        "field2"
      ],
      "minimum_should_match": "100%"
    }
  },
  "highlight": {
    "fields": {
      "name.search": {
        "highlight_query": {
          "match": {
            "name.search": {
              "query": "Guirlande Gui"
            }
          }
        }
      }
    }
  }
}

```

With that I have the following result :

```auto
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 12.150877,
    "hits": [
      {
        "_index": "my_index",
        "_type": "product",
        "_id": "2",
        "_score": 12.150877,
        "_source": {
          "name": "Guirlande Guinguette Blanc 20 Bulbes 10M",
          "field2": "foo"
        },
        "highlight": {
          "name.search": [
            "<em>Guirlande</em> Guinguette Blanc 20 Bulbes 10M"
          ]
        }
      },
      {
        "_index": "my_index",
        "_type": "product",
        "_id": "1",
        "_score": 11.431992,
        "_source": {
          "name": "Kit Guirlande Guinguette 50m Transparent",
          "field2": "foo"
        },
        "highlight": {
          "name.search": [
            "Kit Guirlande Guinguette 50m Transparent"
          ]
        }
      }
    ]
  }
}

```

And what I want is :

```auto
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 195.12189,
    "hits": [
      {
        "_index": "my_index",
        "_type": "product",
        "_id": "2",
        "_score": 195.12189,
        "_source": {
          "name": "Guirlande Guinguette Blanc 20 Bulbes 10M",
          "field2": "foo"
        },
        "highlight": {
          "name.search": [
            "<em>Guirlande Gui</em>nguette Blanc 20 Bulbes 10M"
          ]
        }
      },
      {
        "_index": "my_index",
        "_type": "product",
        "_id": "1",
        "_score": 197.8033,
        "_source": {
          "name": "Kit Guirlande Guinguette 50m Transparent",
          "field2": "foo"
        },
        "highlight": {
          "name.search": [
            "Kit <em>Guirlande Gui</em>nguette 50m Transparent"
          ]
        }
      }
    ]
  }
}

```

And by the way I'm using elasticsearch 5.6.4

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 19, 2017, 8:46am UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/7 "2017-12-19T08:46:40Z")

</div>

Thank you. With 6.1, here is what I'm getting:

```auto
{
  "took": 75,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 12.554357,
    "hits": [
      {
        "_index": "my_index",
        "_type": "product",
        "_id": "2",
        "_score": 12.554357,
        "_source": {
          "name": "Guirlande Guinguette Blanc 20 Bulbes 10M",
          "field2": "foo"
        },
        "highlight": {
          "name.search": [
            "<em>Gui</em><em>rlande</em> <em>Gui</em>nguette Blanc 20 Bulbes 10M"
          ]
        }
      },
      {
        "_index": "my_index",
        "_type": "product",
        "_id": "1",
        "_score": 12.281757,
        "_source": {
          "name": "Kit Guirlande Guinguette 50m Transparent",
          "field2": "foo"
        },
        "highlight": {
          "name.search": [
            "Kit <em>Gui</em><em>rlande</em> <em>Gui</em>nguette 50m Transparent"
          ]
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![Sylvain\_Attoumani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_attoumani/32/45401_2.png) [@Sylvain\_Attoumani](https://discuss.elastic.co/u/Sylvain_Attoumani)\
**Post date:** [December 19, 2017, 8:50am UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/8 "2017-12-19T08:50:27Z")

</div>

Hmm well. I have really no Idea why I don't have that 🤔 thanks for your time anyway @dadoonet

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 19, 2017, 9:01am UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/9 "2017-12-19T09:01:34Z")

</div>

May be because the behavior changed in 6.x series? IIRC @jimczi worked on that.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Attoumani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_attoumani/32/45401_2.png) [@Sylvain\_Attoumani](https://discuss.elastic.co/u/Sylvain_Attoumani)\
**Post date:** [December 19, 2017, 10:13am UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/10 "2017-12-19T10:13:35Z")

</div>

Also I have weirdly high score results I don't know if that's normal

---

<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:** [January 16, 2018, 10:13am UTC](https://discuss.elastic.co/t/highlight-doesnt-work-if-not-in-first-position/110865/11 "2018-01-16T10:13:37Z")

</div>

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