0
0
ElasticsearchConceptBeginner · 3 min read

Boolean Type in Elasticsearch: Definition and Usage

In Elasticsearch, the 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

This example shows how to define a boolean field in an Elasticsearch index mapping and how to index a document with a boolean value.
json
{
  "mappings": {
    "properties": {
      "is_active": {
        "type": "boolean"
      }
    }
  }
}

POST /my_index/_doc/1
{
  "is_active": true
}

GET /my_index/_search
{
  "query": {
    "term": {
      "is_active": true
    }
  }
}
Output
{ "hits": { "total": { "value": 1, "relation": "eq" }, "hits": [ { "_id": "1", "_source": { "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 true or false.
  • 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.

Key Takeaways

The boolean type stores true or false values in Elasticsearch documents.
It is used for simple yes/no or on/off data fields.
Boolean fields enable fast filtering and querying.
You must define boolean fields in your index mapping.
Use boolean type for flags like active status or feature toggles.