[[search-aggregations-bucket-filter-aggregation]]
=== Filter

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 }
        }
    }
}
--------------------------------------------------