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

网友投稿 871 2022-05-30

文章目录

概述

数据

小示例

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

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

概述

继续跟中华石杉老师学习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)

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

小示例

搜索发帖日期为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'

1

2

3

在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" } } } } } } }

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

使用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" } } } } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

搜索帖子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")

1

2

3

4

5

分析一下, 是个组合条件 ,那肯定需要用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" } } ] } } ] } } } } }

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

32

33

34

返回结果:

或者

新版本 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" } } ] } } ] } } }

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

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

总结下:

1.

bool:must,must_not,should,组合多个过滤条件

2.

bool可以嵌套

Elasticsearch

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:MySql数据库列表数据分页查询、全文检索API零代码实现
下一篇:Node.js数据库开发实战之mysql
相关文章