william007
william007

Reputation: 18547

how do we use and query the keyword field?

When I do

PUT /vehicles/_doc/123
{
  "make" : "Honda Civic", 
  "color" : "Blue", 
  "from": "Japan",
  "size": "Big",
  "comment": "deja vu",
  "HP" : 250, 
  "milage" : 24000, 
  "price": 19300.97
}

It automatically generate the index definition below:

{
  "vehicles": {
    "aliases": {},
    "mappings": {
      "properties": {
        "HP": {
          "type": "long"
        },
        "color": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "comment": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "from": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "make": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "milage": {
          "type": "long"
        },
        "price": {
          "type": "float"
        },
        "size": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    },
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "1",
        "provided_name": "vehicles",
        "creation_date": "1670864230815",
        "number_of_replicas": "1",
        "uuid": "etLFicsvSXCpeuFiYCiT0g",
        "version": {
          "created": "8050299"
        }
      }
    }
  }
}

In the index, say color, it has type text, and there is a field keyword, how do we use and query the keyword field?

Upvotes: 0

Views: 72

Answers (1)

Amit
Amit

Reputation: 32386

You just need to use color.keyword in your query when you want to query the keyword field, if you want to just query the text part, you simply use the color in field name.

text and keyword fields are tokenised and stored differently and used in different scenario, this answer will be useful for understand the difference.

Upvotes: 1

Related Questions