[ES] of the difference between term and match

termusage

First look at the definition of the term, term behalf exact match, which is the exact query, the search terms will not be disassembled before the word search.

Here will be described by way of example, to store some of the data:

{
    "title": "love China",
    "content": "people very love China",
    "tags": ["China", "love"]
}
{
    "title": "love HuBei",
    "content": "people very love HuBei",
    "tags": ["HuBei", "love"]
}

To use term the next query:

{
  "query": {
    "term": {
      "title": "love"
    }
  }
}

As a result, the above two data can be queried:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.6931472,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "8",
        "_score": 0.6931472,
        "_source": {
          "title": "love HuBei",
          "content": "people very love HuBei",
          "tags": ["HuBei","love"]
        }
      },
      {
        "_index": "test",
        "_type": "doc",
        "_id": "7",
        "_score": 0.6931472,
        "_source": {
          "title": "love China",
          "content": "people very love China",
          "tags": ["China","love"]
        }
      }
    ]
  }
}

Discovery, title keywords related to love in all checked out, but I just want an exact match  love Chinathis, see if you can check out the following wording:

{
  "query": {
    "term": {
      "title": "love China"
    }
  }
}

Execution found no data , Conceptually, term belongs to an exact match can only check a single word. I would like to term match multiple words how to do? Can be used termsto:

{
  "query": {
    "terms": {
      "title": ["love", "China"]
    }
  }
}

Query results:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.6931472,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "8",
        "_score": 0.6931472,
        "_source": {
          "title": "love HuBei",
          "content": "people very love HuBei",
          "tags": ["HuBei","love"]
        }
      },
      {
        "_index": "test",
        "_type": "doc",
        "_id": "7",
        "_score": 0.6931472,
        "_source": {
          "title": "love China",
          "content": "people very love China",
          "tags": ["China","love"]
        }
      }
    ]
  }
}

All inquiries found out, and why? Because in terms of [ ] more or relationship it is, as long as one word on it. Want to meet notice two words, then you have to use the bool must do, as follows:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "title": "love"
          }
        },
        {
          "term": {
            "title": "china"
          }
        }
      ]
    }
  }
}
You can see, we've used above china is lowercase. When using uppercase China when we searched and found no information found. This is why? When the word title during storage, conducted a segmentation process. We use here is the default word processor a word processing. We can look at how word processing?

Word processor

GET test/_analyze
{
  "text" : "love China"
}

The results are:

{
  "tokens": [
    {
      "token": "love",
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "china",
      "start_offset": 5,
      "end_offset": 10,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}

Out for analysis loveand chinathe two words. The termonly full account of the above word matching, not matching any change. So, we use Chinasuch a way that when the query will fail. Later there will be a devoted word breaker.

match usage

First use love Chinato match.

GET test/doc/_search
{
  "query": {
    "match": {
      "title": "love China"
    }
  }
}

The results are:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.3862944,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "7",
        "_score": 1.3862944,
        "_source": {
          "title": "love China",
          "content": "people very love China",
          "tags": [
            "China",
            "love"
          ]
        }
      },
      {
        "_index": "test",
        "_type": "doc",
        "_id": "8",
        "_score": 0.6931472,
        "_source": {
          "title": "love HuBei",
          "content": "people very love HuBei",
          "tags": [
            "HuBei",
            "love"
          ]
        }
      }
    ]
  }
}

Found that both check out, and why? Because the match when searching, will carry out a word split, Chai Wan, again matching the above two elements, their title entry is: love china hubeiwe search for love Chinaour word to get treatment love china, and the relationship of belonging or as long as any one of the entries in it will be able to match. If you want loveand Chinaat the same time to match words, how do? usematch_phrase

match_phrase usage

match_phrase Called phrase search, requires that all word must appear in the document at the same time, must be close to the same location at the same time.

GET test/doc/_search
{
  "query": {
    "match_phrase": {
      "title": "love china"
    }
  }
}

The results are:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.3862944,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "7",
        "_score": 1.3862944,
        "_source": {
          "title": "love China",
          "content": "people very love China",
          "tags": [
            "China",
            "love"
          ]
        }
      }
    ]
  }
}

 

 

Guess you like

Origin www.cnblogs.com/weknow619/p/ES.html
Recommended