Boolean and binary types in Elasticsearch - Time & Space 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.
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.
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.
As the number of documents grows, Elasticsearch must check more Boolean values.
| 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 filter documents grows in a straight line as the number of documents increases.
[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.
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.
"What if we changed the Boolean filter to a binary field filter? How would the time complexity change?"
Practice
boolean type in Elasticsearch store?Solution
Step 1: Understand the boolean type purpose
The boolean type is designed to store values that represent true or false states.Step 2: Compare with other data types
Text strings, numbers, and base64 encoded files are stored in different types like text, float, or binary, not boolean.Final Answer:
True or false values -> Option AQuick Check:
Boolean = true/false [OK]
- Confusing boolean with text or number types
- Thinking boolean stores encoded files
- Assuming boolean can hold multiple values
binary field in an Elasticsearch mapping?Solution
Step 1: Identify the binary type syntax
In Elasticsearch, a binary field is defined with "type": "binary" inside the mapping.Step 2: Check other options
Options A, B, and D define other types (text, boolean, integer) which are not binary.Final Answer:
{ "my_field": {"type": "binary"} } -> Option CQuick Check:
Binary type uses "type": "binary" [OK]
- Using boolean or text instead of binary
- Missing quotes around type name
- Incorrect JSON structure
{
"mappings": {
"properties": {
"is_active": {"type": "boolean"},
"file_data": {"type": "binary"}
}
}
}
Document:
{
"is_active": true,
"file_data": "SGVsbG8gd29ybGQ="
}What will be the value of
is_active when retrieved?Solution
Step 1: Understand the boolean field value
The field "is_active" is boolean and set to true in the document.Step 2: Confirm retrieval value
When retrieved, the boolean field returns the stored true value, not the binary data or null.Final Answer:
true -> Option BQuick Check:
Boolean field returns true/false as stored [OK]
- Confusing binary field value with boolean
- Expecting base64 string for boolean field
- Assuming null if not explicitly queried
{
"properties": {
"flag": {"type": "boolean"},
"data": {"type": "binary"}
}
}Which of the following is a mistake that will cause an error when indexing a document?
Solution
Step 1: Check boolean field value types
The boolean field "flag" must be true or false, not strings like "yes".Step 2: Validate binary field values
The binary field "data" expects base64 encoded strings, which "dGVzdA==" is valid for.Final Answer:
{"flag": "yes", "data": "dGVzdA=="} -> Option AQuick Check:
Boolean must be true/false, not string [OK]
- Using string 'yes' or 'no' for boolean
- Passing non-base64 strings to binary fields
- Leaving binary field empty without quotes
binary type and also keep a flag if the user is verified using boolean. Which of the following mapping and document pairs is correct and efficient?Solution
Step 1: Check mapping types for fields
The profile picture should be stored as binary type with base64 data, and verified as boolean true/false.Step 2: Validate document values
{ "mappings": { "properties": { "profile_pic": {"type": "binary"}, "verified": {"type": "boolean"} } } } Document: { "profile_pic": "iVBORw0KGgoAAAANSUhEUgAAAAUA", "verified": true } uses binary type with base64 string and boolean true, which is correct and efficient.Step 3: Identify errors in other options
{ "mappings": { "properties": { "profile_pic": {"type": "text"}, "verified": {"type": "boolean"} } } } Document: { "profile_pic": "iVBORw0KGgoAAAANSUhEUgAAAAUA", "verified": "yes" } uses text for binary and string "yes" for boolean, which is incorrect.
{ "mappings": { "properties": { "profile_pic": {"type": "binary"}, "verified": {"type": "text"} } } } Document: { "profile_pic": "iVBORw0KGgoAAAANSUhEUgAAAAUA", "verified": false } uses text for boolean field.
{ "mappings": { "properties": { "profile_pic": {"type": "binary"}, "verified": {"type": "boolean"} } } } Document: { "profile_pic": "not_base64_data", "verified": true } has invalid base64 data for binary field.Final Answer:
Mapping and document with binary and boolean types correctly used -> Option DQuick Check:
Binary = base64, Boolean = true/false [OK]
- Using text type for binary data
- Passing non-base64 strings to binary fields
- Using strings like 'yes' for boolean fields
Ready to prove it?
Pass the test and this pattern moves unknown
