WARNING: Version 5.0 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Query and filter context
editQuery and filter context
editThe behaviour of a query clause depends on whether it is used in query context or in filter context:
- Query context
-
A query clause used in query context answers the question “How well does this document match this query clause?” Besides deciding whether or not the document matches, the query clause also calculates a
_scorerepresenting how well the document matches, relative to other documents.Query context is in effect whenever a query clause is passed to a
queryparameter, such as thequeryparameter in thesearchAPI. - Filter context
-
In filter context, a query clause answers the question “Does this document match this query clause?” The answer is a simple Yes or No — no scores are calculated. Filter context is mostly used for filtering structured data, e.g.
-
Does this
timestampfall into the range 2015 to 2016? -
Is the
statusfield set to"published"?
Frequently used filters will be cached automatically by Elasticsearch, to speed up performance.
Filter context is in effect whenever a query clause is passed to a
filterparameter, such as thefilterormust_notparameters in theboolquery, thefilterparameter in theconstant_scorequery, or thefilteraggregation. -
Does this
Below is an example of query clauses being used in query and filter context
in the search API. This query will match documents where all of the following
conditions are met:
-
The
titlefield contains the wordsearch. -
The
contentfield contains the wordelasticsearch. -
The
statusfield contains the exact wordpublished. -
The
publish_datefield contains a date from 1 Jan 2015 onwards.
GET /_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
|
The |
|
|
The |
|
|
The |
|
|
The |
Use query clauses in query context for conditions which should affect the score of matching documents (i.e. how well does the document match), and use all other query clauses in filter context.