ElasticSearch Filtering aggregates from an array

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?

+6
source share
1 answer

Try this, it will filter the values โ€‹โ€‹created by the buckets themselves for:

 { "size": 0, "aggs": { "colors": { "filter": { "prefix": { "colors": "b" } }, "aggs": { "top-colors": { "terms": { "field": "colors", "include": { "pattern": "b.*" } } } } } } } 
+7
source

Source: https://habr.com/ru/post/980616/


All Articles