Given a set of documents that have a given value in one field, I want to know how many documents there are that have each value for a second field.
I've tried to do that with a terms aggregation with the following query:
{
"size": 0,
"filter": {
"term": {
"field1": "value"
}
},
"aggregations": {
"field2" : {
"terms" : { "field" : "field2" }
}
}
}
but the returned counts show the number of documents with each value for the second field in the whole index, and not restricted to those documents with a given value for the first field.
What am I doing wrong?
With Aggregated Filters, you are now able to filter on an aggregated column value. Aggregated Filters can be applied to string, amount, or date columns and are available to be used everywhere Filters are applied: Inside the Analyzer. As a Quick Filter.
In Elasticsearch, an aggregation is a collection or the gathering of related things together. The aggregation framework collects data based on the documents that match a search request which helps in building summaries of the data.
Terms aggregationedit. A multi-bucket value source based aggregation where buckets are dynamically built - one per unique value. The field can be Keyword, Numeric, ip , boolean , or binary .
Have you tried using filtered query?
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"term": {
"field1": "value"
}
}
}
},
"aggregations": {
"field2": {
"terms": { "field": "field2" }
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With