# Performance impact when encapsulating two bool query

**URL:** <https://discuss.elastic.co/t/performance-impact-when-encapsulating-two-bool-query/255239>\
**Category:** Elasticsearch\
**Created:** [November 12, 2020, 4:22pm UTC](https://discuss.elastic.co/t/performance-impact-when-encapsulating-two-bool-query/255239 "2020-11-12T16:22:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scylanth](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Scylanth](https://discuss.elastic.co/u/Scylanth)\
**Post date:** [November 12, 2020, 4:22pm UTC](https://discuss.elastic.co/t/performance-impact-when-encapsulating-two-bool-query/255239/1 "2020-11-12T16:22:26Z")

</div>

Hello,

I am doing a search on an index with a filter on a field which is a text with a field analyzer. In my query I want to have all the records which contains exactly both of the fields I have specified in my query, or none of those two parameters (based of some inputs of the user).

I have found two ways of doing the first query :

- With two terms query in the must query

```auto
    {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            { "terms":
              {
                 "features": ["feature1"],
                 "boost": 1.0
              }
            },
            { "terms":
              {
                "features": ["feature2"],
                "boost": 1.0
              }
            }
          ]
        }
      }
    }

```

- With two terms query encapsulated in another bool query, so two encapsulated bool queries :

```auto
    {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "must": [
                  { "terms":
                    {
                      "features": ["feature1"],
                      "boost": 1.0
                    }
                  },
                  { "terms":
                    {
                      "features": ["feature2"],
                      "boost": 1.0
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }

```

The second bool query is redundant in that case, but it actually helps me in my code when getting all the records that does not contain both those input parameters. I found out that without the bool encapsulator, the must\_not query on its own behaves as an OR operator in SQL, but encapsulating with a bool query with a must, makes it behaving as an AND operator (which is what I need).

Regardless of that, I would like to know if encapsulating two bool queries this way has an impact in terms of performance for the query. In all the tests I have done, the second query run is always faster (whether it contained the encapsulated bool query or not), but I could not find out if one of them was actually faster than the other one.

Thanks in advance!

---

<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:** [December 10, 2020, 4:22pm UTC](https://discuss.elastic.co/t/performance-impact-when-encapsulating-two-bool-query/255239/2 "2020-12-10T16:22:28Z")

</div>

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