Elasticsearch的复合查询

本文介绍了Elasticsearch中的复合查询技术,包括constant_score、bool、dis_max、function_score及boosting等查询方式。详细解释了每种查询的用途、执行逻辑及应用场景,帮助读者更好地理解和运用这些查询技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.           
名字含义
mustThe clause (query) must appear in matching documents and will contribute to the score.
filterThe 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.
shouldThe 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_notThe 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 负升压

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值