Problem description

In Elasticsearch, insert a record string that is specified as text by default when mapping is not set. Text is segmented by Elasticsearch. For example, if you insert a record with the name (WXX WXX), then when you retrieve (name: WXX), the record will be retrieved; For example, if you insert the address of the record as (Address: Fuzhou city, Jiangxi Province), then when you search according to the address (Address: Fuzhou City, Jiangxi Province), you will find that you can not query the record, this is because your address field default segmentation retrieval. These two cases, a query more, a record is not detected, are caused by segmentation.

Case description

// Insert test data 1
POST index3/_doc
{
  "name": "wxx wxx"."address": "Fuzhou, Jiangxi"."age": 28."hobbby": [
    "Work"]."sex": "man"
}
// Insert test data 2
POST index3/_doc
{
  "name": "wxx"."address": "Fuzhou, Jiangxi"."age": 28."hobbby": [
    "Work"]."sex": "man"
}

// query statement -- retrieve name= WXX and find both
GET index3/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "name": "wxx"
        }
      }
    }
  }
}
Address = address= fuzhou, Jiangxi
GET index3/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "address": "Fuzhou, Jiangxi"
        }
      }
    }
  }
}
Copy the code

Confused answer

// parsing statements
POST index3/_analyze
{
  "field": "name"."text": "wxx wxx"
}

POST index3/_analyze
{
  "field": "address"."text": "Fuzhou, Jiangxi"
}
Copy the code

Note: Through word segmentation, we found that, by default, only the data associated with the word inversion index is retrieved

// parsing statements
POST index3/_analyze
{
  "field": "name.keyword"."text": "wxx wxx"
}

POST index3/_analyze
{
  "field": "address.keyword"."text": "Fuzhou, Jiangxi"
}
Copy the code

Note: Instead of detecting with the default inverted index, we associate the corresponding data as a keyword(supported by newer versions)