Difference between keyword and text in ElasticSearch

前端 未结 1 1213
日久生厌
日久生厌 2020-12-10 10:25

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

相关标签:
1条回答
  • 2020-12-10 11:04

    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

    0 讨论(0)
提交回复
热议问题