Skip to content

Filter Aggregation

Edit page

Defines a single-bucket of all the documents in the current document set context that match a specified filter.

Several keys under _term are ORed: a document falls in the bucket when it matches any one of them, and an array value matches any of its values. The match is on the field’s stored value, not on its index terms (a keyword field is compared with its case).

The following snippet captures the structure of filter aggregations:

"<aggregation_name>": {
"_filter": {
"_term": {
( "<key>": <value>, )*
}
},
...
}

Also supports all other functionality as explained in Bucket Aggregations.

Often this will be used to narrow down the current aggregation context to a specific set of documents containing certain terms.

Assuming the data consists of documents representing bank accounts, as shown in the sample dataset of Data Exploration section:

SEARCH /bank/
{
"_query": "*",
"_limit": 0,
"_check_at_least": 1000,
"_aggs": {
"strawberry_lovers": {
"_filter": {
"_term": {
"favoriteFruit": "strawberry"
}
},
"_aggs": {
"avg_balance": {
"_avg": {
"_field": "balance"
}
}
}
}
}
}

In the above example, we calculate the average balance of all the bank accounts with holders who fancy strawberries.

Response:

{
"aggregations": {
"_doc_count": 1000,
"strawberry_lovers": {
"_doc_count": 76,
"avg_balance": {
"_avg": 2581.807236842105
}
}
}, ...
}