0
0
Elasticsearchquery~5 mins

Numeric field types in Elasticsearch - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Numeric field types
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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

As the number of documents grows, Elasticsearch must check more prices to find matches.

Input Size (n)Approx. Operations
1010 checks
100100 checks
10001000 checks

Pattern observation: The number of checks grows directly with the number of documents.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the range query grows linearly with the number of documents.

Common Mistake

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

Interview Connect

Understanding how numeric field queries scale helps you explain search performance clearly and shows you know how data size affects response time.

Self-Check

What if we added an index on the "price" field? How would the time complexity of the range query change?