# Filter after aggregation

**URL:** <https://discuss.elastic.co/t/filter-after-aggregation/195758>\
**Category:** Elasticsearch\
**Created:** [August 19, 2019, 2:31pm UTC](https://discuss.elastic.co/t/filter-after-aggregation/195758 "2019-08-19T14:31:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ex56ri7](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@ex56ri7](https://discuss.elastic.co/u/ex56ri7)\
**Post date:** [August 19, 2019, 2:31pm UTC](https://discuss.elastic.co/t/filter-after-aggregation/195758/1 "2019-08-19T14:31:39Z")

</div>

I have documents in the form of:

> {  
> "result": "failure",  
> "key": "A"  
> }  
> {  
> "result": "failure",  
> "key": "A"  
> }  
> {  
> "result": "failure",  
> "key": "A"  
> }  
> {  
> "result": "failure",  
> "key": "B"  
> }

I need to get the value of the 'key' field if the latest 3 documents have the result field set to 'failure'.  
In the example above I would get the 'A' but not 'B'.

I tried terms aggregation by key and get the 3 latest documents with top\_hits. But I cannot add anything to filter the results I get from these aggregations because it does not seem to be possible to nest any aggregations inside of a top\_hit aggregation. I also tried a scripted\_field that is 1 or 0 depending on the result field. But I also cannot build a sum depending on the scripted\_field value.

> {  
> "aggs": {  
> "termsByKey": {  
> "terms": {  
> "field": "key.keyword"  
> },  
> "aggs": {  
> "topHitsByKey": {  
> "top\_hits": {  
> "\_source": {  
> "includes": [  
> "key",  
> "result",  
> "@timestamp"  
> ]  
> },  
> "size": 3  
> }  
> }  
> }  
> }  
> }  
> }

I do not know if this is actually the right approach to solve my initial problem.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 19, 2019, 3:49pm UTC](https://discuss.elastic.co/t/filter-after-aggregation/195758/2 "2019-08-19T15:49:45Z")

</div>

Hey,

limiting the number of documents to three is a good start, but you should also sort by the timestamp of those documents to really get the latest ones.

Can you explain why a `terms` aggregation is not enough, as you would know if the last three documents either contain only `A` or `B` as well, by counting the number of buckets or just using the `cardinality` aggregation within the terms agg, by counting the number of distinct elements.

Can you explain why you need the originial JSON via top\_hits? Maybe that request can be shortened a bit...

---

<div class="post-metadata">

**Author:** ![ex56ri7](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@ex56ri7](https://discuss.elastic.co/u/ex56ri7)\
**Post date:** [August 20, 2019, 7:38am UTC](https://discuss.elastic.co/t/filter-after-aggregation/195758/3 "2019-08-20T07:38:33Z")

</div>

I do not need top\_hits but it was the only way I could think of to reduce the number of documents to three. After applying my aggregation I get one bucket per key value that contains three elements. The problem now is that I have to check if those three elements have the desired value in the result field. Therefor the terms aggregation on the key field is not enough.

I cannot add any additional aggregation to work on the output of my topHitsByKey aggregation.

---

<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:** [September 17, 2019, 7:38am UTC](https://discuss.elastic.co/t/filter-after-aggregation/195758/4 "2019-09-17T07:38:34Z")

</div>

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