# How to filter top level aggregation bucket using lower level aggregation results

**URL:** <https://discuss.elastic.co/t/how-to-filter-top-level-aggregation-bucket-using-lower-level-aggregation-results/39527>\
**Category:** Elasticsearch\
**Created:** [January 19, 2016, 10:11am UTC](https://discuss.elastic.co/t/how-to-filter-top-level-aggregation-bucket-using-lower-level-aggregation-results/39527 "2016-01-19T10:11:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GabKlein](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabklein/32/7237_2.png) [@GabKlein](https://discuss.elastic.co/u/GabKlein)\
**Post date:** [January 19, 2016, 10:11am UTC](https://discuss.elastic.co/t/how-to-filter-top-level-aggregation-bucket-using-lower-level-aggregation-results/39527/1 "2016-01-19T10:11:32Z")

</div>

Hi guys,

I have a question on how can I use aggregation filter from a top level aggregation using lower level aggregation result field ?

My data:

> ```
> [ 
> { id: 1, product_id: 1, price: 6 },
> { id: 2, product_id: 2, price: 5 },
> { id: 3, product_id: 1, price: 4 },
> { id: 4, product_id: 2, price: 8 },
> { id: 5, product_id: 3, price: 7 }
> ]
> 
> ```

Query I would like to run:

```
{
  "size": 0,
  "aggs": {
    "products": {
      "filter": {        
        "range": {
          "price_sum": { //Using lower level aggregation filed
            "gte" : 10,
            "lte" : 20
          }
        }
      },
      "aggs": {
        "products_ids": {
          "terms": {
            "field": "product_id",
            "size": 0
          },
          "aggs": {
            "price_sum": {
              "sum": {
                "script": "doc['price'].value.toInteger()"
              }
            }
          }
        }
      }
    }
  }
}

```

Result expected:

```
product_ids: [
  { key: 1, price_sum: 10 },
  { key: 2, price_sum: 13 }
]

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 20, 2016, 10:03pm UTC](https://discuss.elastic.co/t/how-to-filter-top-level-aggregation-bucket-using-lower-level-aggregation-results/39527/2 "2016-01-20T22:03:25Z")

</div>

> [@GabKlein](#):
>
> can I use aggregation filter from a top level aggregation using lower level aggregation result field

You can't, you need to figure that out first.

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [January 21, 2016, 10:11am UTC](https://discuss.elastic.co/t/how-to-filter-top-level-aggregation-bucket-using-lower-level-aggregation-results/39527/3 "2016-01-21T10:11:04Z")

</div>

You could get to what you are after by using the [`bucket_selector` pipeline aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-aggregations-pipeline-bucket-selector-aggregation.html) with something like the below.

This comes with the usual caveats of using scripting which you can read bout in the documentation[here](https://www.elastic.co/guide/en/elasticsearch/reference/2.1/modules-scripting.html)

Also, it is not recommended to use `"size": 0` with the terms aggregation since for high cardinality fields (like a field for product id) it can cause memory issues.

```auto
{
  "size": 0,
  "aggs": {
    "products_ids": {
      "terms": {
        "field": "product_id",
        "size": 0
      },
      "aggs": {
        "price_sum": {
          "sum": {
            "script": "doc['price'].value.toInteger()"
          }
        },
        "bucket_filter": {
          "bucket_selector": {
            "buckets_path": {
              "price_sum": "price_sum"
            },
            "script": "price_sum <= 20 && price_sum >= 10"
          }
        }
      }
    }
  }
}

```

The output for the above aggregation using your example data is:

```auto
"products_ids": {
  "doc_count_error_upper_bound": 0,
  "sum_other_doc_count": 0,
  "buckets": [
    {
      "key": 1,
      "doc_count": 2,
      "price_sum": {
        "value": 10
      }
    },
    {
      "key": 2,
      "doc_count": 2,
      "price_sum": {
        "value": 13
      }
    }
  ]
}

```

---

<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:22pm UTC](https://discuss.elastic.co/t/how-to-filter-top-level-aggregation-bucket-using-lower-level-aggregation-results/39527/4 "2017-07-05T23:22:37Z")

</div>


