# Elasticsearch- How to make the aggregation to return only the key matched

**URL:** <https://discuss.elastic.co/t/elasticsearch-how-to-make-the-aggregation-to-return-only-the-key-matched/153113>\
**Category:** Elasticsearch\
**Created:** [October 19, 2018, 6:18am UTC](https://discuss.elastic.co/t/elasticsearch-how-to-make-the-aggregation-to-return-only-the-key-matched/153113 "2018-10-19T06:18:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandra1](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Alexandra1](https://discuss.elastic.co/u/Alexandra1)\
**Post date:** [October 19, 2018, 6:18am UTC](https://discuss.elastic.co/t/elasticsearch-how-to-make-the-aggregation-to-return-only-the-key-matched/153113/1 "2018-10-19T06:18:50Z")

</div>

I'm using elasticsearch 6.2.3 and i'm trying to make a filter on the aggregation to return only the keys matched not all of them from the buckets. Or maybe i need to do a script and iterate over the buckets to get only the match? My query is:

```auto
 {
"aggs" : {
    "genres" : {
           "nested" : {
            "path" : "tags"
        },
        "aggs": {
          "d":{
        "terms" : { "field" : "tags.values.value.facet"
        },
        "aggs": {
          "NAME": {
            "filter": {
              "prefix": {
                "tags.values.value.facet": "Variatio"
              }
            }
          }
        }
      }
    }
  }                 
}}

```

And the result looks like that:

```auto
"aggregations": {
"genres": {
  "doc_count": 285391,
  "d": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 44437,
    "buckets": [
      {
        "key": "null",
        "doc_count": 251811,
        "NAME": {
          "doc_count": 0
        }
      },
      {
        "key": "MMAkku",
        "doc_count": 15870,
        "NAME": {
          "doc_count": 0
        }
      },
      {
        "key": "MMBaden",
        "doc_count": 15870,
        "NAME": {
          "doc_count": 0
        }
      },
      {
        "key": "MMEttlingen",
        "doc_count": 15870,
        "NAME": {
          "doc_count": 0
        }
      },
      {
        "key": "MMEurope",
        "doc_count": 15870,
        "NAME": {
          "doc_count": 0
        }
      },
      {
        "key": "MMGermany",
        "doc_count": 15870,
        "NAME": {
          "doc_count": 0
        }
      },
      {
        "key": "MMKarlsruhe",
        "doc_count": 15870,
        "NAME": {
          "doc_count": 0
        }
      }
      }

```

Any suggestions?

Thanks

---

<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:** [November 16, 2018, 6:24am UTC](https://discuss.elastic.co/t/elasticsearch-how-to-make-the-aggregation-to-return-only-the-key-matched/153113/2 "2018-11-16T06:24:51Z")

</div>

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