0
0
ElasticsearchComparisonIntermediate · 4 min read

Nested vs Object Type in Elasticsearch: Key Differences and Usage

In Elasticsearch, object type stores JSON objects as flattened fields, which can cause incorrect matches when querying arrays of objects. The nested type stores each object in an array as a separate hidden document, preserving relationships and enabling accurate queries on nested fields.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of nested and object types in Elasticsearch.

Featureobject typenested type
Data storageFlattens objects into fieldsStores each nested object as separate hidden document
Query behaviorMay cause false matches in arraysPreserves object relationships for accurate queries
PerformanceFaster indexing and queryingSlower due to additional joins internally
Use caseSimple JSON objects without arrays of objectsArrays of objects where relationships matter
Supports inner hitsNoYes, can return nested matching objects
Mapping complexitySimplerMore complex mapping and queries
⚖️

Key Differences

The object type in Elasticsearch stores JSON objects by flattening their fields into the parent document. This means that if you have an array of objects, all fields from all objects are merged, which can lead to incorrect query results because Elasticsearch cannot distinguish which fields belong together.

On the other hand, the nested type stores each object in an array as a separate hidden document linked to the parent. This preserves the relationship between fields inside each object, allowing queries to correctly match nested objects as a unit.

While nested types provide more accurate querying for arrays of objects, they add complexity and overhead because Elasticsearch performs internal joins to match nested documents. The object type is simpler and faster but only suitable when you don't need to query individual objects inside arrays precisely.

⚖️

Code Comparison

Example mapping and query using object type for an array of objects.

json
{
  "mappings": {
    "properties": {
      "user": {
        "properties": {
          "name": { "type": "text" },
          "skills": {
            "type": "object",
            "properties": {
              "name": { "type": "text" },
              "level": { "type": "keyword" }
            }
          }
        }
      }
    }
  }
}

// Query to find users with skill name 'java' and level 'senior'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "user.skills.name": "java" } },
        { "match": { "user.skills.level": "senior" } }
      ]
    }
  }
}
Output
May return users who have 'java' skill and 'senior' level but not necessarily in the same skill object, causing false positives.
↔️

Nested Equivalent

Equivalent mapping and query using nested type to correctly query arrays of objects.

json
{
  "mappings": {
    "properties": {
      "user": {
        "properties": {
          "name": { "type": "text" },
          "skills": {
            "type": "nested",
            "properties": {
              "name": { "type": "text" },
              "level": { "type": "keyword" }
            }
          }
        }
      }
    }
  }
}

// Query to find users with skill name 'java' and level 'senior' in the same nested object
{
  "query": {
    "nested": {
      "path": "user.skills",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.skills.name": "java" } },
            { "match": { "user.skills.level": "senior" } }
          ]
        }
      },
      "inner_hits": {}
    }
  }
}
Output
Returns only users who have a skill object where name is 'java' and level is 'senior' together, avoiding false matches.
🎯

When to Use Which

Choose object type when your JSON objects are simple and you don't have arrays of objects that require precise matching. It is faster and easier to manage.

Choose nested type when you have arrays of objects and need to query or filter based on the relationship between fields inside each object. This ensures accurate results but with some performance cost.

In summary, use nested for complex, relational JSON data and object for simpler, flat structures.

Key Takeaways

Use nested type to preserve relationships in arrays of objects for accurate queries.
The object type flattens fields and can cause false matches in arrays.
Nested queries are more complex and slower but necessary for precise matching.
Choose object type for simple JSON without nested arrays.
Use inner_hits with nested to retrieve matching nested objects.