# Should i replace the bool query with filter query?

**URL:** <https://discuss.elastic.co/t/should-i-replace-the-bool-query-with-filter-query/70048>\
**Category:** Elasticsearch\
**Created:** [December 27, 2016, 8:45am UTC](https://discuss.elastic.co/t/should-i-replace-the-bool-query-with-filter-query/70048 "2016-12-27T08:45:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 27, 2016, 8:45am UTC](https://discuss.elastic.co/t/should-i-replace-the-bool-query-with-filter-query/70048/1 "2016-12-27T08:45:30Z")

</div>

i'm reading the es document,one chapter says filter will be cached and filter clause not be calculated in [score.in](http://score.in) my business scenario, i use es to search some eligible data then sorting them by date ,so i need't the score ,does query will be faster if i replace my bool query with filter query?

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [December 27, 2016, 9:07am UTC](https://discuss.elastic.co/t/should-i-replace-the-bool-query-with-filter-query/70048/2 "2016-12-27T09:07:55Z")

</div>

In ES 1.7 or lower yes. If you are on ES 2.0 and later you should place  
your query in the filter clause of the `bool` query [1].  
This will make sure that individual query elements wrapped in the `bool`  
query's filter cache are cached. In your case you're sorting by date and  
then ES will never compute score. However if you were sorting by score the  
query elements wrapped in the `bool` query's filter clause will be omitted  
from score computation.

1:  
[https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-bool-query.html#\_scoring\_with\_literal\_bool\_filter\_literal](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-bool-query.html#_scoring_with_literal_bool_filter_literal)

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 27, 2016, 9:23am UTC](https://discuss.elastic.co/t/should-i-replace-the-bool-query-with-filter-query/70048/3 "2016-12-27T09:23:36Z")

</div>

thank you,i got it.  
here is my java query example :  
"  
QueryBuilders  
.boolQuery()  
.filter(QueryBuilders.termQuery("status", 1))  
.filter(QueryBuilders.termQuery("gender", 1))  
"  
does it right?

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [December 27, 2016, 9:27am UTC](https://discuss.elastic.co/t/should-i-replace-the-bool-query-with-filter-query/70048/4 "2016-12-27T09:27:27Z")

</div>

That looks all right.

---

<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 24, 2017, 9:28am UTC](https://discuss.elastic.co/t/should-i-replace-the-bool-query-with-filter-query/70048/5 "2017-01-24T09:28:12Z")

</div>

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