ElasticSearch之bool多条件查询

写在前面

在实际的业务场景中,不可能只是简单的单值查询 ,更多的是n个条件的综合查询,就像下面的搜索:

针对这种场景我们就需要依赖于bool查询了,本文就一起来看下这部分的内容。

1:bool查询介绍

bool查询用于进行多条件的组合查询,如果是需要计算得分的,则取各个查询的得分之和作为最终得分。bool查询包含四种子查询,must,must_not,should,filter,如下:

实例:

不影响算分的filter和must_not:

影响算分的should和must:

bool的多层嵌套:

相同等级,算分权重相同:

通过boost影响字段的算分权重:

2:实例

2.1:shoud+boost

boost是一个影响的分权中的参数,会被应用在计算得分的公式中影响最终的得分。

看下should用法,以及如何通过boost影响should的得分,准备数据:

复制代码
DELETE blogs
POST blogs/_bulk
{"index": {"_id": 1}}
{"title":"Apple iPad","content":"Apple iPad,Apple iPad"}
{"index": {"_id": 2}}
{"title":"Apple iPad,Apple iPad","content":"Apple iPad"}

我们把相反的内容放到了两个文档里,接下来通过调整title和content查询的boost来影响这两个文档的最终得分,即影响其返回的的顺序。

复制代码
POST blogs/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "title": {
                            "query": "apple,ipad",
                            "boost": 1.1
                        }
                    }
                },
                {
                    "match": {
                        "content": {
                            "query": "apple,ipad",
                            "boost": 1
                        }
                    }
                }
            ]
        }
    }
}

上述查询给了title更高的权重,并且title针对查询的TF(term frequency)更高,所以"_id" : "2"有更高的总得分,被放在结果集的前面,类似的我们也可以调高content的boost,则"_id" : "1"有更高的总得分,被放在结果集的前面:

2.2:boosting+negative+positive

是should查询。

通过boosting+negative+positive来影响得分,其中negative用来给匹配的结果给负分,即减分,positive给匹配的结果正分,即加分,数据:

复制代码
delete news
POST news/_bulk
{"index": {"_id": 1}}
{"content":"Apple Mac"}
{"index": {"_id": 2}}
{"content":"Apple iPad"}
{"index": {"_id": 3}}
{"content":"Apple employee like Apple and Apple Juice"}

假定我们想让苹果公司产品相关的文档有更高的优先级,即放在结果集的更靠前位置,而非苹果产品的文档放在靠后位置,其中文档1,2是评估产品,而3不是苹果产品,如下方式查询满足需求:

复制代码
POST news/_search
{
    "query": {
        "boosting": {
            "positive": {
                "match": {
                    "content": "apple"
                }
            },
            "negative": {
                "match": {
                    "content": "Juice"
                }
            },
            "negative_boost": 0.5
        }
    }
}

写在后面

参考文章列表

ElasticSearch之search API

相关推荐
BerryS3N10 小时前
Code Git 工作树:多分支开发的痛点与工作树的曙光
大数据·git·elasticsearch
Elasticsearch12 小时前
Elastic 与 Deductive AI 携手合作,加速工程团队的 agent 式事件调查
elasticsearch
Elasticsearch13 小时前
将你的 Grafana Kubernetes 仪表板迁移到 Elastic Observability:相同的 PromQL,30 倍更快的查询
elasticsearch
爱莉希雅&&&17 小时前
elasticsearch+kibana+logstash+filebeat链路部署流程
大数据·elasticsearch·搜索引擎·kibana·logstash·filebeat
前端 贾公子19 小时前
Git Worktree 使用指南
大数据·elasticsearch·搜索引擎
Elasticsearch19 小时前
如何使用 OpenTelemetry 对你的搜索 API 进行埋点,并使用 ES|QL 对其进行查询
elasticsearch
阿里云大数据AI技术1 天前
更快、更稳、更省:揭秘阿里云 Elasticsearch 存算分离与弹性扩缩
elasticsearch
阿里云大数据AI技术1 天前
Search Lake:ES x Paimon 让湖上多模态数据可搜可用
人工智能·elasticsearch·搜索引擎
Elasticsearch2 天前
Elastic 如何利用磁盘支持的追踪存储将 OpenTelemetry 尾部采样内存占用降低 65%
elasticsearch
.柒宇.2 天前
ELK日志分析系统实战指南:Elasticsearch搭建与入门(8.18.8版本)
运维·elk·elasticsearch·logback·日志系统