# Highlighting of irrelevant text between matches

**URL:** <https://discuss.elastic.co/t/highlighting-of-irrelevant-text-between-matches/27525>\
**Category:** Elasticsearch\
**Created:** [August 17, 2015, 4:26pm UTC](https://discuss.elastic.co/t/highlighting-of-irrelevant-text-between-matches/27525 "2015-08-17T16:26:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![drippel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drippel/32/44823_2.png) [@drippel](https://discuss.elastic.co/u/drippel)\
**Post date:** [August 17, 2015, 4:26pm UTC](https://discuss.elastic.co/t/highlighting-of-irrelevant-text-between-matches/27525/1 "2015-08-17T16:26:44Z")

</div>

When I index the following text:

```
"_source":{"_analyzer":"english_index","streamId":1,"language":"english","message":"The Doctor to anyone who tries to speak the truth and to reveal how evil he is"}

```

and I run the following search with highlighting request:

```
{
  "size": 50,
  "query": {
    "bool": {
      "must": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "_all": {
                        "query": "doctor",
                        "type": "boolean",
                        "analyzer": "english"
                      }
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "_all": {
                        "query": "evil",
                        "type": "boolean",
                        "analyzer": "english"
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  },
  "post_filter": {
    "term": {
      "_type": "Document"
    }
  },
  "highlight": {
    "pre_tags": [
      "<b>"
    ],
    "post_tags": [
      "</b>"
    ],
    "fragment_size": 0,
    "number_of_fragments": 0,
    "fields": {
      "WordA": {},
      "WordB": {},
      "WordC": {},
      "message": {},
      "user": {}
    }
  }
}

```

I get the following result:

```
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 30,
    "successful" : 30,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.7589359,
    "hits" : [ {
      "_index" : "fts-english",
      "_type" : "Document",
      "_id" : "mixed_en_cn",
      "_score" : 1.7589359,
      "_source":{"_analyzer":"english_index","streamId":1,"language":"english","message":"The Doctor to anyone who tries to speak the truth and to reveal how evil he is"},
      "highlight" : {
        "message" : ["The <b>Doctor to anyone who tries to speak the truth and to reveal how evil</b> he is"]
      }
    } ]
  }
}

```

As you can see in the highlight field - the whole text between the two matched words is highlighted, whereas I expected to get only the words "Doctor" and "evil" highlighted, much like the following:

```
"message" : ["The <b>Doctor</b> to anyone who tries to speak the truth and to reveal how <b>evil</b> he is"]

```

What is wrong here? Is there a way to fix it or is that an ES bug?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [August 17, 2015, 4:46pm UTC](https://discuss.elastic.co/t/highlighting-of-irrelevant-text-between-matches/27525/2 "2015-08-17T16:46:40Z")

</div>

> [@drippel](#):
>
> What is wrong here? Is there a way to fix it or is that an ES bug?

Sounds like a bug to me. Highlighting is a part of Elasticsearch that really needs some love. At some point in the next few months I suspect I'll start spending a lot of time on it. Until then you might try the `experimental-highlighter` plugin which I worked on before joining Elastic. It tends to work better than the builtin highlighters.

---

<div class="post-metadata">

**Author:** ![drippel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drippel/32/44823_2.png) [@drippel](https://discuss.elastic.co/u/drippel)\
**Post date:** [August 18, 2015, 5:36am UTC](https://discuss.elastic.co/t/highlighting-of-irrelevant-text-between-matches/27525/3 "2015-08-18T05:36:22Z")

</div>

Thank you Nik!  
I will definitely try it.

---

<div class="post-metadata">

**Author:** ![drippel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drippel/32/44823_2.png) [@drippel](https://discuss.elastic.co/u/drippel)\
**Post date:** [August 18, 2015, 6:22am UTC](https://discuss.elastic.co/t/highlighting-of-irrelevant-text-between-matches/27525/4 "2015-08-18T06:22:25Z")

</div>

I tried it and it worked well for the above example, but once I tried phrase search for more than one word in a phrase - it returned no highlighting at all (whereas the same query without the experimental-highlighter did return highlighting).

---

<div class="post-metadata">

**Author:** ![drippel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drippel/32/44823_2.png) [@drippel](https://discuss.elastic.co/u/drippel)\
**Post date:** [January 25, 2016, 5:11pm UTC](https://discuss.elastic.co/t/highlighting-of-irrelevant-text-between-matches/27525/5 "2016-01-25T17:11:30Z")

</div>

Works fine on ES 2.1.1.

---

<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:** [July 5, 2017, 11:21pm UTC](https://discuss.elastic.co/t/highlighting-of-irrelevant-text-between-matches/27525/6 "2017-07-05T23:21:53Z")

</div>


