# Filter vs bool Query Performance , getting bad performance on using Filters

**URL:** <https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182>\
**Category:** Elasticsearch\
**Created:** [May 2, 2018, 5:13am UTC](https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182 "2018-05-02T05:13:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jalaj](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jalaj](https://discuss.elastic.co/u/jalaj)\
**Post date:** [May 2, 2018, 5:13am UTC](https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182/1 "2018-05-02T05:13:33Z")

</div>

Hi,  
Generally it is recommended to use filters for optimal performance, but I am getting 5 times more latency when using filters compared to bool query.  
However for me it does not make sense to use bool as logically I inly have to filter.

The queries are

```
GET entity/entity/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "type": [
                    "tags"
                  ]
                }
              },
              {
                "terms": {
                  "state": [
                   0
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "prefix": {
                  "name": {
                    "value": "a"
                  }
                }
              },
              {
                "prefix": {
                  "ID": {
                    "value": "a"
                  }
                }
              }
              
            ]
          }
        }
      ]
    }
  }
}

```

The other query is

```
GET entity/entity/_search
{
  "query": { 
    "bool": { 
      "should": [
              {
                "prefix": {
                  "name": {
                    "value": "a"
                  }
                }
              },
              {"prefix": {
                  "ID": {
                    "value": "a"
                  }
              }
              }
              
            ],
      "filter": [ 
        { "term": { "type": "tags" }},
        {"term":{"state":0}}
      ]
    }
  }
}

```

The filter query is comes out in 3 seconds and bool query takes 300-400 ms.  
The total number of hits differ significantly with a 10K on filter query and 500 on bool query.  
Total documents on entity index are 150 million.

Why does filter query have more number of hits then bool query and bad performance ?

---

<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:** [May 2, 2018, 5:35am UTC](https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182/2 "2018-05-02T05:35:16Z")

</div>

Is that always the case? I mean after some runs it should be fast, right?

---

<div class="post-metadata">

**Author:** ![jalaj](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jalaj](https://discuss.elastic.co/u/jalaj)\
**Post date:** [May 9, 2018, 11:03am UTC](https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182/3 "2018-05-09T11:03:57Z")

</div>

@dadoonet It is always the case.

---

<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:** [May 9, 2018, 12:06pm UTC](https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182/4 "2018-05-09T12:06:29Z")

</div>

Can you share the outputs of each query?

Which versions?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 9, 2018, 12:50pm UTC](https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182/5 "2018-05-09T12:50:33Z")

</div>

Your queries are not equivalent. The prefix queries are required to match in the first query but optional in the second query. This is why you are getting different counts. You need to put your SHOULD clauses under a MUST clause like in the first query.

---

<div class="post-metadata">

**Author:** ![jalaj](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jalaj](https://discuss.elastic.co/u/jalaj)\
**Post date:** [May 9, 2018, 1:22pm UTC](https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182/6 "2018-05-09T13:22:01Z")

</div>

@jpountz Both queries have should only.  
The part which is in must in first is on must filter in other.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 9, 2018, 1:55pm UTC](https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182/7 "2018-05-09T13:55:06Z")

</div>

Take your first query and only replace `must` with `filter`. You will get the same number of hits. Your first query requires that one of the prefix queries matches while the second one doesn't. This is why you don't get the same numbers of hits and worse performance.

---

<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:** [June 6, 2018, 1:55pm UTC](https://discuss.elastic.co/t/filter-vs-bool-query-performance-getting-bad-performance-on-using-filters/130182/8 "2018-06-06T13:55:25Z")

</div>

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