Explanation of the difference between term and match in elasticsearch

  • 2021-07-01 07:32:41
  • OfStack

Difference between term and match in elasticsearch

term is an exact query match is a fuzzy query

term Query

term stands for exact match, that is, accurate query. Search terms will not be segmented before search, so our search terms must be one of the document segmentation sets. For example, we are looking for all the documents titled Beijing Olympics


$curl -XGET http://localhost:9200/index/doc/_search?pretty -d 
'{
 "query":{
  "term":{
    "title":" Beijing Olympic Games "
  }
 }
}'

You will get the following results


{
  "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": " Same as 1 All worlds are the same 1 A dream ",
      "title": " Beijing Olympic Games ",
      "tags": [
        " Peace "
      ]
    }
   }
  ]
 }
}

match Class Query

match query will segment the search terms first, and then match the segmentation results one by one after the segmentation is completed. Therefore, compared with the precise search of term, match is a word segmentation matching search, and match search has two variants with similar functions, one is match_phrase, and the other is multi_match. Next, we will introduce in detail 1

match

As mentioned above, match search will segment the search terms first. For the most basic match search, as long as one or more of the segmentation sets of search terms exist in the document, for example, when we search Hangzhou, China, the search terms will be segmented into China and Hangzhou first. As long as the document contains any one word of search and Hangzhou, it will be searched


$curl -XGET http://localhost:9200/index/doc/_search?pretty -d 
'{
  "query": {
    "match": {
      "content": " Hangzhou, China "
    }
  }
}'

There are Hangzhou in the text of Document 3, China in Document 2, so there are two search results, and Hangzhou appears twice in Document 3, so it ranks first. The results are as follows:


{
 "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" : " Hangzhou is 1 A beautiful city , Welcome to Hangzhou ",
        "title" : " Propaganda ",
        "tags" : [ " Tourism ", " City " ]
      }
    }, {
      "_index" : "index",
      "_type" : "doc",
      "_id" : "2",
      "_score" : 0.8838835,
      "_source" : {
         "content" : " China is the most populous country in the world ",
         "title" : " China ",
         "tags" : [ " China ", " Population " ]
      }
    } ]
  }
}

Summarize


Related articles: