# How to optimize filters using better order and combinations (bool vs and/or/not)?

**URL:** <https://discuss.elastic.co/t/how-to-optimize-filters-using-better-order-and-combinations-bool-vs-and-or-not/28542>\
**Category:** Elasticsearch\
**Created:** [September 2, 2015, 4:25pm UTC](https://discuss.elastic.co/t/how-to-optimize-filters-using-better-order-and-combinations-bool-vs-and-or-not/28542 "2015-09-02T16:25:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laurent\_T](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/laurent_t/32/1133_2.png) [@Laurent\_T](https://discuss.elastic.co/u/Laurent_T)\
**Post date:** [September 2, 2015, 4:25pm UTC](https://discuss.elastic.co/t/how-to-optimize-filters-using-better-order-and-combinations-bool-vs-and-or-not/28542/1 "2015-09-02T16:25:43Z")

</div>

Hi,

I'm currently working on optimizing a filtered query. It's basically a `match_all` query with over 40 filters.  
I use mainly Bool Filters that encapsulate Terms Filters, Missing Filters, Not Filters (applied to Terms Filters) and some Range Filters and Geo\* Filters (namely Geohash Cell Filter and GeoShape Filter).

I found the article [All About Elasticsearch Filter BitSets](https://www.elastic.co/blog/all-about-elasticsearch-filter-bitsets) that talks about how one should be using Boolean Filters vs AND/OR/NOT filters but it's marked as being outdated.  
I've also been reading about [Filter Order](https://www.elastic.co/guide/en/elasticsearch/guide/current/_filter_order.html)

I realized my 40 filters weren't organized at all and I want to optimize the order and the use of bitsets and cache.

I'm quite sure I understood how order impacts everything but concerning using Bool vs AND/OR/NOT, is that article still accurate ? How should I combine my various filters ?

I was thinking of starting from the top with a global AND filter and the put a global boolean filter in it followed by my range and geo filters. But what if i need to combine range filters deeply inside boolean filters ? Should i just do `bool.must(and(range,range),terms,terms)` ?

Also I do alot of those: `bool.should(not(terms),missing)`. Should I be using a `mustNot` bool filter instead of a `notFilter` inside my should ?

One last thing: the outdated link i referenced above gives a final example that has a range query inside the bool query between two terms query. Isn't that contradictory to what is explained in the article ?

Thanks for your advices.

Cheers

Note: I'm currently using ES 1.5 but we'll be moving to the latest version soon.

---

<div class="post-metadata">

**Author:** ![Laurent\_T](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/laurent_t/32/1133_2.png) [@Laurent\_T](https://discuss.elastic.co/u/Laurent_T)\
**Post date:** [September 7, 2015, 10:05am UTC](https://discuss.elastic.co/t/how-to-optimize-filters-using-better-order-and-combinations-bool-vs-and-or-not/28542/2 "2015-09-07T10:05:58Z")

</div>

I've done the reordering of my filters but i still don't know how I should manage my range and geo filters vs my boolean filters. Should i be using AND/OR/NOT ? Should i replace my NOT filters by boolean.mustNot filters ?

Thanks

---

<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:51pm UTC](https://discuss.elastic.co/t/how-to-optimize-filters-using-better-order-and-combinations-bool-vs-and-or-not/28542/3 "2017-07-05T23:51:48Z")

</div>


