Monday, 16 October 2023

What is the Elastic Search keyword data type?

The Keyword data type in Elasticsearch is a text field that is not analyzed. This means that Elasticsearch will store the text exactly as it is, without performing any analysis such as tokenization, stemming, or stop word removal.

Keyword fields are typically used for fields that need to be searched for exact matches, such as product IDs, email addresses, or URLs. They can also be used for fields that contain structured data, such as JSON or XML.

Here are some examples of keyword fields:

  • Product ID
  • Email address
  • URL
  • Hostname
  • Status code
  • ZIP code
  • Tag

Keyword fields can be used in conjunction with other field types, such as text fields, to create more complex search queries. For example, you could search for products that have a specific product ID and that also contain a certain keyword in their title.

Here is an example of a mapping that defines a keyword field:

JSON
{
  "properties": {
    "product_id": {
      "type": "keyword"
    }
  }
}

Once you have defined a keyword field, you can search for documents that contain that field using the term query. For example, the following query will find all products that have the product ID 1234567890:

JSON
{
  "term": {
    "product_id": "1234567890"
  }
}

Keyword fields can also be used in conjunction with other field types, such as text fields, to create more complex search queries. For example, the following query will find all products that have the product ID 1234567890 and that also contain the keyword shoes in their title:

JSON
{
  "bool": {
    "must": [
      {
        "term": {
          "product_id": "1234567890"
        }
      },
      {
        "match": {
          "title": "shoes"
        }
      }
    ]
  }
}

Keyword fields are a powerful tool that can be used to improve the accuracy and performance of your Elasticsearch searches.

No comments:

Post a Comment