0
0
ElasticsearchConceptBeginner · 3 min read

What is Object Type in Elasticsearch: Explanation and Example

In Elasticsearch, the object type is used to store JSON objects as fields within a document. It allows you to index nested key-value pairs inside a single field, enabling structured data storage and search.
⚙️

How It Works

Think of the object type in Elasticsearch like a folder inside a filing cabinet. Instead of just storing a simple value like a name or number, it holds a set of related information grouped together. This group is a JSON object with keys and values, similar to a mini-record inside your main document.

When you index a document with an object field, Elasticsearch treats the keys inside that object as sub-fields. This means you can search, filter, or aggregate based on those nested keys just like normal fields. It helps keep related data organized and searchable without flattening everything into one big list.

💻

Example

This example shows how to define an object type field called address with sub-fields for street, city, and zipcode. Then it indexes a document with that structure.

json
PUT /my_index
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "address": {
        "type": "object",
        "properties": {
          "street": { "type": "text" },
          "city": { "type": "keyword" },
          "zipcode": { "type": "keyword" }
        }
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "name": "John Doe",
  "address": {
    "street": "123 Elm St",
    "city": "Springfield",
    "zipcode": "12345"
  }
}

GET /my_index/_search
{
  "query": {
    "match": { "address.city": "Springfield" }
  }
}
Output
{ "hits": { "total": { "value": 1, "relation": "eq" }, "hits": [ { "_index": "my_index", "_id": "1", "_source": { "name": "John Doe", "address": { "street": "123 Elm St", "city": "Springfield", "zipcode": "12345" } } } ] } }
🎯

When to Use

Use the object type when you want to store structured data inside a single field that has multiple related parts. For example, addresses, user profiles, or product specifications often have multiple attributes that belong together.

This type is helpful when you want to keep your data organized and searchable by parts of the object without creating separate documents or flattening all fields. It works well for moderate nesting but is not ideal for deeply nested or complex relationships, where the nested type might be better.

Key Points

  • The object type stores JSON objects as fields inside a document.
  • It allows searching and filtering on sub-fields within the object.
  • Good for grouping related data like addresses or profiles.
  • Not suitable for deeply nested or complex arrays of objects.

Key Takeaways

The object type stores grouped key-value pairs inside a single document field.
It enables searching on nested keys without flattening the data.
Use it for structured data like addresses or user details.
Avoid it for deeply nested arrays; consider nested type instead.