# How to aggregate a term before it's used to filter results

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-a-term-before-its-used-to-filter-results/116081>\
**Category:** Elasticsearch\
**Created:** [January 18, 2018, 3:03pm UTC](https://discuss.elastic.co/t/how-to-aggregate-a-term-before-its-used-to-filter-results/116081 "2018-01-18T15:03:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guy\_Bowden](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@Guy\_Bowden](https://discuss.elastic.co/u/Guy_Bowden)\
**Post date:** [January 18, 2018, 3:03pm UTC](https://discuss.elastic.co/t/how-to-aggregate-a-term-before-its-used-to-filter-results/116081/1 "2018-01-18T15:03:16Z")

</div>

I have a field containing simple tags for example. I'd like to be able to use those tags to perform a faceted search but have the tag counters showing the counts of matches before the tag is used as a filter.

e.g.

```
Foo (20)
*Bar (25)* <--- selected tag
Bof (23) <--- if I select this tag 'Bar' is deselected and I get 23 results (which may or may not have documents tagged `Bar`
Bif (19) 

```

At the moment I have a search that looks like this:

```
{
   "query":{
      "bool":{
         "filter":[
            {
               "term":{
                  "tag":"Bar"
               }
            }
         ]
      }
   },
   "aggs":{
      "by_tag":{
         "terms":{
            "field":"tag",
            "size":0
         }
      }
   }
}

```

This will give me counts for all document tags where the document is tagged Bar already

I could perform multiple searches to get the aggregations, but wondered if there was a simpler way?

---

<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:** [January 19, 2018, 10:43am UTC](https://discuss.elastic.co/t/how-to-aggregate-a-term-before-its-used-to-filter-results/116081/2 "2018-01-19T10:43:42Z")

</div>

Check out the[post\_filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html) expression to apply additional filters to your search results but which don't filter any aggregations.

---

<div class="post-metadata">

**Author:** ![Guy\_Bowden](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@Guy\_Bowden](https://discuss.elastic.co/u/Guy_Bowden)\
**Post date:** [January 19, 2018, 11:05am UTC](https://discuss.elastic.co/t/how-to-aggregate-a-term-before-its-used-to-filter-results/116081/3 "2018-01-19T11:05:17Z")

</div>

Thanks Mark - a little more investigation has shown me the 'global' keyword and filter on aggregations. I have implemented this by copying the overall search filter and removing the one I'm interested in for each aggregation.

i couldn't see how to use post\_filter aside from a simple use-case where I didn't want any filters applied to any of the aggregations, whereas I need to essentially have all my filters applied _except_ for the filter applied on my aggregation field.

---

<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:** [February 16, 2018, 11:05am UTC](https://discuss.elastic.co/t/how-to-aggregate-a-term-before-its-used-to-filter-results/116081/4 "2018-02-16T11:05:50Z")

</div>

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