0
0
Elasticsearchquery~5 mins

Boolean and binary types in Elasticsearch - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Boolean and binary types
O(n)
Understanding Time Complexity

When working with Boolean and binary types in Elasticsearch, it's important to understand how the time to process queries grows as data size increases.

We want to know how the cost of searching or filtering on these types changes when we have more documents.

Scenario Under Consideration

Analyze the time complexity of the following Elasticsearch query filtering on a Boolean field.


GET /my_index/_search
{
  "query": {
    "term": {
      "is_active": true
    }
  }
}
    

This query searches for documents where the Boolean field is_active is true.

Identify Repeating Operations

In this query, Elasticsearch checks each document's is_active field to see if it matches true.

  • Primary operation: Checking the Boolean field value for each document.
  • How many times: Once per document in the index.
How Execution Grows With Input

As the number of documents grows, Elasticsearch must check more Boolean values.

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 filter documents grows in a straight line as the number of documents increases.

Common Mistake

[X] Wrong: "Filtering on Boolean fields is instant no matter how many documents there are."

[OK] Correct: Even though Boolean fields have only two values, Elasticsearch uses inverted indexes to quickly find matching documents, so filtering is generally faster than checking each document individually.

Interview Connect

Understanding how simple filters like Boolean checks scale helps you explain how search engines handle large data efficiently and shows your grasp of query performance.

Self-Check

"What if we changed the Boolean filter to a binary field filter? How would the time complexity change?"