11[[query-filter-context]]
22== Query and filter context
33
4- The behaviour of a query clause depends on whether it is used in _query context_ or
5- in _filter context_:
4+ [float]
5+ [[relevance-scores]]
6+ === Relevance scores
67
7- Query context::
8- +
9- --
10- A query clause used in query context answers the question ``__How well does this
8+ By default, Elasticsearch sorts matching search results by **relevance
9+ score**, which measures how well each document matches a query.
10+
11+ The relevance score is a positive floating point number, returned in the
12+ `_score` meta-field of the <<search-request-body,search>> API. The higher the
13+ `_score`, the more relevant the document. While each query type can calculate
14+ relevance scores differently, score calculation also depends on whether the
15+ query clause is run in a **query** or **filter** context.
16+
17+ [float]
18+ [[query-context]]
19+ === Query context
20+ In the query context, a query clause answers the question ``__How well does this
1121document match this query clause?__'' Besides deciding whether or not the
12- document matches, the query clause also calculates a `_score` representing how
13- well the document matches, relative to other documents .
22+ document matches, the query clause also calculates a relevance score in the
23+ `_score` meta-field .
1424
15- Query context is in effect whenever a query clause is passed to a `query` parameter,
16- such as the `query` parameter in the <<request-body-search-query,`search`>> API.
17- --
25+ Query context is in effect whenever a query clause is passed to a `query`
26+ parameter, such as the `query` parameter in the
27+ <<request-body-search-query,search>> API.
1828
19- Filter context::
20- +
21- --
22- In _filter_ context, a query clause answers the question ``__Does this document
23- match this query clause?__'' The answer is a simple Yes or No -- no scores are
24- calculated. Filter context is mostly used for filtering structured data, e.g.
29+ [float]
30+ [[filter-context]]
31+ === Filter context
32+ In a filter context, a query clause answers the question ``__Does this
33+ document match this query clause?__'' The answer is a simple Yes or No -- no
34+ scores are calculated. Filter context is mostly used for filtering structured
35+ data, e.g.
2536
2637* __Does this +timestamp+ fall into the range 2015 to 2016?__
2738* __Is the +status+ field set to ++"published"++__?
@@ -34,8 +45,10 @@ parameter, such as the `filter` or `must_not` parameters in the
3445<<query-dsl-bool-query,`bool`>> query, the `filter` parameter in the
3546<<query-dsl-constant-score-query,`constant_score`>> query, or the
3647<<search-aggregations-bucket-filter-aggregation,`filter`>> aggregation.
37- --
3848
49+ [float]
50+ [[query-filter-context-ex]]
51+ === Example of query and filter contexts
3952Below is an example of query clauses being used in query and filter context
4053in the `search` API. This query will match documents where all of the following
4154conditions are met:
@@ -80,4 +93,4 @@ significand's precision will be converted to floats with loss of precision.
8093
8194TIP: Use query clauses in query context for conditions which should affect the
8295score of matching documents (i.e. how well does the document match), and use
83- all other query clauses in filter context.
96+ all other query clauses in filter context.
0 commit comments