0
0
Elasticsearchquery~5 mins

Text vs keyword field types in Elasticsearch - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Text vs keyword field types
O(m) for text, O(1) for keyword
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As data grows, text search scans more matches, keyword search stays fast.

Input Size (n)Approx. Operations for TextApprox. Operations for Keyword
10Few matches, quick scanOne quick lookup
100More matches, longer scanStill one quick lookup
1000Many matches, longer scanOne quick lookup

Pattern observation: Text search work grows with matching documents; keyword search work stays almost constant.

Final Time Complexity

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.

Common Mistake

[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.

Interview Connect

Understanding how field types affect search speed shows you know how to design fast queries and indexes, a useful skill in real projects.

Self-Check

"What if we changed the text field to use keyword type with normalizers? How would the time complexity change?"