0
0
Elasticsearchquery~30 mins

Object and nested types in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(book_doc) to display the document. The output should show the full nested structure.