Numeric field types in Elasticsearch - Time & Space Complexity
When working with numeric fields in Elasticsearch, it is important to understand how the time to process queries grows as the amount of data increases.
We want to know how the cost of searching or indexing numeric data changes when we have more numbers stored.
Analyze the time complexity of the following Elasticsearch mapping and query.
PUT /products
{
"mappings": {
"properties": {
"price": { "type": "double" }
}
}
}
GET /products/_search
{
"query": {
"range": { "price": { "gte": 10, "lte": 100 } }
}
}
This code defines a numeric field "price" as a double type and runs a range query to find products priced between 10 and 100.
Look at what repeats when Elasticsearch processes this query.
- Primary operation: Checking each document's "price" field against the range condition.
- How many times: Once for each document in the index.
As the number of documents grows, Elasticsearch must check more prices to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of documents.
Time Complexity: O(n)
This means the time to run the range query grows linearly with the number of documents.
[X] Wrong: "Numeric fields make queries run instantly no matter how many documents there are."
[OK] Correct: Numeric fields help with storage and sorting, but queries still check many documents, so time grows with data size.
Understanding how numeric field queries scale helps you explain search performance clearly and shows you know how data size affects response time.
What if we added an index on the "price" field? How would the time complexity of the range query change?