Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

Difference between keyword and text in ElasticSearch

Can someone explain the difference between keyword and text in ElasticSearch with an example?

question from:https://stackoverflow.com/questions/52845088/difference-between-keyword-and-text-in-elasticsearch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

keyword type: if you define a field to be of type keyword like this.

 PUT products
{
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "keyword"
        }
      }
    }
  }
}

Then when you make a search query on this field you have to insert the whole value (keyword search) so keyword field.

 POST products/_doc
{
  "name": "washing machine"
}

when you execute search like this:

 GET products/_search
{
  "query": {
    "match": {
      "name": "washing"
    }
  }
}

it will not match any docs. You have to search with the whole word "washing machine".

text type on the other hand is analyzed and you can search using tokens from the field value. a full text search in the whole value:

    PUT products
{
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "text"
        }
      }
    }
  }
}

and the search :

 GET products/_search
{
  "query": {
    "match": {
      "name": "washing"
    }
  }
}

will return a matching documents.

You can check this to more details keyword Vs. text


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...