Match and match in elasticsearch_ phrase、query_ The difference between string and term

Time:2021-8-5

1、 The difference between text field and keyword field

Before understanding the differences between these queries, first understand the differences between the text field and the keyword field:

  • Create index and type, and introduce keyword field
PUT my_index
{
  "mappings": {
    "properties": {
      "name":{
        "type": "keyword"
      }
    }
  }
}
GET /my_index
You can see that the field has been created and the type of the field is keyword

Match and match in elasticsearch_ phrase、query_ The difference between string and term

The default index type is_doc
Match and match in elasticsearch_ phrase、query_ The difference between string and term

Add a piece of data

POST /my_index/_doc/1
{
  "name":"first test"
}

Then query

GET /my_index/_search
{
  "query": {
    "term": {
      "name":"first"
    }
  }
}

GET /my_index/_search
{
  "query": {
    "term": {
      "name":"test"
    }
  }
}

You can see that no data is found
Match and match in elasticsearch_ phrase、query_ The difference between string and term

Then query

GET /my_index/_search
{
  "query": {
    "term": {
      "name": "first test"
    }
  }
}

You can see that the data is matched
Match and match in elasticsearch_ phrase、query_ The difference between string and term
Therefore, set the field to keyword, and the existing value will not be segmented during query

  • Add a field of type text now
PUT /my_index/_mapping
{
  "properties":{
    "tag":{
      "type":"text"
    }
  }
}

GET /my_index

Match and match in elasticsearch_ phrase、query_ The difference between string and term
Add tag data to the previously created doc:

POST /my_index/_doc/1
{
  "name":"first test",
  "tag":"second test"
}

Or use
(recommended)
POST /my_index/_doc/1/_update
{
  "doc":{
    "tag":"second test"
  }
}

Match and match in elasticsearch_ phrase、query_ The difference between string and term

You can see that the data has gone in
Match and match in elasticsearch_ phrase、query_ The difference between string and term

Then query the tag field:

POST /my_index/_search
{
  "query": {
    "term": {
      "tag": "second"
    }
  }
}

POST /my_index/_search
{
  "query": {
    "term": {
      "tag": "test"
    }
  }
}

Match and match in elasticsearch_ phrase、query_ The difference between string and term
Now enter all queries:

POST /my_index/_search
{
  "query": {
    "term": {
      "tag": "second test"
    }
  }
}

Match and match in elasticsearch_ phrase、query_ The difference between string and term
Note: text type fields will be segmented. You can find them if you use split query, but you can’t find them if you directly use all queries.
Note: “1,2” will be split into [1,2], but “1,2” will not be split because there is a space between them.

2、 The difference between match and term

Match and match in elasticsearch_ phrase、query_ The difference between string and term

1、term

1) . term query keyword field

Term does not have word segmentation, and the keyword field does not. You need an exact match

Match and match in elasticsearch_ phrase、query_ The difference between string and term

However, if the word segmentation query is used, the corresponding data cannot be matched

Match and match in elasticsearch_ phrase、query_ The difference between string and term

2) . term query keyword field

Because the text field can be segmented, but the term does not, the term query criteria must be one after the word segmentation of the text field

Match and match in elasticsearch_ phrase、query_ The difference between string and term
If yes, enter all queries:

Match and match in elasticsearch_ phrase、query_ The difference between string and term
Or partial input
Match and match in elasticsearch_ phrase、query_ The difference between string and term

The query failed because the current tag has been segmented and stored as [listen, to, music]

2. Match query text field

Match word segmentation, text word segmentation, as long as the word segmentation result of match is the same as that of text

Match and match in elasticsearch_ phrase、query_ The difference between string and term
success. If they are different, they fail.

3、match_phrase

1)、match_ Phrase matches the keyword field

match_ Phrase will be segmented, but keyword will not be segmented. Match_ The phrase needs to match the keyword exactly
Match and match in elasticsearch_ phrase、query_ The difference between string and term
This is the only way to succeed

2)match_ Phrase matches the text field

match_ Phrase is participle and text is participle. match_ The word segmentation results of phrase must be included in the word segmentation of text field, and the order must be the same and must be continuous

Match and match in elasticsearch_ phrase、query_ The difference between string and term

If it is not continuous, it will fail

Match and match in elasticsearch_ phrase、query_ The difference between string and term

4、query_string

1)query_ String to query the fields of key type. All fields must match

Match and match in elasticsearch_ phrase、query_ The difference between string and term

2)query_ String queries a field of type text

And match_ The difference between phrase and query_ String queries text type fields, which do not need to be continuous, and the order can be changed

Match and match in elasticsearch_ phrase、query_ The difference between string and term

Match and match in elasticsearch_ phrase、query_ The difference between string and term

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Detailed explanation of how to customize redis command with Lua

preface As a very successful database, redis provides a wealth of data types and commands. Using these, we can easily and efficiently complete many cache operations, but there are always some special problems or needs to be solved. At this time, we may need to customize our own redis data structure and commands. Redis command […]