0
0
Elasticsearchquery~10 mins

Text vs keyword field types in Elasticsearch - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Text vs keyword field types
Input Document
Field Type?
text
Analyzed
Tokenized
Documents have fields that are either 'text' or 'keyword'. Text fields are analyzed and tokenized for full-text search. Keyword fields are stored as-is for exact matching and sorting.
Execution Sample
Elasticsearch
PUT /my_index
{
  "mappings": {
    "properties": {
      "title": {"type": "text"},
      "status": {"type": "keyword"}
    }
  }
}
Defines an index with 'title' as a text field for full-text search and 'status' as a keyword field for exact matching.
Execution Table
StepFieldTypeAnalyzed?Stored AsUse Case
1titletextYesTokens (words)Full-text search, relevance scoring
2statuskeywordNoExact valueFiltering, sorting, aggregations
3Search 'title:quick fox'titleAnalyzed queryMatches tokens 'quick' and 'fox'Find documents with those words
4Filter 'status:active'statusExact matchMatches exact 'active'Filter documents with status active
5Sort by 'status'statusExact valueSorts documents alphabetically by statusOrdering results
6Exit---No more steps, mapping and usage clear
💡 All fields processed with their types and usage explained.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
titleundefinedtext type setanalyzed tokens createdtokens matched in searchused for full-text search
statusundefinedkeyword type setstored as exact valueexact match filter appliedused for filtering and sorting
Key Moments - 3 Insights
Why is the 'text' field analyzed but the 'keyword' field is not?
Because 'text' fields are meant for full-text search, they are broken into tokens (words) to match parts of the text. 'Keyword' fields store exact values for filtering and sorting, so they are not broken down. See execution_table rows 1 and 2.
Can you use a 'keyword' field for full-text search?
No, 'keyword' fields are stored as exact values and do not support tokenization or relevance scoring. Full-text search requires 'text' fields. Refer to execution_table row 3 for how 'title' is searched.
Why do we sort on 'keyword' fields and not on 'text' fields?
'Keyword' fields store exact values, so sorting is straightforward. 'Text' fields are tokenized and analyzed, making sorting ambiguous. See execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the 'status' field stored as after step 2?
AExact value
BAnalyzed tokens
CLowercase words
DStemmed words
💡 Hint
Check execution_table row 2 under 'Stored As' column.
At which step does the 'title' field get tokenized for search?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at execution_table row 3 describing search on 'title'.
If you want to filter documents by exact status, which field type should you use?
Atext
Bkeyword
Cinteger
Ddate
💡 Hint
Refer to execution_table rows 2 and 4 about filtering on 'status'.
Concept Snapshot
Text vs Keyword fields in Elasticsearch:
- text: analyzed, tokenized for full-text search
- keyword: not analyzed, stored as exact value
- Use text for searching words inside text
- Use keyword for exact match, filtering, sorting
- Mapping defines field type for proper usage
Full Transcript
In Elasticsearch, fields can be either 'text' or 'keyword' types. Text fields are analyzed and broken into tokens to support full-text search, allowing you to find documents by words inside the text. Keyword fields are stored as exact values without analysis, making them perfect for filtering, sorting, and aggregations. For example, a 'title' field as text lets you search for words like 'quick' or 'fox', while a 'status' field as keyword lets you filter documents exactly matching 'active'. Understanding these types helps you design your index for efficient search and filtering.