Text vs keyword field types in Elasticsearch - Performance Comparison
When searching in Elasticsearch, the type of field affects how fast queries run.
We want to know how query time changes with data size for text and keyword fields.
Analyze the time complexity of these query examples:
GET /my_index/_search
{
"query": {
"match": { "description": "fast car" }
}
}
GET /my_index/_search
{
"query": {
"term": { "status.keyword": "active" }
}
}
The first searches a text field with full-text search. The second searches a keyword field with exact match.
Look at what repeats when searching:
- Primary operation: For text, scanning inverted index terms and scoring many matches.
- How many times: Depends on how many documents contain matching words.
- For keyword: Direct lookup in a hash-like structure for exact matches.
- How many times: Usually one quick lookup per query.
As data grows, text search scans more matches, keyword search stays fast.
| Input Size (n) | Approx. Operations for Text | Approx. Operations for Keyword |
|---|---|---|
| 10 | Few matches, quick scan | One quick lookup |
| 100 | More matches, longer scan | Still one quick lookup |
| 1000 | Many matches, longer scan | One quick lookup |
Pattern observation: Text search work grows with matching documents; keyword search work stays almost constant.
Time Complexity: O(m) for text, O(1) for keyword
This means text search time grows with matches found, keyword search time stays constant regardless of data size.
[X] Wrong: "Text and keyword fields always take the same time to search."
[OK] Correct: Text fields need to scan many matches and score them, which takes more time as data grows. Keyword fields do exact lookups that stay fast.
Understanding how field types affect search speed shows you know how to design fast queries and indexes, a useful skill in real projects.
"What if we changed the text field to use keyword type with normalizers? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of text fields
Text fields are designed for full-text search, allowing analysis and tokenization of sentences.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.Final Answer:
text -> Option AQuick Check:
Full-text search = text [OK]
- Choosing keyword for full-text search
- Confusing integer or date types with text
- Thinking keyword fields tokenize text
Solution
Step 1: Recall Elasticsearch field types
Elasticsearch uses "keyword" as the type for exact match fields.Step 2: Identify the correct syntax
"keyword" is the correct type name; "text" is for analyzed fields, "string" and "fulltext" are invalid types.Final Answer:
"name": { "type": "keyword" } -> Option BQuick Check:
Keyword field syntax = "name": { "type": "keyword" } [OK]
- Using "string" instead of "keyword"
- Confusing "text" with "keyword"
- Using invalid type names like "fulltext"
{
"properties": {
"title": { "type": "text" },
"category": { "type": "keyword" }
}
}
Which query will correctly filter documents where category is exactly "books"?Solution
Step 1: Identify field types and query types
"category" is a keyword field, so exact match queries use "term" query.Step 2: Match query usage
"match" queries are for full-text search on text fields, not exact matches on keyword fields.Final Answer:
{ "term": { "category": "books" } } -> Option AQuick Check:
Exact filter on keyword = term query [OK]
- Using match query on keyword fields
- Filtering text fields with term query expecting full-text search
- Confusing field names in queries
{
"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?Solution
Step 1: Understand keyword field behavior
Keyword fields are not analyzed and do not support partial matching with match query.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.Final Answer:
Because match query does not work on keyword fields for partial matches -> Option CQuick Check:
Match query + keyword field = no partial match [OK]
- Thinking keyword fields support partial match with match query
- Confusing field types for filtering
- Using match query on numeric or date fields
Solution
Step 1: Understand the need for both full-text search and exact filtering
Full-text search requires text type; exact filtering requires keyword type.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.Final Answer:
"tags": { "type": "text", "fields": { "raw": { "type": "keyword" } } } -> Option DQuick Check:
Multi-fields = text + keyword for search and filter [OK]
- Using only text type and expecting exact filters
- Using only keyword type and expecting full-text search
- Not defining subfields for multi-purpose queries
Ready to prove it?
Pass the test and this pattern moves unknown
