欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Elasticsearch的复合查询

程序员文章站 2022-07-09 15:38:22
...

Elasticsearch的复合查询 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.
复合查询包装其他复合查询或叶查询,以组合其结果和分数,更改其行为,或从查询切换到筛选上下文。
改组中的查询如下
-constant_score query
A query which wraps another query, but executes it in filter context. All matching documents are given the same “constant” _score.
一个包含另一个查询的查询,但在Filtercontext中执行它。所有匹配的文档都被赋予相同的“常量”分数。

GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "term" : { "user" : "kimchy"}
            },
            "boost" : 1.2
        }
    }
}
Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.
Filter子句在Filter体中执行,意味着忽略评分,考虑子句缓存;

-bool 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.

组合多叶或者复合查询子句的缺省查询,例如must,should, must_not, or过滤子句。must子句和should子句他们的分数合并,匹配子句越多。当,must_not和filter子句在filter体中执行效果最好

boolQuery()
        .must(termQuery("content", "test1"))                 
        .must(termQuery("content", "test4"))                 
        .mustNot(termQuery("content", "test2"))              
        .should(termQuery("content", "test3"))               
        .filter(termQuery("content", "test5"));   //a query that must appear in the matching documents but doesn’t contribute to scoring.           
名字 含义
must The clause (query) must appear in matching documents and will contribute to the score.
filter The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. 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. If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match. In this case these clauses are only used to influence the score. If the bool query is a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query. This behavior may be explicitly controlled by settings the minimum_should_match parameter.
must_not The clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned.

-dis_max query
A query which accepts multiple queries, and returns any documents which match any of the query clauses. While the bool query combines the scores from all matching queries, the dis_max query uses the score of the single best- matching query clause.
一个接受多个查询的查询,并返回任何匹配查询的文档。当bool查询合并所有匹配查询的分数时,dis_max查询使用单个最佳匹配查询子句的得分。

disMaxQuery()
        .add(termQuery("name", "kimchy"))                    
        .add(termQuery("name", "elasticsearch"))             
        .boost(1.2f)          //boost factor  升压因子                               
        .tieBreaker(0.7f);   //tie breaker

-function_score query
Modify the scores returned by the main query with functions to take into account factors like popularity, recency, distance, or custom algorithms implemented with scripting.
使用函数修改主查询返回的分数,以考虑受欢迎、最近、距离或用脚本实现的自定义算法等因素。
-boosting query
Return documents which match a positive query, but reduce the score of documents which also match a negative query.
回与正查询匹配的文档,但减少与否定查询匹配的文档得分。

boostingQuery(
            termQuery("name","kimchy"),            //query that will promote documents 提升文档查询          
            termQuery("name","dadoonet"))        //query that will demote documents 降级文档的查询            
        .negativeBoost(0.2f);   //  negative boost 负升压

相关标签: Elasticsearch