Boolean Type in Elasticsearch: Definition and Usage
boolean type is used to store values that can only be true or false. It is a simple data type ideal for representing yes/no or on/off states in your documents.How It Works
The boolean type in Elasticsearch stores data as either true or false. Think of it like a light switch that can only be on or off. This type helps Elasticsearch quickly filter and search documents based on these two states.
When you define a field as boolean in your index mapping, Elasticsearch expects only these two values. It stores them efficiently, making queries on boolean fields very fast. This is useful when you want to check if a condition is met or not, such as whether a user is active or if a product is in stock.
Example
{
"mappings": {
"properties": {
"is_active": {
"type": "boolean"
}
}
}
}
POST /my_index/_doc/1
{
"is_active": true
}
GET /my_index/_search
{
"query": {
"term": {
"is_active": true
}
}
}When to Use
Use the boolean type when you need to store simple true/false information about your data. For example, you might track if a user account is verified, if a product is available, or if a feature is enabled.
This type is perfect for filtering search results quickly based on yes/no conditions. It helps keep your data clear and your queries efficient.
Key Points
- Boolean fields store only
trueorfalse. - They are efficient for filtering and querying.
- Ideal for flags like active status, availability, or toggles.
- Must be defined in the index mapping before indexing documents.