XML DOM 獲取節點值
1059
2025-03-31
文章目錄
概述
數據
小示例
搜索發帖日期為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)
小示例
搜索發帖日期為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小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。