AI Hints
Elasticsearch · Lesson 42 of 120

text vs keyword

The big choice.

⏱ 2 min read▶ Video at 38:47🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

The most important mapping choice is text or keyword. Get it wrong, and your filters, sorting and search all break in confusing ways.

  • Most important choice
  • text
  • keyword

Two string types

Text is split into lower case words, so it's great for searching sentences. Keyword is stored exactly as one value. Use keyword for brands, categories, tags, IDs, and anything you filter, sort or count.

text

  • Split into wordsLowercasedFor search: match

keyword

  • One exact valueCase mattersFor filter, sort, aggregate

Same value, two types

Our dynamic products have category as text, plus category dot keyword. Match on the text field finds sleeping bags, in lower case. Term on the keyword field needs the exact value. Run it. Both count five, but only because we used each one the right way.

Dev Tools Consoletext
GET products/_count?filter_path=count
{ "query": { "match": { "category": "sleeping bags" } } }

GET products/_count?filter_path=count
{ "query": { "term": { "category.keyword": "Sleeping Bags" } } }
Output
# GET products/_count 200 OK
{"count": 5}

# GET products/_count 200 OK
{"count": 5}
Also uses _load
products

Summary

Text is for searching words, and keyword is for exact values, filters, sorting and aggregations. Next, having both at once.

Key takeaways
  • text = words
  • keyword = exact
  • Choose per field
Next up: Multi-fields — text and keyword together