白话Elasticsearch03- 基于bool组合多个filter条件来搜索数据

版权声明:【show me the code ,change the world】 https://blog.csdn.net/yangshangwei/article/details/90140727

概述

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

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

白话Elasticsearch01- 使用term filter来搜索数据中演示了filter 单个过滤条件使用 term 的用法,只有一个term条件,如果有多个呢? 这里我们就来学习下基于bool组合多个filter条件来搜索数据

6.4版本官网说明:
https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl-bool-query.html

在这里插入图片描述

7.0版本官网说明:
https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-bool-query.html


数据

在这里插入图片描述

我们在 白话Elasticsearch01- 使用term filter来搜索数据通过_bulk的方式批量写入了4条数据,这里我们基于 forum 索引的这几条数据来演示下 bool 组合多个filter

mapping 如下: (articleID 为 keyword)
在这里插入图片描述


小示例

搜索发帖日期为2017-01-01,或者帖子ID为XHDK-A-1293-#fJ3的帖子,同时要求帖子的发帖日期绝对不为2017-01-02

用我们熟悉的SQL来写的话 类似如下的方式:

	select  *  from  forum.article
	where (post_date='2017-01-01' or article_id='XHDK-A-1293-#fJ3')
	and  post_date!='2017-01-02'

在ES中

  • must 需要满足条件 ==或like
  • must_not 不需要在满足条件内的 !=或 not like
  • should: should中的两个条件至少满足一个就可以,should下有多个条件时注意加参数 minimum_should_match

bool中可以使用 must、 must_not 、should 来组合查询条件 ,bool 可嵌套。

分析一下 where 后的 两个条件 ,那就需要用bool来组合了,并且这两个条件的关联是 and ,那就是 要都符合。

(post_date=‘2017-01-01’ or article_id=‘XHDK-A-1293-#fJ3’) --> 第一个查询条件中 两个字段是or的关系 ,shoud 正好符合

post_date!=‘2017-01-02’–> 第二个条件 != , 使用must_not 即可

然后把 shoud 和must_not 使用bool关联起来即可。

如下:

GET /forum/article/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "postDate": "2017-01-01"
              }
            },
            {
              "term": {
                "articcleID": "XHDK-A-1293-#fJ3"
              }
            }
          ],
          "must_not": {
            "term": {
              "postDate": "2017-01-02"
            }
          }
        }
      }
    }
  }
}

使用constant_score是因为我们这里不关心相关度的排名,仅仅是过滤数据,使用constant_score将_score都设置为1

返回结果:

在这里插入图片描述
根据搜索要求我们来校验下

  • 发帖日期为2017-01-01,或者帖子ID为XHDK-A-1293-#fJ3的帖子

  • 发帖日期绝对不为2017-01-02

返回结果中没有2017-01-02的数据, 同时这两个数据第二条数据符合2017-01-01, 第一条数据 符合 2017-01-01 XHDK-A-1293-#fJ3 。 符合需求

新版本 bool query 推荐写法

GET /forum/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "postDate": "2017-01-01"
          }
        },
        {
          "term": {
            "articcleID": "XHDK-A-1293-#fJ3"
          }
        }
      ],
      "must_not": {
        "term": {
          "postDate": "2017-01-02"
        }
      }
    }
  }
}

搜索帖子ID为XHDK-A-1293-#fJ3,或者是帖子ID为JODL-X-1937-#pV7而且发帖日期为2017-01-01的帖子

我们把上述的搜索转换为SQL来看下

select * from forum.article 
where  
	articleID="XHDK-A-1293-#fJ3"  
	or 
	(articleID = "JODL-X-1937-#pV7" and postDate="2017-01-01")

分析一下, 是个组合条件 ,那肯定需要用bool了, 大条件是 or , 那肯定是一个大should里。

shoud 中第一个条件 articleID=“XHDK-A-1293-#fJ3” ,可以用 must表示

第二个条件 (articleID = “JODL-X-1937-#pV7” and postDate=“2017-01-01”) ,两个must ,那就还得再套上一层 bool,嵌套一层bool

如下:

GET /forum/article/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "articleID": "XHDK-A-1293-#fJ3"
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "articleID": "JODL-X-1937-#pV7"
                    }
                  },
                  {
                    "term": {
                      "postDate": "2017-01-01"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

返回结果:

在这里插入图片描述
或者

新版本 bool query 推荐写法

GET /forum/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "articleID": "XHDK-A-1293-#fJ3"
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "articleID": "JODL-X-1937-#pV7"
                }
              },
              {
                "term": {
                  "postDate": "2017-01-01"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

不过上面的写法会对_score进行计算,然后按照_score 降序排名。 而constant_score 则是对所有的文档的_score 设置为1.0 。

总结下:

1. bool:must,must_not,should,组合多个过滤条件
2. bool可以嵌套


猜你喜欢

转载自blog.csdn.net/yangshangwei/article/details/90140727