How to get Elasticsearch terms aggregation to filter multi value fields by the same method as an aggs filter?

I'm looking to create a query to give me all the top widgets in the widget field of my document to work with an autocomplete search. It's a multi value field with a keyword tokenizer and an autocomplete tokenizer.

The issue I'm having getting the buckets to filter in the same way the aggs filter does.

Some example data, multiple docs containing values such as

widgets['Thingy', 'Whatsit', 'Thingyamybob', 'Big Thingy']

my-index/my-type/_search
{
  "aggs": {
    "filterAggs": {
      "filter": {
        "match": {
          "widgets": {
            "query": "t"  <- will filter all the docs that match the autocomplete filter.
          }
        }
      }
    },
    "aggs": {
      "bucketAggs": {
        "terms": {
          "field": "widgets.keyword", <- Will bring back all the values in the matched docs, including the ones that didn't match, e.g. 'Whatsit'
          "include": "t.*",  <- This is the closest I've got but it's using regex and not the autocomplete filter.
          "size": 7
        }
      }
    }       
  },
  "size": 0
}

Without the "include": "t.*" I get all the values including the non matching 'Whatsit'. "include": "t.*" is using regex which is not the same method as the match query and will give different results, it's case sensitive, doesn't have ASCII folding etc.

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