Elasticsearch 组合查询
Elasticsearch 组合查询(Compound queries),部分内容来自 B 站中华石杉 Elasticsearch 顶尖高手系列课程核心知识篇,英文内容来自官方文档。 其中大部分内容来自官方文档,英文,TL;DR
组合查询 Compound queries
Compound queries wrap other compound or leaf queries, either to combine their results and scores, to change their behaviour, or to switch from query to filter context.
bool query
boosting query
constant_score query
dis_max query
function_score query
Boolean query
The default query for combining multiple leaf or compound query clauses, as must, should, must_not, or filter clauses. The must and should clauses have their scores combined -- the more matching clauses, the better -- while the must_not and filter clauses are executed in filter context.
A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, each clause with a typed occurrence.
must: The clause (query) must appear in matching documents and will contribute to the score.
filter: The clause (query) must appear in matching documents. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.
should: The clause (query) should appear in the matching document.
must_not: The clause (query) must not appear in the matching documents. ...Because scoring is ignored, a score of 0 for all documents is returned.
The bool query takes a more-matches-is-better approach, so the score from each matching must or should clause will be added together to provide the final _score for each document.
每个子查询都会计算一个 document 针对它的相关度分数,然后 bool 综合所有分数,合并为一个分数,当然 filter 是不会计算分数的
Using minimum_should_match
You can use the minimum_should_match parameter to specify the number or percentage of should clauses returned documents must match.
If the bool query includes at least one should clause and no must or filter clauses, the default value is 1. Otherwise, the default value is 0.
Scoring with bool.filter
Queries specified under the filter element have no effect on scoring -- scores are returned as 0. Scores are only affected by the query that has been specified.
Named queries
Each query accepts a _name in its top level definition. You can use named queries to track which queries matched returned documents.
Boost Query
Returns documents matching a positive query while reducing the relevance score of documents that also match a negative query.
Constant score Query
Wraps a filter query and returns every matching document with a relevance score equal to the boost paramster value.
Disjunction max query
Return documents matching one or more wrapped queries, call query clauses or clauses.
If a returned document matches multiple query clauses, the dis_max query assigns the document the highest relevance score from any matching clause, plus a tie breaking increment for any additional matching subqueries.
Function score query
The function_score allows you to modify the score of documents that are retrieved by a query.
版权声明: 本文为 InfoQ 作者【escray】的原创文章。
原文链接:【http://xie.infoq.cn/article/4b0b8905373f670c97286b04e】。
本文遵守【CC-BY 4.0】协议,转载请保留原文出处及本版权声明。
评论