# Filter on Aggregation Results

**URL:** <https://discuss.elastic.co/t/filter-on-aggregation-results/125752>\
**Category:** Elasticsearch\
**Created:** [March 27, 2018, 12:39pm UTC](https://discuss.elastic.co/t/filter-on-aggregation-results/125752 "2018-03-27T12:39:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![abcxyz](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@abcxyz](https://discuss.elastic.co/u/abcxyz)\
**Post date:** [March 27, 2018, 12:39pm UTC](https://discuss.elastic.co/t/filter-on-aggregation-results/125752/1 "2018-03-27T12:39:55Z")

</div>

Hello World,

I am currently facing the following challenge:

I want to :  
1- Group data by center and sum over value1 (resulting in sum\_value1)  
2- Given ALL OF the output of step1 and some min and max values, filter sum\_value1 to be in range of [min,max]  
3- Sort the results of the filtering (step2) and take the top X results (desc or asc)

```
Document schema:
    {
    	center: keyword
    	value1: integer
    }

```

I have currently came up with the following query:

```
curl -XGET 'indexName/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "center_terms" : {
            "terms" : {
                "field" : "center" ,
                "size": 1000            
                },
            "aggs": {
                "sum_value1": {
                    "sum": {
                        "field": "value1"
                    }
                },
                "sum_bucket_selector1": {
                    "bucket_selector": {
                        "buckets_path": {
                            "sumValue1": "sum_value1"
                            },
                        "script": "params.sumValue1 > 200"
                    }
                },
                "sum_bucket_selector2": {
                    "bucket_selector": {
                        "buckets_path": {
                            "sumValue1": "sumValue1"
                            },
                        "script": "params.sumValue1 < 1000"
                    }
                }
            }
        }
    }
}'

```

The problem that I am facing is that the filtering is **only** performed on the "size" buckets (in this case only 1000 buckets) and that misses a lot of "interesting" buckets. In particular, there are more buckets than any value of the size parameter.

In other words, what I really want is: to filter on **ALL** the buckets, then sort and return the top X results.

Can someone please help?

Thanks a lot!

---

<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 24, 2018, 12:41pm UTC](https://discuss.elastic.co/t/filter-on-aggregation-results/125752/2 "2018-04-24T12:41:01Z")

</div>

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