0
0
ElasticsearchConceptBeginner · 3 min read

What is Nested Type in Elasticsearch: Explanation and Example

In Elasticsearch, a nested type allows you to index arrays of objects so each object is stored and queried independently. This helps keep related data together and avoids incorrect matches when searching inside arrays of objects.
⚙️

How It Works

Imagine you have a list of books, and each book has multiple authors. If you store authors as a simple array of objects, Elasticsearch might mix up authors when you search, matching parts of different authors together incorrectly. The nested type solves this by storing each author as a separate hidden document linked to the main book document.

This means when you search, Elasticsearch looks inside each nested object independently, like checking each author separately instead of mixing all authors into one big list. It keeps the data organized and queries accurate.

💻

Example

This example shows how to define a nested type for authors inside a book document and how to query it.

json
PUT /library
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "authors": {
        "type": "nested",
        "properties": {
          "name": { "type": "text" },
          "age": { "type": "integer" }
        }
      }
    }
  }
}

POST /library/_doc/1
{
  "title": "Elasticsearch Basics",
  "authors": [
    { "name": "Alice", "age": 30 },
    { "name": "Bob", "age": 40 }
  ]
}

GET /library/_search
{
  "query": {
    "nested": {
      "path": "authors",
      "query": {
        "bool": {
          "must": [
            { "match": { "authors.name": "Alice" } },
            { "range": { "authors.age": { "gte": 25 } } }
          ]
        }
      }
    }
  }
}
Output
{ "hits": { "total": { "value": 1, "relation": "eq" }, "hits": [ { "_id": "1", "_source": { "title": "Elasticsearch Basics", "authors": [ { "name": "Alice", "age": 30 }, { "name": "Bob", "age": 40 } ] } } ] } }
🎯

When to Use

Use the nested type when you have arrays of objects and need to keep each object’s data separate for accurate searching. For example, if you store products with multiple reviews, each review should be nested to avoid mixing review details.

This is important when you want to query conditions that apply to the same object inside the array, like finding authors older than 25 with a specific name, without mixing data from different authors.

Key Points

  • Nested type stores arrays of objects as separate hidden documents.
  • It keeps related data together and prevents incorrect matches in queries.
  • Use it when you need precise queries on objects inside arrays.
  • Queries on nested fields use the nested query with a path.

Key Takeaways

The nested type stores arrays of objects separately for accurate querying.
Use nested when you need to query conditions inside the same object in an array.
Nested queries require specifying the path to the nested field.
Without nested, Elasticsearch may mix data from different objects in arrays.
Nested type improves data organization and query precision in complex documents.