重庆本地建站,网站快速排名,管家通库存管理软件,精品网站建设平台前言
ES 里面有 5 种复合查询#xff0c;分别是#xff1a;
Boolean QueryBoosting QueryConstant Score QueryDisjunction Max QueryFunction Score Query
Boolean Query在之前已经介绍过了#xff0c;今天来看一下 Boosting Query 用法#xff0c;其实也非常简单…前言
ES 里面有 5 种复合查询分别是
Boolean QueryBoosting QueryConstant Score QueryDisjunction Max QueryFunction Score Query
Boolean Query在之前已经介绍过了今天来看一下 Boosting Query 用法其实也非常简单总结起来就一句话对不期待的查询关键词进行相关性降分。 Boost 加权机制底层也是 Lucene 提供的能力对重要的数据加权有两个时机一个是在索引时一个是在查询时在索引时候加权查询性能会比较高但不灵活所以都会选择在查询时加权加权的方式也很简单如
title: china^20 OR content: china^20
在 ES里面的大多数全文检索 单 Query 都支持 boost 加权但想要实现降权却不行因为 Lucene 底层不直接支持需要使用 function score query来间接实现boost 的数值必须是正数当然也可以包括 0-1 之间的小数所以在 ES 中就封装了 Boosting Query 来支持对某些关键词进行降权查询却又不是不让其出现在查询结果中只是让其排名靠后
写入测试数据
在 kibana 中的 dev_tools 的 console 中直接使用下面的 POST 语句即可需要注意如果 ES 版本低于 7.x 的在 PATH 里面要加上 type否则会报错
POST test01/doc/_bulk
{ index : { _id : 1 } }
{ title : Collecting Service, content: Logstash }
{ index : { _id : 2 } }
{ title : Collecting Service, content: Beats }
{ index : { _id : 3 } }
{ title : Collecting Service, content: FLume } 写完之后可以在 Management Index patterns Create Index Patterns 里面创建手动创建索引模板可以看到生成了如下 mapping需要注意的时这里面自动推断的 mapping 字段并不能删减字段因为我们是已经
将数据写入了 ES如果想要控制字段的生成比如不想要 content.keyword 字段那么就要在写入数据前提前定制 mapping 才可以 查询测试数据
GET test01/_search?
{query: {match: {title: Collecting}}
}
返回结果
{took : 2,timed_out : false,_shards : {total : 6,successful : 6,skipped : 0,failed : 0},hits : {total : 3,max_score : 0.2876821,hits : [{_index : test01,_type : doc,_id : 3,_score : 0.2876821,_source : {title : Collecting Service,content : FLume}},{_index : test01,_type : doc,_id : 2,_score : 0.2876821,_source : {title : Collecting Service,content : Beats}},{_index : test01,_type : doc,_id : 1,_score : 0.2876821,_source : {title : Collecting Service,content : Logstash}}]}
}可以看到评分都相等这个时候如果我想要命中 logstash 的不优先展示就可以使用 Boosting Query 了
GET test01/_search?
{query: {boosting: {positive: {match: {title: Collecting Service}},negative: {match: {content: Logstash}},negative_boost: 0.5}}
}
结果展示
{took : 2,timed_out : false,_shards : {total : 6,successful : 6,skipped : 0,failed : 0},hits : {total : 3,max_score : 0.5753642,hits : [{_index : test01,_type : doc,_id : 3,_score : 0.5753642,_source : {title : Collecting Service,content : FLume}},{_index : test01,_type : doc,_id : 2,_score : 0.5753642,_source : {title : Collecting Service,content : Beats}},{_index : test01,_type : doc,_id : 1,_score : 0.2876821,_source : {title : Collecting Service,content : Logstash}}]}
}Boosting Query原理
Positive Boosting:
这种形式用于增强具有特定条件的文档的得分。它由两个子查询组成主查询positive query和副查询boost query。主查询用于匹配文档而副查询用于对匹配到的文档进行权重调整。Boosting Query将副查询的分数与主查询的分数相乘从而影响文档的最终得分。 Negative Boosting:
这种形式用于降低具有特定条件的文档的得分。它同样由两个子查询组成主查询和副查询。在Negative Boosting中主查询用于匹配文档而副查询用于对不匹配的文档进行权重调整。Boosting Query将副查询的分数与主查询的分数相乘并将结果从1中减去以降低不匹配文档的得分。 Boosting Query的实现原理如下
解析查询语句Elasticsearch首先解析用户提供的Boosting Query语句提取出主查询和副查询以及相应的权重。执行查询对索引中的文档进行主查询匹配并为匹配到的文档计算得分。计算副查询得分对于每个匹配到的文档执行副查询并计算副查询的得分。应用权重调整根据Boosting Query的类型Positive Boosting或Negative Boosting将副查询的得分与主查询的得分相乘或者从1中减去从而调整文档的最终得分。返回结果根据得分对匹配的文档进行排序将搜索结果返回给用户。