首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >ElasticSearch 6.x 学习笔记:32.Java API之复合查询

ElasticSearch 6.x 学习笔记:32.Java API之复合查询

作者头像
程裕强
发布2022-05-06 19:23:31
发布2022-05-06 19:23:31
4730
举报

https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-compound-queries.html

Compound queriesedit 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.

1、constant_score query

A query which wraps another query, but executes it in filter context. All matching documents are given the same “constant” _score.

代码语言:javascript
复制
constantScoreQuery(termQuery("name","kimchy")).boost(2.0f);

2、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.

代码语言:javascript
复制
boolQuery()
        .must(termQuery("content", "test1"))                 
        .must(termQuery("content", "test4"))                 
        .mustNot(termQuery("content", "test2"))              
        .should(termQuery("content", "test3"))               
        .filter(termQuery("content", "test5"));

3、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.

代码语言:javascript
复制
disMaxQuery()
        .add(termQuery("name", "kimchy"))                    
        .add(termQuery("name", "elasticsearch"))             
        .boost(1.2f)                                         
        .tieBreaker(0.7f);

4、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.

To use ScoreFunctionBuilders just import them in your class:

代码语言:javascript
复制
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.*;
代码语言:javascript
复制
FilterFunctionBuilder[] functions = {
        new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                matchQuery("name", "kimchy"),                
                randomFunction()),                           
        new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                exponentialDecayFunction("age", 0L, 1L))     
};
functionScoreQuery(functions);

5、boosting query

Return documents which match a positive query, but reduce the score of documents which also match a negative query.

代码语言:javascript
复制
boostingQuery(
            termQuery("name","kimchy"),                      
            termQuery("name","dadoonet"))                    
        .negativeBoost(0.2f); 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-02-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、constant_score query
  • 2、bool query
  • 3、dis_max query
  • 4、function_score query
  • 5、boosting query
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档