七 dis_max--best fields策略

1. 假设数据

doc1:

title: black cat.

content: The cat like eat fish.

doc2:

title: red dog.

content: The dog like eat bone.

doc3:

title: yellow cat.

content: yellow cat look good.

doc4:

title: white dog.

content: People like white dog.

doc5:

title: animal.

content: cat is animal, and dog is also animal.

 

2. 查询需求

查询title或content中包含cat或is的帖子。

GET /forum/article/_search
{
    "query": {
        "bool": {
            "should": [
                { "match": { "title": "cat is" }},
                { "match": { "content":  "cat is" }}
            ]
        }
    }
}

 

3.结果分析

查询结果排序可能是:doc1>doc3>doc5

 

4. 原因分析

计算每个document的relevance score:每个query的分数相加,乘以matched query数量,除以总query数量。

doc1---->  title查询分数:1.1,content查询分数:1.2,两者相加得2.3;matched query数量为2(title和content各1次),总查询数量为2(title和content各被查询了1次)。relevance score:2.3 * 2 / 2 = 2.3

doc3与doc1类似。

doc5---->  title查询分数:没有match到,为0,content查询分数:2.3(cat和is都匹配到了,分数较高),两者相加得2.3;matched query数量为1(content 1次),总查询数量为2(title和content各被查询了1次)。relevance score:2.3 * 1 / 2 = 1.15

 

5. best fields策略

如果我们想要到查询结果是:一个filed中能够匹配到尽可能多的关键字,则排在前面,即让doc5排在前面,则可以使用下面的查询方式:

GET /forum/article/_search
{
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "cat is" }},
                { "match": { "content":  "cat is" }}
            ]
        }
    }
}

这样就会直接以最大的filed分数作为relevance score,那么doc1的relevance score分数为1.2,doc5的relevance score分数为2.3

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值