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?"