# How to Filter nested aggregations (busket results)

**URL:** <https://discuss.elastic.co/t/how-to-filter-nested-aggregations-busket-results/243190>\
**Category:** Elasticsearch\
**Created:** [July 30, 2020, 9:12am UTC](https://discuss.elastic.co/t/how-to-filter-nested-aggregations-busket-results/243190 "2020-07-30T09:12:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aakhiezer](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@aakhiezer](https://discuss.elastic.co/u/aakhiezer)\
**Post date:** [July 30, 2020, 9:12am UTC](https://discuss.elastic.co/t/how-to-filter-nested-aggregations-busket-results/243190/1 "2020-07-30T09:12:30Z")

</div>

Hi all,

I'm trying to filter an aggregations.  
I created aggregation for nested fields(pipe of id|value) of Badge field. It works good and return me buskets with pipe and doc\_count. But I want to filter them to return me only ones that contain word "future". Is it possible?

Index (Badge is nested, value is keyword):  
{"id":1, "Badge":[{"id": 5, "value":"analysis of economics"}, {"id": 4, "value":" **future** of science"}]}  
{"id":2, "Badge":[{"id": 3, "value":" **future** of investments"}, {"id": 2, "value":"solar energy"}]}  
{"id":3, "Badge":[{"id": 3, "value":" **future** of investments"}, {"id": 4, "value":" **future** of science"}]}

Query:  
{  
"size": 0,  
"aggs": {  
"Badge\_agg" : {  
"nested": {"path": "Badge"},  
"aggs": {  
"Badge\_count": {  
"terms": {  
"script": { "lang": "painless", "source": "doc['Badge.id'].value + '|' + doc['Badge.value'].value" }  
}  
}  
}  
}  
}  
}

Result (baskets)

"buckets": [  
{  
"key": "3| **future** of investments",  
"doc\_count": 2  
},  
{  
"key": "4| **future** of science",  
"doc\_count": 2  
},  
{  
"key": "2|solar energy",  
"doc\_count": 1  
},  
{  
"key": "5|analysis of economics",  
"doc\_count": 1  
}  
]

**Desirable result:**  
"buckets": [  
{  
"key": "3| **future** of investments",  
"doc\_count": 2  
},  
{  
"key": "4| **future** of science",  
"doc\_count": 2  
}  
]

Many thanks

---

<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 3, 2020, 10:40am UTC](https://discuss.elastic.co/t/how-to-filter-nested-aggregations-busket-results/243190/2 "2020-08-03T10:40:51Z")

</div>

Hey,

please take your time to properly format the snippets, this will make it much more readable.

How about this?

```auto
PUT test
{
  "mappings": {
    "properties": {
      "Badge": { "type": "nested"}
    }
  }
}

PUT test/_doc/1
{"id":1, "Badge":[{"id": 5, "value":"analysis of economics"}, {"id": 4, "value":"future of science"}]}
PUT test/_doc/2
{"id":2, "Badge":[{"id": 3, "value":"future of investments"}, {"id": 2, "value":"solar energy"}]}
PUT test/_doc/3
{"id":3, "Badge":[{"id": 3, "value":"future of investments"}, {"id": 4, "value":"future of science"}]}

GET test/_search
{
  "size": 0,
  "aggs": {
    "science_only": {
      "nested": {
        "path": "Badge"
      },
      "aggs": {
        "by_id": {
          "terms": {
            "field": "Badge.id",
            "size": 10
          }
        }
      }
    }
  }
}

GET test/_search
{
  "size": 0,
  "aggs": {
    "science_only": {
      "nested": {
        "path": "Badge"
      },
      "aggs": {
        "by_future": {
          "filter": {
            "match": {
              "Badge.value": "future"
            }
          },
          "aggs": {
            "by_id": {
              "terms": {
                "field": "Badge.id",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

```

--Alex

---

<div class="post-metadata">

**Author:** ![aakhiezer](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@aakhiezer](https://discuss.elastic.co/u/aakhiezer)\
**Post date:** [August 4, 2020, 6:23am UTC](https://discuss.elastic.co/t/how-to-filter-nested-aggregations-busket-results/243190/3 "2020-08-04T06:23:22Z")

</div>

Thank you so much!

---

<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 1, 2020, 6:23am UTC](https://discuss.elastic.co/t/how-to-filter-nested-aggregations-busket-results/243190/4 "2020-09-01T06:23:25Z")

</div>

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