WARNING: The 2.x versions of Elasticsearch have passed their EOL dates. If you are running a 2.x version, we strongly advise you to upgrade.
This documentation is no longer maintained and may be removed. For the latest information, see the current Elasticsearch documentation.
Filtering Queries
edit
IMPORTANT: This documentation is no longer updated. Refer to Elastic's version policy and the latest documentation.
Filtering Queries
editIf 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.