在Elasticsearch的DSL中, 有两个概念需要搞清楚, query 和 filter, 对ES的检索效率是很有影响的。
下面就来搞清楚这两个关键字的具体函数。
query context: 回答的是这个文档在多大程度上匹配查询语句(How well does this document match this query clause?),会计算出一个分数_score。
filter context: 回答的是这个文档与查询语句是否匹配,是 或者 不是(Does this document match this query clause?),不会计算分数。
除了需要匹配程度的查询(有_score的情况)使用query, 其余的查询都应该使用filter。(As a general rule,
use query clauses for full-text search or for any condition that should affect the relevance score, and use filters for everything else.)
filter的结果是会被ES缓存的, 以此来提高效率。
另外, filter由于不计算分数及排序, 所以, 速度较 query要快。
下面的例子来自https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
GET _search { "query": { "bool": { "must": [ { "match": { "title": "Search" }}, { "match": { "content": "Elasticsearch" }} ], "filter": [ { "term": { "status": "published" }}, { "range": { "publish_date": { "gte": "2015-01-01" }}} ] } } }
|
The query parameterindicates query context. |
|
The bool andtwo match clausesare used in query context, which means that they are used to score how well each document matches. |
|
The filter parameterindicates filter context. |
|
The term and range clausesare used in filter context. They will filter out documents which do not match, but they will not affect the score for matching documents. |
时间: 2024-11-11 01:00:59