# Prefix match phrase filter query on the keys returned from terms aggregation

**URL:** <https://discuss.elastic.co/t/prefix-match-phrase-filter-query-on-the-keys-returned-from-terms-aggregation/177705>\
**Category:** Elasticsearch\
**Created:** [April 20, 2019, 9:53am UTC](https://discuss.elastic.co/t/prefix-match-phrase-filter-query-on-the-keys-returned-from-terms-aggregation/177705 "2019-04-20T09:53:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vamshi\_Sai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vamshi_sai/32/42723_2.png) [@Vamshi\_Sai](https://discuss.elastic.co/u/Vamshi_Sai)\
**Post date:** [April 20, 2019, 9:53am UTC](https://discuss.elastic.co/t/prefix-match-phrase-filter-query-on-the-keys-returned-from-terms-aggregation/177705/1 "2019-04-20T09:53:50Z")

</div>

I have terms query and the result looks something like below

"buckets": [  
{  
"key": "KEY",  
"count": 20  
},  
{  
"key": "LOCK",  
"count": 30  
}  
]

Now the requirement is to filter those buckets whose key starts with a certain prefix, so something similar to match phrase prefix. For example if input to match phrase prefix query is "LOC", then only one bucket should be returned(2nd one). So effectively it's a filter on terms aggregation. Thanks for your thoughts.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [April 21, 2019, 8:39am UTC](https://discuss.elastic.co/t/prefix-match-phrase-filter-query-on-the-keys-returned-from-terms-aggregation/177705/2 "2019-04-21T08:39:48Z")

</div>

You can [use the `include` parameter to filter values](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_3), using a regular expression:

```auto
{
  "size": 0,
  "aggs": {
    "my_agg": {
      "terms": {
        "field": "my_field",
        "include" : "LOC.*",
        "size": 10
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Vamshi\_Sai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vamshi_sai/32/42723_2.png) [@Vamshi\_Sai](https://discuss.elastic.co/u/Vamshi_Sai)\
**Post date:** [April 22, 2019, 10:48pm UTC](https://discuss.elastic.co/t/prefix-match-phrase-filter-query-on-the-keys-returned-from-terms-aggregation/177705/3 "2019-04-22T22:48:55Z")

</div>

Thanks a lot, It helps. Also is there a pagination support over aggregated result. For example if the aggregation gives all the results in some sorted order and I need to get only 50 of them at a time as the user is issuing more requests. This can be done on the backend, but would like to understand if it is supported within Elastic Search ? Thanks.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [April 23, 2019, 6:24am UTC](https://discuss.elastic.co/t/prefix-match-phrase-filter-query-on-the-keys-returned-from-terms-aggregation/177705/4 "2019-04-23T06:24:28Z")

</div>

Take a look at [the documentation for the composite aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html). It can do exactly as you want.

---

<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:** [May 21, 2019, 6:24am UTC](https://discuss.elastic.co/t/prefix-match-phrase-filter-query-on-the-keys-returned-from-terms-aggregation/177705/5 "2019-05-21T06:24:35Z")

</div>

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