0
0
ElasticsearchConceptBeginner · 3 min read

What is Keyword Type in Elasticsearch: Explanation and Example

In Elasticsearch, the keyword type is used to store structured content such as IDs, tags, or keywords that should be searchable but not analyzed. It treats the entire field value as a single token, allowing exact matches and aggregations.
⚙️

How It Works

The keyword type in Elasticsearch stores data exactly as it is without breaking it into smaller parts. Imagine you have a list of tags or IDs; you want to find exact matches or count how many times each tag appears. The keyword type treats the whole value as one piece, like a single word, so it doesn't split or change it.

This is different from text fields, which analyze and break down content into words for full-text search. Keyword fields are perfect when you want to filter, sort, or aggregate data based on exact values, like filtering users by country code or grouping products by category.

💻

Example

This example shows how to define a keyword field in an Elasticsearch index mapping and how to search for an exact match.

json
{
  "mappings": {
    "properties": {
      "status": {
        "type": "keyword"
      }
    }
  }
}

POST /my_index/_doc
{
  "status": "active"
}

GET /my_index/_search
{
  "query": {
    "term": {
      "status": {
        "value": "active"
      }
    }
  }
}
Output
{ "hits": { "total": 1, "hits": [ { "_source": { "status": "active" } } ] } }
🎯

When to Use

Use the keyword type when you need to store data for exact matching, sorting, or aggregations. Common use cases include storing IDs, tags, email addresses, or status codes where you want to filter or group results precisely.

For example, if you want to filter users by country code or group sales by product category, keyword fields are ideal because they keep the data intact and searchable as whole values.

Key Points

  • Keyword fields store exact values without analysis.
  • They are used for filtering, sorting, and aggregations.
  • Not suitable for full-text search where word matching is needed.
  • Commonly used for IDs, tags, and codes.

Key Takeaways

The keyword type stores data exactly as is for exact match searches.
Use keyword fields for filtering, sorting, and aggregations on structured data.
Keyword fields do not analyze or break down text into tokens.
Ideal for IDs, tags, status codes, and other exact values.