Filtering Queriesedit

If we want to find all cars over $10,000 and also calculate the average price for those cars, we can use a constant_score query and its filter clause:

GET /cars/transactions/_search
{
    "size" : 0,
    "query" : {
        "constant_score": {
            "filter": {
                "range": {
                    "price": {
                        "gte": 10000
                    }
                }
            }
        }
    },
    "aggs" : {
        "single_avg_price": {
            "avg" : { "field" : "price" }
        }
    }
}

Fundamentally, using a non-scoring query is no different from using a match query, as we discussed in the previous chapter. The query returns a certain subset of documents, and the aggregation operates on those documents. It just happens to omit scoring and may proactively cache bitsets, etc.