I want to do aggregation on the list field in elasticsearch. But while doing same, as this is list field if there is other value along with searched value inside the list then aggregation will also give those in the result. Is there any way to filter this aggregation results to get only matching records. Also want this to work with pagination as well.
Example:
Doc:
{
id: 1,
field: [foo, bar, foobar]
},
{
id: 2,
field: [foo]
},
{
id: 3,
field: [foo, foobar]
}
ES query:
{
"aggFilter": {
"filter": {
"terms": {
"field": [
"foo"
],
"boost": 1
}
},
"aggregations": {
"fieldAggregation0": {
"terms": {
"field": "field",
"size": 1000,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
}
}
Output:
{
"_key" : "foo",
"doc_count" : 3
},
{
"_key" : "foobar",
"doc_count" : 2,
},
{
"_key" : "bar",
"doc_count" : 1
}
Expecting:
{
"_key" : "foo",
"doc_count" : 3
}
Tried with bucket_selector but it gives error that value to be filtered(value at bucket_path) should be numeric and in my case I want to filter _key which is string.
The
termsaggregation has theincludeparameter to filter terms (excludeas well)Documents
Modified query
Response
I use the
termsquery to filter documents for aggregationUse the
bucket_sortaggregation to paginate results