Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

Mapping the book fieldsStep 1 of 4
  1. 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.

quick drill

How does the standard analyzer index the title "Night Train to Pune"?

quick drill

Which type suits an order status like SHIPPED?

quick drill

author is a keyword field holding "Mira Das". Which exact-value filter matches?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainChoose the string types

For the book fields title, isbn, genre and blurb, choose text, keyword or both, and explain each choice using what shoppers and reports do with the field.