# Performance of outside Query vs filter inside aggregation while aggregating

**URL:** <https://discuss.elastic.co/t/performance-of-outside-query-vs-filter-inside-aggregation-while-aggregating/266726>\
**Category:** Elasticsearch\
**Created:** [March 9, 2021, 5:03pm UTC](https://discuss.elastic.co/t/performance-of-outside-query-vs-filter-inside-aggregation-while-aggregating/266726 "2021-03-09T17:03:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![the\_nemo](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@the\_nemo](https://discuss.elastic.co/u/the_nemo)\
**Post date:** [March 9, 2021, 5:03pm UTC](https://discuss.elastic.co/t/performance-of-outside-query-vs-filter-inside-aggregation-while-aggregating/266726/1 "2021-03-09T17:03:35Z")

</div>

Is there any performance difference between the below two quires assuming "type" is a keyword?

```auto
    {
      "size": 0,
      "aggs": {
        "t_shirts": {
          "filter": {
            "term": {
              "type": "t-shirt"
            }
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }

```

```auto
    {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "type": {
                  "value": "t-shirt"
                }
              }
            }
          ]
        }
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }

```

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [March 10, 2021, 3:39am UTC](https://discuss.elastic.co/t/performance-of-outside-query-vs-filter-inside-aggregation-while-aggregating/266726/2 "2021-03-10T03:39:40Z")

</div>

There are dozen of discussions about this topic.  
A couple of examples

> **[Boolean Operations and Filters in the Bool Query in Elasticsearch](https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query)**
>
> Some query types deprecated in 2.x will be removed in 5.0. Many of them are replaced by functionality of the bool query, so here’s a quick guide on bool query.

> <https://stackoverflow.com/questions/43349044/what-is-the-difference-between-must-and-filter-in-query-dsl-in-elasticsearch>

---

<div class="post-metadata">

**Author:** ![the\_nemo](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@the\_nemo](https://discuss.elastic.co/u/the_nemo)\
**Post date:** [March 10, 2021, 4:10am UTC](https://discuss.elastic.co/t/performance-of-outside-query-vs-filter-inside-aggregation-while-aggregating/266726/3 "2021-03-10T04:10:05Z")

</div>

Thanks, @Aclerk. I am more concerned about using the same query outside and inside an aggregation wrt performance.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [March 10, 2021, 8:24am UTC](https://discuss.elastic.co/t/performance-of-outside-query-vs-filter-inside-aggregation-while-aggregating/266726/4 "2021-03-10T08:24:16Z")

</div>

I’m going to go with the query.  
It reads a set of Lucene doc IDs from disk for a given term and then feeds them into the tree of aggregation collectors for consideration.

The query-less version iterates over all (non-deleted) doc IDs in the range 0 to number-of-docs-in-index and feeds those into the agg tree. The first agg in your tree then filters these to see if they match a set of doc IDs from disk for a given term. More iterations (unless there’s some special optimisation for an agg tree with a single-filter at the root which I’m unaware of).

---

<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:** [April 7, 2021, 8:24am UTC](https://discuss.elastic.co/t/performance-of-outside-query-vs-filter-inside-aggregation-while-aggregating/266726/5 "2021-04-07T08:24:25Z")

</div>

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