📖 Scenario: You are working with an Elasticsearch index to store information about electronic devices. Each device has a boolean field indicating if it is currently in stock, and a binary field storing a small image thumbnail in base64 format.
🎯 Goal: Create an Elasticsearch mapping with in_stock as a boolean type and thumbnail as a binary type. Then add a document with these fields and finally retrieve and display the stored document.
📋 What You'll Learn
Create an index mapping with in_stock as boolean type
Add thumbnail field as binary type in the mapping
Index a document with in_stock set to true and a sample base64 string for thumbnail
Retrieve and print the stored document
💡 Why This Matters
🌍 Real World
Storing device stock status and small images efficiently in Elasticsearch for quick searching and retrieval.
💼 Career
Understanding how to use boolean and binary types in Elasticsearch is important for roles involving search engine management, data indexing, and backend development.
Progress0 / 4 steps
1
Create the index mapping with boolean and binary fields
Create an Elasticsearch index called devices with a mapping that has a properties object containing in_stock as a boolean type and thumbnail as a binary type.
Elasticsearch
Hint
Use the PUT method to create the index with the mappings property. Define in_stock as boolean and thumbnail as binary inside properties.
2
Index a document with boolean and binary values
Index a document into the devices index with in_stock set to true and thumbnail set to the base64 string "U29tZUJpbmFyeURhdGE=".
Elasticsearch
Hint
Use PUT /devices/_doc/1 to add a document with the exact fields and values.
3
Retrieve the stored document
Retrieve the document with ID 1 from the devices index using the GET method.
Elasticsearch
Hint
Use the GET method with the document ID to retrieve it.
4
Display the retrieved document
Print the retrieved document's _source field showing in_stock and thumbnail values.
Elasticsearch
Hint
The retrieved document's _source field contains the stored boolean and binary values.
Practice
(1/5)
1. What type of data does the boolean type in Elasticsearch store?
easy
A. True or false values
B. Text strings
C. Numbers with decimals
D. Base64 encoded files
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 A
Quick Check:
Boolean = true/false [OK]
Hint: Boolean means true or false only [OK]
Common Mistakes:
Confusing boolean with text or number types
Thinking boolean stores encoded files
Assuming boolean can hold multiple values
2. Which of the following is the correct way to define a binary field in an Elasticsearch mapping?
easy
A. {
"my_field": {"type": "text"}
}
B. {
"my_field": {"type": "boolean"}
}
C. {
"my_field": {"type": "binary"}
}
D. {
"my_field": {"type": "integer"}
}
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 C
Quick Check:
Binary type uses "type": "binary" [OK]
Hint: Binary type always uses "type": "binary" in mapping [OK]
Common Mistakes:
Using boolean or text instead of binary
Missing quotes around type name
Incorrect JSON structure
3. Given this Elasticsearch document mapping and data:
Which of the following is a mistake that will cause an error when indexing a document?
medium
A. {"flag": "yes", "data": "dGVzdA=="}
B. {"flag": true, "data": "dGVzdA=="}
C. {"flag": false, "data": ""}
D. {"flag": false, "data": "dGVzdA=="}
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 A
Quick Check:
Boolean must be true/false, not string [OK]
Hint: Boolean fields accept only true or false, not strings [OK]
Common Mistakes:
Using string 'yes' or 'no' for boolean
Passing non-base64 strings to binary fields
Leaving binary field empty without quotes
5. You want to store user profile pictures in Elasticsearch using the 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?