# Query string and highlighting not working as expected

**URL:** <https://discuss.elastic.co/t/query-string-and-highlighting-not-working-as-expected/50116>\
**Category:** Elasticsearch\
**Created:** [May 16, 2016, 12:52pm UTC](https://discuss.elastic.co/t/query-string-and-highlighting-not-working-as-expected/50116 "2016-05-16T12:52:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolinost](https://avatars.discourse-cdn.com/v4/letter/n/48db29/32.png) [@nicolinost](https://discuss.elastic.co/u/nicolinost)\
**Post date:** [May 16, 2016, 12:52pm UTC](https://discuss.elastic.co/t/query-string-and-highlighting-not-working-as-expected/50116/1 "2016-05-16T12:52:37Z")

</div>

Hello everyone,

I'm trying to highlight a search query. The below query, is expected to highlight only elastic search if apple is in the text ... unfortunately, NO ! The highlight function does not take care about the boolean expression. Am I correct ?

Thank you.

Nicolas.

```auto
{
   "query": {
      "filtered": {
         "query": {
            "query_string": {
               "default_field": "message",
               "query": "flexible OR (elasticsearch AND apple)"
            }
         }
      }
   },
   "highlight": {
      "fields": {
         "message": {}
      }
   }
}

```

---

<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:** [May 16, 2016, 1:07pm UTC](https://discuss.elastic.co/t/query-string-and-highlighting-not-working-as-expected/50116/2 "2016-05-16T13:07:21Z")

</div>

I suspect the filter is getting in the way of the terms extraction process. Either use [highlight\_query](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html#_highlight_query) to work around it or remove the `filtered` query from the query.

PS. I edited your query and replaced "`" with "```" to fix the code block. It makes it much easier to read!

---

<div class="post-metadata">

**Author:** ![nicolinost](https://avatars.discourse-cdn.com/v4/letter/n/48db29/32.png) [@nicolinost](https://discuss.elastic.co/u/nicolinost)\
**Post date:** [May 16, 2016, 1:10pm UTC](https://discuss.elastic.co/t/query-string-and-highlighting-not-working-as-expected/50116/3 "2016-05-16T13:10:50Z")

</div>

I saw your edition. Thank you.

I removed the filter and apply just query with no filter. Unfortunately, I got the same result ☹

```auto
{
   "query": {
      "query_string": {
         "default_field": "message",
         "query": "flexible OR (elasticsearch AND apple)"
      }
   },
   "highlight": {
      "fields": {
         "message": {}
      }
   }
}

```

---

<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:** [May 16, 2016, 1:56pm UTC](https://discuss.elastic.co/t/query-string-and-highlighting-not-working-as-expected/50116/4 "2016-05-16T13:56:22Z")

</div>

Oh! I see what you mean:

> [@nicolinost](#):
>
> The highlight function does not take care about the boolean expression.

You are right. For the most part it just flattens the booleans. If you were using a bool query you could use [named queries](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-named-queries-and-filters.html) to know which queries match. But, yeah, highlighting tends to flatten.

---

<div class="post-metadata">

**Author:** ![nicolinost](https://avatars.discourse-cdn.com/v4/letter/n/48db29/32.png) [@nicolinost](https://discuss.elastic.co/u/nicolinost)\
**Post date:** [May 16, 2016, 2:21pm UTC](https://discuss.elastic.co/t/query-string-and-highlighting-not-working-as-expected/50116/5 "2016-05-16T14:21:26Z")

</div>

> [@nik9000](#):
>
> But, yeah, highlighting tends to flatten.

Well, I can not see the advantage to highlight a text without taking into account the boolean request. Do you have one ?

Thank you.  
Nicolas.

---

<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:** [May 16, 2016, 2:34pm UTC](https://discuss.elastic.co/t/query-string-and-highlighting-not-working-as-expected/50116/6 "2016-05-16T14:34:22Z")

</div>

> [@nicolinost](#):
>
> Well, I can not see the advantage to highlight a text without taking into account the boolean request. Do you have one ?

That is just how they work. Highlighters are messy and tend to make lots of simplifying assumptions.

---

<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, 10:51pm UTC](https://discuss.elastic.co/t/query-string-and-highlighting-not-working-as-expected/50116/7 "2017-07-05T22:51:21Z")

</div>


