0
0
Elasticsearchquery~5 mins

Boolean and binary types in Elasticsearch

Choose your learning style9 modes available
Introduction

Boolean and binary types help store simple true/false values and raw data in Elasticsearch. They make searching and filtering data easier and faster.

When you want to store yes/no or true/false answers, like if a user is active or not.
When you need to save small files or images as raw data inside Elasticsearch.
When filtering search results based on true/false conditions, like showing only verified users.
When you want to store flags or switches that control features in your data.
When you want to store encoded data that is not text, like encrypted information.
Syntax
Elasticsearch
PUT /my_index
{
  "mappings": {
    "properties": {
      "is_active": { "type": "boolean" },
      "file_data": { "type": "binary" }
    }
  }
}

The boolean type stores true or false values.

The binary type stores base64 encoded data, like files or images.

Examples
This example creates a field email_verified that stores true or false values.
Elasticsearch
PUT /users
{
  "mappings": {
    "properties": {
      "email_verified": { "type": "boolean" }
    }
  }
}
This example creates a field attachment to store raw file data encoded in base64.
Elasticsearch
PUT /documents
{
  "mappings": {
    "properties": {
      "attachment": { "type": "binary" }
    }
  }
}
Sample Program

This program creates an index with boolean and binary fields, adds a document with true and base64 data, then searches for documents where is_published is true.

Elasticsearch
PUT /example_index
{
  "mappings": {
    "properties": {
      "is_published": { "type": "boolean" },
      "image_data": { "type": "binary" }
    }
  }
}

POST /example_index/_doc/1
{
  "is_published": true,
  "image_data": "iVBORw0KGgoAAAANSUhEUgAAAAUA"
}

GET /example_index/_search
{
  "query": {
    "term": { "is_published": true }
  }
}
OutputSuccess
Important Notes

Boolean fields accept only true or false values, no other types.

Binary fields must be base64 encoded before storing.

Binary data is not searchable but can be retrieved and decoded.

Summary

Boolean type stores true/false values for easy filtering.

Binary type stores raw data encoded in base64, like files or images.

Use these types to organize and search your data efficiently in Elasticsearch.