elasticsearch 中term與match區別
- term是精確查詢
- match是模糊查詢
term查詢
term是代表完全匹配,也就是精確查詢,搜索前不會再對搜索詞進行分詞,所以我們的搜索詞必須是文檔分詞集合中的一個。比如說我們要找標題為北京奧運的所有文檔
1
2
3
4
5
6
7
8
|
$curl -xget http: //localhost:9200/index/doc/_search?pretty -d '{ "query" :{ "term" :{ "title" : "北京奧運" } } }' |
將會得到如下結果
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
|
{ "took" : 1 , "timed_out" : false , "_shards" : { "total" : 5 , "successful" : 5 , "failed" : 0 }, "hits" : { "total" : 1 , "max_score" : 0.92055845 , "hits" : [ { "_index" : "index" , "_type" : "doc" , "_id" : "3" , "_score" : 0.92055845 , "_source" : { "content" : "同一個世界同一個夢想" , "title" : "北京奧運" , "tags" : [ "和平" ] } } ] } } |
match類查詢
match查詢會先對搜索詞進行分詞,分詞完畢后再逐個對分詞結果進行匹配,因此相比于term的精確搜索,match是分詞匹配搜索,match搜索還有兩個相似功能的變種,一個是match_phrase,一個是multi_match,接下來詳細介紹一下
match
前面提到match搜索會先對搜索詞進行分詞,對于最基本的match搜索來說,只要搜索詞的分詞集合中的一個或多個存在于文檔中即可,例如,當我們搜索中國杭州,搜索詞會先分詞為中國和杭州,只要文檔中包含搜索和杭州任意一個詞,都會被搜索到
1
2
3
4
5
6
7
8
|
$curl -xget http: //localhost:9200/index/doc/_search?pretty -d '{ "query" : { "match" : { "content" : "中國杭州" } } }' |
文檔3正文中有杭州,文檔2中有中國,因此搜索結果有兩個,文檔3中杭州出現兩次,所以排在前面,結果如下:
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
|
{ "took" : 1 , "timed_out" : false , "_shards" : { "total" : 5 , "successful" : 5 , "failed" : 0 }, "hits" : { "total" : 2 , "max_score" : 0.99999994 , "hits" : [ { "_index" : "index" , "_type" : "doc" , "_id" : "4" , "_score" : 0.99999994 , "_source" : { "content" : "杭州是一個美麗的城市,歡迎來到杭州" , "title" : "宣傳" , "tags" : [ "旅游" , "城市" ] } }, { "_index" : "index" , "_type" : "doc" , "_id" : "2" , "_score" : 0.8838835 , "_source" : { "content" : "中國是世界上人口最多的國家" , "title" : "中國" , "tags" : [ "中國" , "人口" ] } } ] } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/sxf_123456/article/details/78845437