当前位置:   article > 正文

es中,should的用法(以组合搜索做示例)_es should

es should

可以先参考这里:https://blog.csdn.net/wyqwilliam/article/details/84024182
接下来以组合搜索时使用should条件来做演示。
数据准备如下:
创建索引:

PUT http://127.0.0.1:9200/mytest/
{
  "settings": {
    "index": {
      "number_of_shards": "1",
      "number_of_replicas": "0"
   }
 },
  "mappings": {
    "person": {
      "properties": {
        "name": {
          "type": "text"
       },
        "age": {
          "type": "integer"
       },
        "mail": {
          "type": "keyword"
       },
        "hobby": {
          "type": "text",
          "analyzer":"ik_max_word"
       }
     }
   }
 }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

添加文档:

POST http://127.0.0.1:9200/mytest/_bulk
{"index":{"_index":"mytest","_type":"person"}}
{"name":"张三","age": 20,"mail": "111@qq.com","hobby":"羽毛球、乒乓球、足球"}
{"index":{"_index":"mytest","_type":"person"}}
{"name":"李四","age": 21,"mail": "222@qq.com","hobby":"羽毛球、乒乓球、足球、篮球"}
{"index":{"_index":"mytest","_type":"person"}}
{"name":"王五","age": 22,"mail": "333@qq.com","hobby":"羽毛球、篮球、游泳、听音乐"}
{"index":{"_index":"mytest","_type":"person"}}
{"name":"赵六","age": 23,"mail": "444@qq.com","hobby":"跑步、游泳、篮球"}
{"index":{"_index":"mytest","_type":"person"}}
{"name":"孙七","age": 24,"mail": "555@qq.com","hobby":"听音乐、看电影、羽毛球"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

搜索查询如下:

POST /mytest/person/_search
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "hobby": "篮球"
                }
            }, 
            "must_not": {
                "match": {
                    "hobby": "音乐"
                }
            }, 
            "should": [
                {
                    "match": {
                        "hobby": "游泳"
                    }
                }
            ]
        }
    }, 
    "highlight": {
        "fields": {
            "hobby": { }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

上面搜索的意思是:
搜索结果中必须包含篮球,不能包含音乐,如果包含了游泳,那么它的相似度更高。

结果:
在这里插入图片描述
评分的计算规则:
bool 查询会为每个文档计算相关度评分 _score , 再将所有匹配的 must 和should 语句的分数 _score 求和,最后除以 must 和 should 语句的总数。
must_not 语句不会影响评分; 它的作用只是将不相关的文档排除。

默认情况下,should中的内容不是必须匹配的,如果查询语句中没有must,那么就会至少匹配其中一个。当然了,
也可以通过minimum_should_match参数进行控制,该值可以是数字也可以的百分比。
示例:

post  127.0.0.1:9200/mytest/person/_search

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "hobby": "游泳"
                    }
                }, 
                {
                    "match": {
                        "hobby": "篮球"
                    }
                }, 
                {
                    "match": {
                        "hobby": "音乐"
                    }
                }
            ], 
            "minimum_should_match": 2
        }
    }, 
    "highlight": {
        "fields": {
            "hobby": { }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

minimum_should_match为2,意思是should中的三个词,至少要满足2个。
结果:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/807342
推荐阅读
相关标签
  

闽ICP备14008679号