Nested vs Object Type in Elasticsearch: Key Differences and Usage
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.
| Feature | object type | nested type |
|---|---|---|
| Data storage | Flattens objects into fields | Stores each nested object as separate hidden document |
| Query behavior | May cause false matches in arrays | Preserves object relationships for accurate queries |
| Performance | Faster indexing and querying | Slower due to additional joins internally |
| Use case | Simple JSON objects without arrays of objects | Arrays of objects where relationships matter |
| Supports inner hits | No | Yes, can return nested matching objects |
| Mapping complexity | Simpler | More 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.
{
"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" } }
]
}
}
}Nested Equivalent
Equivalent mapping and query using nested type to correctly query arrays of objects.
{
"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": {}
}
}
}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
nested type to preserve relationships in arrays of objects for accurate queries.object type flattens fields and can cause false matches in arrays.object type for simple JSON without nested arrays.inner_hits with nested to retrieve matching nested objects.