Elasticsearch Understanding Elasticsearch Data Types: A Deep Dive

By Opster Team

Updated: Jul 23, 2023

| 2 min read

Introduction 

Elasticsearch, a highly scalable open-source full-text search and analytics engine, is known for its flexibility and diverse functionality. One of the key aspects that contribute to its versatility is the wide range of data types it supports. This article will delve into the intricacies of Elasticsearch data types, providing a comprehensive understanding of their usage and benefits. If you want to learn about Elasticsearch object fields VS. nested field types, check out this guide. 

Core Data Types

Elasticsearch supports a variety of core data types, each designed to handle specific kinds of data.

Text and Keyword

Text data types are designed for full-text search. They are analyzed, meaning they are broken down into separate words (or tokens) during indexing. This makes them ideal for running full-text queries.

On the other hand, keyword data types are used for exact value searches. They are not analyzed and are used as they are. This makes them suitable for sorting, aggregating, or filtering.

Example:

json
{
  "properties": {
    "name": { "type": "text" },
    "tag": { "type": "keyword" }
  }
}

Numeric

Elasticsearch supports five numeric data types: long, unsigned_long, integer, short, and byte. These are used for whole numbers of varying sizes. For decimal numbers, it provides four data types: double,float, half_float and scaled_float.

Example:

json
{
  "properties": {
    "age": { "type": "integer" },
    "height": { "type": "double" }
  }
}

Date

The date and date_nanos data types are used for dates and times. Elasticsearch can handle date values in many different formats, making it highly flexible for time-based data. The date_nanos data type stores dates with nanoseconds precision from 1970 to 2262.

Example:

json
{
  "properties": {
    "timestamp": { "type": "date" },
    "timestamp_ns": { "type": "date_nanos" }
  }
}

Boolean

The boolean data type is used for true/false values.

Example:

json
{
  "properties": {
    "is_active": { "type": "boolean" }
  }
}

Complex Data Types

Elasticsearch also supports complex data types for handling more sophisticated data structures.

Object

The object data type is used for JSON objects. It allows for nested fields within a document.

Example:

json
{
  "properties": {
    "user": { 
      "type": "object",
      "properties": {
        "name": { "type": "text" },
        "age": { "type": "integer" }
      }
    }
  }
}

Array

Elasticsearch can handle arrays of any data type. There is no special array data type; any field can contain zero or more values by default.

Example:

json
{
  "tags": ["elasticsearch", "data", "types"]
}

Nested

The nested data type is a specialized version of the object data type. It allows arrays of objects to be indexed and queried independently of each other.

Example:

json
{
  "properties": {
    "users": { 
      "type": "nested",
      "properties": {
        "name": { "type": "text" },
        "age": { "type": "integer" }
      }
    }
  }
}

Geo Data Types

Elasticsearch provides geo data types for handling geographical data. These include geo_point for lat/lon points and geo_shape for complex shapes like polygons.

Example:

json
{
  "properties": {
    "location": { "type": "geo_point" }
  }
}

Conclusion 

In conclusion, understanding Elasticsearch data types is crucial for effective data modeling and search optimization. By choosing the right data type for each field, you can ensure that your data is stored, indexed, and queried in the most efficient way possible.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?