Query and filter context
查询和过滤上下文
Relevance scores
相关性得分
By default, Elasticsearch sorts matching search results by relevance score, which measures how well each document matches a query.
默认情况下,Elasticsearch按相关性得分对匹配的搜索结果进行排序,该得分衡量每个文档与查询的匹配程度。
The relevance score is a positive floating point number, returned in the _score
meta-field of the search API. The higher the _score
, the more relevant the document. While each query type can calculate relevance scores differently, score calculation also depends on whether the query clause is run in a query or filter context.
相关性分数是一个正浮点数,在搜索 API 的meta-field(元字段)_score
中返回 。_score
越高,文档越相关。每种查询类型计算相关性得分可以不同,分数计算还取决于查询子句是运行在查询上下文中还是在过滤器上下文。
Query context
查询上下文
In the query context, a query clause 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 relevance score in the _score
meta-field.
在查询上下文中,查询子句回答这样的问题“ 此文档与该查询子句的匹配程度如何?”除了确定文档是否匹配外,查询子句还计算meta-field(元字段)_score
的相关性得分 。
Query context is in effect whenever a query clause is passed to a query
parameter, such as the query
parameter in the search API.
查询上下文在查询子句中通过query
参数设置,例如:
GET /_search
{
"query" : {
"term" : { "user" : "kimchy" }
}
}
Filter context
过滤器上下文
In a 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
timestamp
fall into the range 2015 to 2016? -
Is the
status
field set to"published"
? -
timestamp
是否属于2015年至2016年的范围? -
*
status
字段设置为"published"
*吗?
Frequently used filters will be cached automatically by Elasticsearch, to speed up performance.
常用过滤器将由Elasticsearch自动缓存,以提高性能。
Filter context is in effect whenever a query clause is passed to a filter
parameter, such as the filter
or must_not
parameters in the bool
query, the filter
parameter in the constant_score
query, or the filter
aggregation.
过滤器上下文在查询子句中通过filter
参数设置(例如 bool
查询中的filter
参数和must_not
参数, constant_score
中的filter
参数,或者filter
聚合)。
Example of query and filter contexts
查询和过滤器上下文示例
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:
以下是在search
API中使用查询上下文和过滤器上下文查询子句的示例。此查询将匹配满足以下所有条件的文档:
-
The
title
field contains the wordsearch
. -
The
content
field contains the wordelasticsearch
. -
The
status
field contains the exact wordpublished
. -
The
publish_date
field contains a date from 1 Jan 2015 onwards. -
该
title
字段包含单词search
。 -
该
content
字段包含单词elasticsearch
。 -
该
status
字段包含确切的单词published
。 -
该
publish_date
字段包含从2015年1月1日开始的日期。
GET /_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
Copy as cURLView in Console
The query parameter indicates query context. | |
---|---|
The bool and two match clauses are used in query context, which means that they are used to score how well each document matches. | |
The filter parameter indicates filter context. Its term and range clauses are used in filter context. They will filter out documents which do not match, but they will not affect the score for matching documents. |
query 参数表示查询上下文。 | |
---|---|
must 下的两个match 子句用于查询上下文,它们被用来表示每个文档匹配程度的评分。 | |
filter 下的参数表示过滤器上下文。其term and range 子句用于过滤器上下文。它们将过滤出不匹配的文档,但不会影响匹配文档的分数。 |
Scores calculated for queries in query context are represented as single precision floating point numbers; they have only 24 bits for significand’s precision. Score calculations that exceed the significand’s precision will be converted to floats with loss of precision.
在查询上下文中为查询计算的分数被表示为单精度浮点数;他们只有24位bits的有效数字的精度。超过有效位数精度的计算分数将转换为失去精度的浮点数。
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.
在查询上下文中使用查询子句以应对可能影响匹配文档得分(即文档匹配程度)的条件,并在过滤器上下文中使用所有其他查询子句。