# How to filter with pipeline aggregations in all aggregated results

**URL:** <https://discuss.elastic.co/t/how-to-filter-with-pipeline-aggregations-in-all-aggregated-results/288340>\
**Category:** Elasticsearch\
**Created:** [November 3, 2021, 3:12pm UTC](https://discuss.elastic.co/t/how-to-filter-with-pipeline-aggregations-in-all-aggregated-results/288340 "2021-11-03T15:12:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elina](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elina](https://discuss.elastic.co/u/elina)\
**Post date:** [November 3, 2021, 3:12pm UTC](https://discuss.elastic.co/t/how-to-filter-with-pipeline-aggregations-in-all-aggregated-results/288340/1 "2021-11-03T15:12:52Z")

</div>

Hi! I'm working on a project with elasticseach. I'am stuck with the below problem. I can't figure this out no matter what I have tried so far.  
Firstly, I would like to group by "keyword" in buckets using the terms aggregation.  
After that, I would like to calculate some metrics such as "sum" of the field "clicks" using the sum\_aggregation  
After that, I would like to filter the returned results with pipeline aggregation based on min max values selected from the user.  
At the end, I want to sort them by "clicks" with descending order. Also to return only first 100 results to the client.

What I have done so far is the below query.

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date": {
              "gte": "2021-10-01",
              "lte": "2021-10-31"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "group_by_terms": {
      "terms": {
        "field": "keyword.keyword"
      },
      "aggs": {
        "sum_clicks": {
          "sum": {
            "script": {
              "lang": "painless",
              "inline": "doc['clicks'].size()== 0 ? 0 : doc['clicks'].value"
            }
          }
        },
        "sum_clicks_bucket_sort": {
          "bucket_sort": {
            "sort": [
              {
                "sum_clicks": {
                  "order": "desc"
                }
              }
            ],
            "size": 100
          }
        },
        "bucket_filter": {
          "bucket_selector": {
            "buckets_path": {
              "sum_clicks": "sum_clicks"
            },
            "script": "params.sum_clicks < 14 && params.sum_clicks > 0"
          }
        }
      }
    }
  }
}

```

So, although there are many aggregated results with "sum\_clicks" \> 0 and "sum\_clicks" \< 14 they are not returned by the query.  
If I increase the min max then it will rerurn results. Another strange scenario is that if I remove the ordering it will return results. Maybe terms aggregation doens't include all results of all documents in Elasticsearch, that's why they can't be returned. I'am not sure about it. Any help will be appreciated! Thanks in advance!

---

<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:** [December 1, 2021, 3:13pm UTC](https://discuss.elastic.co/t/how-to-filter-with-pipeline-aggregations-in-all-aggregated-results/288340/2 "2021-12-01T15:13:41Z")

</div>

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