I am trying to aggregate values โโin an array and also filter the buckets returned by the prefix. Not sure if this is possible, or I'm using the filter bucket incorrectly.
3 documents:
{ "colors":["red","black","blue"] } { "colors":["red","black"] } { "colors":["red"] }
The goal is to get the number of documents having a color starting with the letter B:
{ "size":0, "aggs" : { "colors" : { "filter" : { "prefix" : { "colors" : "b" } }, "aggs" : { "top-colors" : { "terms" : { "field":"colors" } } } } } }
The results that come back, unfortunately, are red. Obviously, because documents with red still match the filter, because they also have blue and / or black.
"aggregations": { "colors": { "doc_count": 2, "top-colors": { "buckets": [ { "key": "black", "doc_count": 2 }, { "key": "red", "doc_count": 2 }, { "key": "blue", "doc_count": 1 } ] } } }
Is there a way to filter only the results of a bucket?
source share