Working with Object and Nested Types in Elasticsearch
📖 Scenario: You are building a search index for an online bookstore. Each book has details like title, author, and a list of reviews. Each review has a reviewer name and a rating. You want to store this data in Elasticsearch using object and nested types to keep the reviews structured.
🎯 Goal: Create an Elasticsearch index mapping that uses object type for the author details and nested type for the list of reviews. Then, add a sample document with one book, its author, and two reviews.
📋 What You'll Learn
Create an index mapping named books with author as an object type
Define reviews as a nested type with fields reviewer (text) and rating (integer)
Index a sample document with title, author details, and two reviews
Print the indexed document to verify the structure
💡 Why This Matters
🌍 Real World
Online bookstores and review platforms often store complex data with nested relationships. Using object and nested types in Elasticsearch helps keep this data organized and searchable.
💼 Career
Understanding how to model nested data in Elasticsearch is important for roles like backend developers, data engineers, and search specialists who build scalable search applications.
Progress0 / 4 steps
1
Create the index mapping with object and nested types
Create an index mapping called books with a properties field. Inside properties, define author as an object type with fields name (text) and birth_year (integer). Also, define reviews as a nested type with fields reviewer (text) and rating (integer). Write the JSON mapping exactly as shown.
Elasticsearch
Hint
Remember to nest the name and birth_year fields inside the author object, and the reviewer and rating fields inside the reviews nested type.
2
Add a sample book document with author and reviews
Create a JSON document called book_doc with these exact fields: title as "The Great Adventure", author as an object with name "John Smith" and birth_year 1975, and reviews as a list of two nested objects: first with reviewer "Alice" and rating 5, second with reviewer "Bob" and rating 4.
Elasticsearch
Hint
Make sure the author is an object with the exact fields, and reviews is a list of nested objects with the correct reviewer names and ratings.
3
Index the sample document into the books index
Write a command to index the book_doc document into the books index using the Elasticsearch Python client syntax: es.index(index="books", document=book_doc). Assign the result to a variable called response.
Elasticsearch
Hint
Use the es.index method with the index parameter set to "books" and the document parameter set to book_doc. Assign the result to response.
4
Print the indexed document to verify
Write a print statement to display the book_doc variable so you can see the full document structure.
Elasticsearch
Hint
Use print(book_doc) to display the document. The output should show the full nested structure.
Practice
(1/5)
1. What is the main difference between object and nested types in Elasticsearch?
easy
A. Both types store data the same way but nested type is faster.
B. Object type stores arrays of objects separately, while nested type stores grouped fields as one JSON object.
C. Nested type is used only for numeric fields, object type for text fields.
D. Nested type stores arrays of objects separately to keep their fields linked during searches, while object type stores grouped fields as one JSON object.
Solution
Step 1: Understand object type storage
Object type stores grouped fields as a single JSON object inside a document, meaning all fields are flattened and stored together.
Step 2: Understand nested type storage
Nested type stores arrays of objects separately to maintain the relationship between fields within each object during searches.
Final Answer:
Nested type stores arrays of objects separately to keep their fields linked during searches, while object type stores grouped fields as one JSON object. -> Option D
But your nested query returns no results even though documents have matching tags. What is the likely mistake?
medium
A. You forgot to use a nested query; used a regular bool query instead.
B. You defined tags as object type instead of nested.
C. You used match query on keyword field instead of term query.
D. You indexed tags as text instead of keyword.
Solution
Step 1: Check mapping correctness
The mapping correctly defines "tags" as nested with keyword "name" field, so mapping is fine.
Step 2: Identify query mistake
If nested query returns no results, often the cause is using a regular bool query instead of a nested query to search nested fields, losing the link between nested objects.
Final Answer:
You forgot to use a nested query; used a regular bool query instead. -> Option A
Quick Check:
Nested fields require nested queries [OK]
Hint: Always use nested query for nested fields [OK]
Common Mistakes:
Using bool query without nested for nested fields
Confusing object and nested types in mapping
Using match query on keyword fields
5. You have documents with a nested field orders containing product and quantity. You want to find documents where there is an order with product "apple" and quantity greater than 10, but also an order with product "banana" and quantity less than 5. Which query structure correctly finds such documents?