0
0
Elasticsearchquery~5 mins

Object and nested types in Elasticsearch

Choose your learning style9 modes available
Introduction

Object and nested types help you store and search complex data with multiple layers inside Elasticsearch.

When you want to store a group of related fields together, like an address with street, city, and zip code.
When you have lists of objects, like multiple comments on a blog post, and want to search each comment separately.
When you need to keep the relationship between fields inside an object, so searches don't mix data from different objects.
When you want to organize data clearly to make queries easier and more accurate.
Syntax
Elasticsearch
PUT /my_index
{
  "mappings": {
    "properties": {
      "user": {
        "type": "object",
        "properties": {
          "name": { "type": "text" },
          "age": { "type": "integer" }
        }
      },
      "comments": {
        "type": "nested",
        "properties": {
          "author": { "type": "text" },
          "message": { "type": "text" }
        }
      }
    }
  }
}

Object type stores data as a single JSON object inside a document.

Nested type stores arrays of objects separately to keep each object's fields linked during searches.

Examples
This example creates an object type for a book with title and author fields.
Elasticsearch
PUT /library
{
  "mappings": {
    "properties": {
      "book": {
        "type": "object",
        "properties": {
          "title": { "type": "text" },
          "author": { "type": "text" }
        }
      }
    }
  }
}
This example uses nested type for comments so each comment is treated separately in searches.
Elasticsearch
PUT /blog
{
  "mappings": {
    "properties": {
      "comments": {
        "type": "nested",
        "properties": {
          "user": { "type": "text" },
          "comment": { "type": "text" }
        }
      }
    }
  }
}
Sample Program

This program creates an index with object and nested types, adds a document, and searches for comments by author 'Bob'.

Elasticsearch
PUT /my_index
{
  "mappings": {
    "properties": {
      "user": {
        "type": "object",
        "properties": {
          "name": { "type": "text" },
          "age": { "type": "integer" }
        }
      },
      "comments": {
        "type": "nested",
        "properties": {
          "author": { "type": "text" },
          "message": { "type": "text" }
        }
      }
    }
  }
}

POST /my_index/_doc/1
{
  "user": {
    "name": "Alice",
    "age": 30
  },
  "comments": [
    { "author": "Bob", "message": "Great post!" },
    { "author": "Carol", "message": "Thanks for sharing." }
  ]
}

GET /my_index/_search
{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "match": { "comments.author": "Bob" }
      }
    }
  }
}
OutputSuccess
Important Notes

Use object type for simple nested data without needing separate queries.

Use nested type when you want to search inside arrays of objects without mixing fields from different objects.

Remember to use nested queries when searching nested fields to get correct results.

Summary

Object type stores grouped fields as one JSON object inside a document.

Nested type stores arrays of objects separately to keep their fields linked during searches.

Use nested queries to search inside nested fields correctly.