1. 程式人生 > >elasticsearch :term與match區別

elasticsearch :term與match區別

elasticsearch 中term與match區別

term是精確查詢

match是模糊查詢

term查詢

term是代表完全匹配,也就是精確查詢,搜尋前不會再對搜尋詞進行分詞,所以我們的搜尋詞必須是文件分詞集合中的一個。比如說我們要找標題為北京奧運的所有文件

$curl -XGET http://localhost:9200/index/doc/_search?pretty -d 
'{
  "query":{
    "term":{
        "title":"北京奧運"
    }
  }
}'


將會得到如下結果

{
    "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搜尋來說,只要搜尋詞的分詞集合中的一個或多個存在於文件中即可,例如,當我們搜尋中國杭州,搜尋詞會先分詞為中國和杭州,只要文件中包含搜尋和杭州任意一個詞,都會被搜尋到

$curl -XGET http://localhost:9200/index/doc/_search?pretty -d 
'{
    "query": {
        "match": {
            "content": "中國杭州"
        }
    }
}'


文件3正文中有杭州,文件2中有中國,因此搜尋結果有兩個,文件3中杭州出現兩次,所以排在前面,結果如下:

{
  "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