白话Elasticsearch04- 结构化搜索之使用terms query搜索多个值以及多值搜索结果优化

举报
小工匠 发表于 2021/09/09 23:27:35 2021/09/09
【摘要】 文章目录 terms概述准备数据小例子搜索articleID为KDKE-B-9947-#kL5或QQPX-R-3956-#aD8的帖子搜索tag中包含java的帖子 优化搜索结果,仅仅搜索ta...


在这里插入图片描述

terms概述

继续跟中华石杉老师学习ES,第三篇

课程地址: https://www.roncoo.com/view/55

https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl-terms-filter.html
在这里插入图片描述

6.4版本对应的 terms query
https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl-terms-query.html

7.0 版本对应的 terms query
https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-terms-query.html

前面的实例中,我们都是使用的term,只能将一个字段,从一个value中取搜索

    term: {"field": "value"}

  
 
  • 1

比如

{
   "term": {
      "articcleID": "XHDK-A-1293-#fJ3"
    }
  }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

terms 呢? terms可以实现将一个字段,从多个value中检索的效果

   terms: {"field": ["value1", "value2"]}


  
 
  • 1
  • 2

类似于SQL中的in

select * from table where col in ("value1","value2"......)

  
 
  • 1

准备数据

为了演示terms, 我们再新增个tag字段吧

POST /forum/article/_bulk
{"update":{"_id":"1"}}
{"doc":{"tag":["java","hadoop"]}}
{"update":{"_id":"2"}}
{"doc":{"tag":["java"]}}
{"update":{"_id":"3"}}
{"doc":{"tag":["hadoop"]}}
{"update":{"_id":"4"}}
{"doc":{"tag":["java","elasticsearch"]}}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

小例子

搜索articleID为KDKE-B-9947-#kL5或QQPX-R-3956-#aD8的帖子

GET /forum/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "articleID": [
            "KDKE-B-9947-#kL5",
            "QQPX-R-3956-#aD8"
          ]
        }
      }
    }
  }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Terms Query写法(推荐)

GET /forum/_search
{
  "query": {
    "terms": {
      "articleID": [
        "KDKE-B-9947-#kL5",
        "QQPX-R-3956-#aD8"
      ]
    }
  }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述


搜索tag中包含java的帖子

GET /forum/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "tag": [
            "java"
          ]
        }
      }
    }
  }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Terms Query写法(推荐)

GET /forum/_search
{
  "query": {
    "terms": {
      "tag": [
        "java"
      ]
    }
  }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述


优化搜索结果,仅仅搜索tag只包含java的帖子

上面的第二个例子中,搜索java ,可以看到返回了3条结果,其中

	"tag": [
            "java",
            "elasticsearch"
          ]


	"tag": [
            "java",
            "hadoop"
          ],

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

也被搜索出来了,如果仅仅是想搜索tag只包含java的帖子呢 ?

为了达到该效果,我们新增个tag_cnt字段 ,用数量来过滤下

POST /forum/article/_bulk
{"update":{"_id":"1"}}
{"doc":{"tag_cnt":2}}
{"update":{"_id":"2"}}
{"doc":{"tag_cnt":1}}
{"update":{"_id":"3"}}
{"doc":{"tag_cnt":1}}
{"update":{"_id":"4"}}
{"doc":{"tag_cnt":2}}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
GET /forum/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "tag_cnt": 1
              }
            },
            {
              "terms":{
                "tag":["java"]
              }
            }
          ]
        }
      }
    }
  }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这里插入图片描述

Terms Query写法(推荐) ,_score 固定为1

GET /forum/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "tag_cnt": 1
          }
        },
        {
          "terms": {
            "tag": [
              "java"
            ]
          }
        }
      ]
    }
  }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

计算相关度分数 _score 的写法

GET /forum/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "tag_cnt": 1
          }
        },
        {
          "terms": {
            "tag": [
              "java"
            ]
          }
        }
      ]
    }
  }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在这里插入图片描述


总结一下:

  • terms多值搜索
  • 优化terms多值搜索的结果,可以增加个cnt字段标示一下,组合过滤
  • terms相当于SQL中的in语句

文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。

原文链接:artisan.blog.csdn.net/article/details/90142687

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。