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

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.

You can use the include parameter to filter values, using a regular expression:

  "size": 0,
  "aggs": {
    "my_agg": {
      "terms": {
        "field": "my_field",
        "include" : "LOC.*",
        "size": 10
1 Like

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.

Take a look at the documentation for the composite aggregation. It can do exactly as you want.

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