# Do aggregations support filters outside query context?

**URL:** <https://discuss.elastic.co/t/do-aggregations-support-filters-outside-query-context/39593>\
**Category:** Elasticsearch\
**Created:** [January 19, 2016, 7:48pm UTC](https://discuss.elastic.co/t/do-aggregations-support-filters-outside-query-context/39593 "2016-01-19T19:48:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benedictfischer09](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@benedictfischer09](https://discuss.elastic.co/u/benedictfischer09)\
**Post date:** [January 19, 2016, 7:48pm UTC](https://discuss.elastic.co/t/do-aggregations-support-filters-outside-query-context/39593/1 "2016-01-19T19:48:26Z")

</div>

If I run the two queries below they both come back with the same hits, the difference is that the buckets for the first query use every doc in the index while the second query only uses the docs in the search results

```
GET /development_users/user/_search
{
    "filter": {
        "geo_bounding_box" :{
          "user_lat_lon" :{
            "top_left" :{
              "lat" :"26.429013452407396",
              "lon" :"-90.412109375"
            },
            "bottom_right" :{
              "lat" :"25.05593126519445",
              "lon" :"-70.33203125"
            }
          }
        }
    },
    "aggs": {
        "docs_grid" :{
          "geohash_grid" :{
            "field" :"user_lat_lon",
            "precision" :3,
            "size" :500
          }
        }
    }
}

GET /development_users/user/_search
{
    "query": {
        "filtered": {
            "filter": {
                "geo_bounding_box" :{
                  "user_lat_lon" :{
                    "top_left" :{
                      "lat" :"26.429013452407396",
                      "lon" :"-90.412109375"
                    },
                    "bottom_right" :{
                      "lat" :"25.05593126519445",
                      "lon" :"-70.33203125"
                    }
                  }
                }
            }
        }
    },
    "aggs": {
        "docs_grid" :{
          "geohash_grid" :{
            "field" :"user_lat_lon",
            "precision" :3,
            "size" :500
          }
        }
    }
}

```

The only difference between these queries is that the first one uses a filter outside a query filtered context and the second one uses it in the query filtered context. To me the documentation seems to say you can use either

> An aggregation can be seen as a unit-of-work that builds analytic information over a set of documents. The context of the execution defines what this document set is (e.g. a top-level aggregation executes within the context of the executed query/filters of the search request).

I haven't been able to find any examples of doing an aggregation with just a filter seems most people use a query filtered context e.g.

> [@Geo bounds aggregation as a sub aggregator?](https://discuss.elastic.co/t/geo-bounds-aggregation-as-a-sub-aggregator/18856/4):
>
> I think the problem is that you have an extra aggregations object surrounding the geo\_bounds aggregation. Try the following: curl -XGET "[http://localhost:9200/postings/\_search](http://localhost:9200/postings/_search)" -d' { "query": { "filtered": { "filter": { "geo\_bounding\_box": { "point": { "bottom\_right": [104.45856200712711, 52.208730692828844], "top\_left": [104.19077025908017, 52.34035674074375] } } }, "query": { "match\_all": {} } } }, "aggregations": { "cells": { "geohash\_grid": { "field": "point"…

Can it be done? If so how?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 19, 2016, 8:50pm UTC](https://discuss.elastic.co/t/do-aggregations-support-filters-outside-query-context/39593/2 "2016-01-19T20:50:54Z")

</div>

The former treats the filter as a post filter:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html)  
[https://www.elastic.co/guide/en/elasticsearch/guide/current/\_post\_filter.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/_post_filter.html)

I thought Elasticsearch moved to having post\_filter to be explicitly named.  
What version of elasticsearch are you using?

---

<div class="post-metadata">

**Author:** ![benedictfischer09](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@benedictfischer09](https://discuss.elastic.co/u/benedictfischer09)\
**Post date:** [January 19, 2016, 9:57pm UTC](https://discuss.elastic.co/t/do-aggregations-support-filters-outside-query-context/39593/3 "2016-01-19T21:57:03Z")

</div>

I'm on 1.7.3

Thanks that explanation definitely makes sense, so I probably have to go with the filtered query or since that's being deprecated maybe use a filter clause on a bool query [https://www.elastic.co/blog/better-query-execution-coming-elasticsearch-2-0](https://www.elastic.co/blog/better-query-execution-coming-elasticsearch-2-0)

---

<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:23pm UTC](https://discuss.elastic.co/t/do-aggregations-support-filters-outside-query-context/39593/4 "2017-07-05T23:23:14Z")

</div>


