Bird
Raised Fist0
Elasticsearchquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which Elasticsearch field type should you use if you want to perform full-text search inside sentences?
easy
A. text
B. keyword
C. integer
D. date

Solution

  1. Step 1: Understand the purpose of text fields

    Text fields are designed for full-text search, allowing analysis and tokenization of sentences.
  2. Step 2: Compare with keyword fields

    Keyword fields are for exact matches and do not analyze the text, so they are not suitable for searching inside sentences.
  3. Final Answer:

    text -> Option A
  4. Quick Check:

    Full-text search = text [OK]
Hint: Full-text search needs analyzed text fields [OK]
Common Mistakes:
  • Choosing keyword for full-text search
  • Confusing integer or date types with text
  • Thinking keyword fields tokenize text
2. Which of the following is the correct way to define a keyword field in an Elasticsearch mapping?
easy
A. "name": { "type": "text" }
B. "name": { "type": "keyword" }
C. "name": { "type": "string" }
D. "name": { "type": "fulltext" }

Solution

  1. Step 1: Recall Elasticsearch field types

    Elasticsearch uses "keyword" as the type for exact match fields.
  2. Step 2: Identify the correct syntax

    "keyword" is the correct type name; "text" is for analyzed fields, "string" and "fulltext" are invalid types.
  3. Final Answer:

    "name": { "type": "keyword" } -> Option B
  4. Quick Check:

    Keyword field syntax = "name": { "type": "keyword" } [OK]
Hint: Keyword type is exactly "keyword" in mapping [OK]
Common Mistakes:
  • Using "string" instead of "keyword"
  • Confusing "text" with "keyword"
  • Using invalid type names like "fulltext"
3. Given this Elasticsearch mapping snippet:
{
  "properties": {
    "title": { "type": "text" },
    "category": { "type": "keyword" }
  }
}
Which query will correctly filter documents where category is exactly "books"?
medium
A. { "term": { "category": "books" } }
B. { "match": { "category": "books" } }
C. { "match": { "title": "books" } }
D. { "term": { "title": "books" } }

Solution

  1. Step 1: Identify field types and query types

    "category" is a keyword field, so exact match queries use "term" query.
  2. Step 2: Match query usage

    "match" queries are for full-text search on text fields, not exact matches on keyword fields.
  3. Final Answer:

    { "term": { "category": "books" } } -> Option A
  4. Quick Check:

    Exact filter on keyword = term query [OK]
Hint: Use term query for keyword fields exact match [OK]
Common Mistakes:
  • Using match query on keyword fields
  • Filtering text fields with term query expecting full-text search
  • Confusing field names in queries
4. You have this mapping:
{
  "properties": {
    "username": { "type": "keyword" },
    "bio": { "type": "text" }
  }
}
A developer tries to filter users with username containing "john" using this query:
{ "match": { "username": "john" } }
Why does this query not work as expected?
medium
A. Because match query only works on numeric fields
B. Because username field should be of type text for exact matches
C. Because match query does not work on keyword fields for partial matches
D. Because bio field should be used instead of username

Solution

  1. Step 1: Understand keyword field behavior

    Keyword fields are not analyzed and do not support partial matching with match query.
  2. Step 2: Understand match query limitations

    Match query analyzes input and is meant for text fields, not keyword fields which require term or wildcard queries for partial matches.
  3. Final Answer:

    Because match query does not work on keyword fields for partial matches -> Option C
  4. Quick Check:

    Match query + keyword field = no partial match [OK]
Hint: Match query needs text fields, not keyword [OK]
Common Mistakes:
  • Thinking keyword fields support partial match with match query
  • Confusing field types for filtering
  • Using match query on numeric or date fields
5. You want to store user tags in Elasticsearch so you can both filter by exact tag and search inside tag text. Which mapping setup is best?
hard
A. "tags": { "type": "text" }
B. "tags": { "type": "keyword" }
C. "tags": { "type": "keyword", "fields": { "analyzed": { "type": "text" } } }
D. "tags": { "type": "text", "fields": { "raw": { "type": "keyword" } } }

Solution

  1. Step 1: Understand the need for both full-text search and exact filtering

    Full-text search requires text type; exact filtering requires keyword type.
  2. Step 2: Use multi-fields to support both

    Mapping with "text" type and a subfield "raw" as "keyword" allows searching inside tags and filtering/sorting by exact tag.
  3. Final Answer:

    "tags": { "type": "text", "fields": { "raw": { "type": "keyword" } } } -> Option D
  4. Quick Check:

    Multi-fields = text + keyword for search and filter [OK]
Hint: Use multi-fields for both text search and exact filter [OK]
Common Mistakes:
  • Using only text type and expecting exact filters
  • Using only keyword type and expecting full-text search
  • Not defining subfields for multi-purpose queries
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard