summaryrefslogtreecommitdiff
path: root/docs/reference/search/aggregations/bucket/filter-aggregation.asciidoc
blob: cc2e104354aaa775a15db6319bdbcf8783d4b32d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[[search-aggregations-bucket-filter-aggregation]]
=== Filter Aggregation

Defines a single bucket of all the documents in the current document set context that match a specified filter. Often this will be used to narrow down the current aggregation context to a specific set of documents.

Example:

[source,js]
--------------------------------------------------
{
    "aggs" : {
        "in_stock_products" : {
            "filter" : { "range" : { "stock" : { "gt" : 0 } } },
            "aggs" : {
                "avg_price" : { "avg" : { "field" : "price" } }
            }
        }
    }
}
--------------------------------------------------

In the above example, we calculate the average price of all the products that are currently in-stock.

Response:

[source,js]
--------------------------------------------------
{
    ...

    "aggs" : {
        "in_stock_products" : {
            "doc_count" : 100,
            "avg_price" : { "value" : 56.3 }
        }
    }
}
--------------------------------------------------