# Ordering of Nested Bool Filters

**URL:** https://discuss.elastic.co/t/ordering-of-nested-bool-filters/27795
**Category:** Elasticsearch
**Created:** [August 20, 2015, 7:53pm UTC](https://discuss.elastic.co/t/ordering-of-nested-bool-filters/27795 "2015-08-20T19:53:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Harlin\_ES](https://avatars.discourse-cdn.com/v4/letter/h/977dab/32.png) [@Harlin\_ES](https://discuss.elastic.co/u/Harlin_ES)
#### Post date: [August 20, 2015, 7:53pm UTC](https://discuss.elastic.co/t/ordering-of-nested-bool-filters/27795/1 "2015-08-20T19:53:20Z")

</div>

I am sending a nested bool filter to my cluster and the order in which each filter executes is very important. From what I understand the filter that comes first should be executed first but that doesn't seem to be what is happening when I execute my query:

```
"query" : {
    "filtered" : {
      "filter" : {
        "bool" : {
          "must" : [ {
            "bool" : {
              "should" : [ {
                "term" : {
                  "category" : 64
                }
              }, {
                "term" : {
                  "category" : 65
                }
              } ]
            }
          }, {
            "term" : {
              "identity" : 25914331
            }
          }, {
            "range" : {
              "timestamp" : {
                "from" : 1440057600000,
                "to" : 1440071999999,
                "include_lower" : true,
                "include_upper" : true
              }
            }
          } ]
        }
      }
    }
  }
}

```

I need the nested bool on the "category" field to execute first, though I am not sure this is what is actually happening. Any insight would be greatly appreciated.

Thank,  
Harlin

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [August 20, 2015, 10:51pm UTC](https://discuss.elastic.co/t/ordering-of-nested-bool-filters/27795/2 "2015-08-20T22:51:46Z")

</div>

Actually, the only time order matters is for the `and`/`or`/`not` family of compound filters. They are order dependent because of their internal execution path... which is also why they are usually sub-optimal for performance.

The `bool` filter operates differently: it internally re-arranges the filters to produce the most efficient execution path. The `bool` filter basically aligns the various filter bitsets and executes the least expensive one first (determined heuristically, usually the sparsest filter). The bitsets then "leapfrog" their iterators to visit the fewest number of documents as possible.

If you're interested in the technical details, Adrien has a good talk about how conjunctions (boolean combinations) work in Lucene here: [https://berlinbuzzwords.de/file/bbuzz-2015-adrien-grand-algorithms-and-data-structures-power-lucene-and-elasticsearch](https://berlinbuzzwords.de/file/bbuzz-2015-adrien-grand-algorithms-and-data-structures-power-lucene-and-elasticsearch)

So, the morale of the story is: don't worry about order 🙂 Lucene will choose the fastest/best order for you

---

<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:54pm UTC](https://discuss.elastic.co/t/ordering-of-nested-bool-filters/27795/3 "2017-07-05T23:54:44Z")

</div>


