# Bool query with filter is slower than filtered query

**URL:** https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152
**Category:** Elasticsearch
**Created:** [April 12, 2016, 5:04pm UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152 "2016-04-12T17:04:44Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![eprst](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@eprst](https://discuss.elastic.co/u/eprst)
#### Post date: [April 12, 2016, 5:04pm UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/1 "2016-04-12T17:04:44Z")

</div>

we tested more like this query inside filtered query with some filters. but filtered query is depricated. and that's why we transform it bool query, we put more\_like\_this inside must clause and all filters inside filter. Result counts are the same but the new query is ~20% slower than old one. how can it be possible if filtered query should be transfromed to bool internally?

---

<div class="post-metadata">

### Author: ![eprst](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@eprst](https://discuss.elastic.co/u/eprst)
#### Post date: [April 14, 2016, 1:31am UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/2 "2016-04-14T01:31:15Z")

</div>

when i put the filters inside another bool.must (  
bool:{  
filter:{  
bool:{  
must:[{filter1},{filter2]  
}  
},  
must{mlt}} )  
it works with the same perfomance as filtered query. Can anyone explain why should i do this.

---

<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: [April 14, 2016, 1:36pm UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/3 "2016-04-14T13:36:52Z")

</div>

Can you also provide us with the `filtered` query that you ran as well as the slow `bool` query.

---

<div class="post-metadata">

### Author: ![eprst](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@eprst](https://discuss.elastic.co/u/eprst)
#### Post date: [April 14, 2016, 2:01pm UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/4 "2016-04-14T14:01:47Z")

</div>

{"query": {"bool": {"filter": [{"terms": {"book\_id": [some ids here]}}, {"term": {"not\_available": false}}, {"bool": {"should": [{"exists": {"field": "link"}}, {"exists": {"field": "ISBN"}}]}}], "must": {"more\_like\_this": {"fields": ["text\_field"], "like": [{"\_type": "chapters", "\_id": id}]}}}}}

{"query": {"filtered": {"filter": {"and": [{"terms": {"not\_available": ["false"]}}, {"bool": {"should": [{"exists": {"field": "link"}}, {"exists": {"field": "ISBN"}}]}}, {"terms": {"book\_id": [some ids here]}}]}, "query": {"more\_like\_this": {"fields": ["text\_field"], "like": [{"\_type": "chapters", "\_id": id}]}}}}}

---

<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: [April 14, 2016, 4:41pm UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/5 "2016-04-14T16:41:39Z")

</div>

I am also confused why you are seenig different response times. Could you pass these queries to the `_validate/query` API ([https://www.elastic.co/guide/en/elasticsearch/reference/current/search-validate.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-validate.html)) with `?rewrite=true` and share the output?

---

<div class="post-metadata">

### Author: ![eprst](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@eprst](https://discuss.elastic.co/u/eprst)
#### Post date: [April 14, 2016, 6:43pm UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/6 "2016-04-14T18:43:26Z")

</div>

new  
"explanations":[{"index":"index1","valid":true,"explanation":"+(+(((text\_field:level text\_field:aa text\_field:c18 text\_field:linol text\_field:tabl text\_field:c20 text\_field:c22 text\_field:australian text\_field:linolen text\_field:dha text\_field:powder text\_field:oil text\_field:liquid text\_field:181 text\_field:lcp text\_field:pufa text\_field:la text\_field:infant text\_field:2c text\_field:isom text\_field:acid text\_field:fatti text\_field:lna text\_field:tran text\_field:formula)~7) -ConstantScore(\_uid:chapter#7ab02ae1-063e-4e8d-bd13-19cae103e8b5)) #ConstantScore(book\_id: \u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 book\_id: \u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0002) #not\_available:F #((ConstantScore(\_field\_names:link) ConstantScore(\_field\_names:ISBN))~1)) #ConstantScore(\_type:chapter)"}]}

old  
"explanations":[{"index":"index1","valid":true,"explanation":"+(+(((text\_field:level text\_field:aa text\_field:c18 text\_field:linol text\_field:tabl text\_field:c20 text\_field:c22 text\_field:australian text\_field:linolen text\_field:dha text\_field:powder text\_field:oil text\_field:liquid text\_field:181 text\_field:lcp text\_field:pufa text\_field:la text\_field:infant text\_field:2c text\_field:isom text\_field:acid text\_field:fatti text\_field:lna text\_field:tran text\_field:formula)~7) -ConstantScore(\_uid:chapter#7ab02ae1-063e-4e8d-bd13-19cae103e8b5)) #(+ConstantScore(not\_available:F) +((ConstantScore(\_field\_names:link) ConstantScore(\_field\_names:ISBN))~1) +ConstantScore(book\_id: \u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 book\_id: \u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0002))) #ConstantScore(\_type:chapter)"}]}

---

<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: [April 18, 2016, 9:14am UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/7 "2016-04-18T09:14:30Z")

</div>

This still looks very similar. The main difference is that `new` puts filters on the top level while `old` puts them in a nested filter clause. Something that is odd is that the `new` query uses the `notavailable` field while the `old` one uses `not_available`. Could it be a copy-pasting issue?

I'm not sure there is anything we can fix here. I suspect that for some reason the `old` query is more friendly to the JVM.

---

<div class="post-metadata">

### Author: ![eprst](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@eprst](https://discuss.elastic.co/u/eprst)
#### Post date: [April 18, 2016, 9:44am UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/8 "2016-04-18T09:44:08Z")

</div>

yes it is copy-pasting issue, and as i wrote before when wrap all filter clauses in another bool. must, it is working faster. should i try to construct new query as similar to old one as possible? Or should i use old?

---

<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: [April 18, 2016, 1:26pm UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/9 "2016-04-18T13:26:43Z")

</div>

If this works consistently better for you, then you can wrap filter clauses in a nested boolean query like the old one did.

---

<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:58pm UTC](https://discuss.elastic.co/t/bool-query-with-filter-is-slower-than-filtered-query/47152/10 "2017-07-05T22:58:31Z")

</div>


