Skip to the workspace
Pebellslearn by building
Data Types  ›  Lesson

Text vs Keyword — The Most Important Choice

Text 12 minText fields are split into lower-cased words for full-text search, while keyword fields keep the whole exact value for filtering, sorting and aggregating.

elasticsearch / Text vs Keyword — The Most Important Choice

Explore each scene at your own pace.

01 · The rule

The rule

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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.