Text vs Keyword — The Most Important Choice
A shopper searches "garden" and should find "The Quiet Garden". A manager filters the report to genre fiction and should get exactly fiction, sorted by author. Both are strings, yet they need opposite treatment. That is the difference between text and keyword.
The rule
Text is chopped into words for searching; keyword is kept whole for matching.
When a text field is indexed, it goes through an analyzer. The standard analyzer splits on spaces and punctuation and lower-cases each piece:
"The Quiet Garden" → [ "the", "quiet", "garden" ]Those words (called terms) go into the index, so a search for garden, GARDEN or quiet garden all find the book, and results are ranked by relevance.
A keyword field is stored exactly as given, as one single term:
"Mira Das" → [ "Mira Das" ]It matches only the whole, exact, case-sensitive value — "Mira Das" matches, "mira das" and "Mira" do not. In return, keyword fields are cheap to sort, aggregate (count books per author) and filter.
| Need | Type |
|---|---|
| Search inside titles, descriptions, reviews | text |
| IDs, status codes, tags, genres, emails | keyword |
| Sort or count by the value | keyword |
When a field needs both, map it as text with a keyword sub-field (exactly what dynamic mapping does):
"title": {
"type": "text",
"fields": { "raw": { "type": "keyword" } }
}Search title, sort on title.raw.
The shortcut
Ask "would a person type part of it, or pick all of it?" Typing loose words into a search box → text. Picking an exact value from a list, or sorting by it → keyword.
title
Shoppers type words from it →
text, with a keyword sub-field for alphabetical sorting.
The trap: sorting or aggregating on a plain text field fails with an error, because text is stored as scattered words, not one sortable value.
Drills
Answer fast — trust the rule.
How does the standard analyzer index the title "Night Train to Pune"?
Which type suits an order status like SHIPPED?
author is a keyword field holding "Mira Das". Which exact-value filter matches?
Sorting books by a text field title fails. What is the usual fix?
Remember
- Text is chopped into words for searching; keyword is kept whole for matching.
- Typed in part → text; picked whole or sorted → keyword.
- Need both? Text with a keyword sub-field.
