Learn advanced CRUD and search queries for nested objects in Elasticsearch from practical examples

Elasticsearch can get complex but also powerful

Lynn Kwong
4 min readApr 26, 2021

--

Photo by Markus Winkler on Unsplash.

In this article, some advanced CRUD and search queries for nested objects in Elasticsearch will be introduced. We will focus on nested objects, which are advanced topics in Elasticsearch. You can run this command in Kibana to create the index which will be used later.

In this example, the attributes field is a nested field and contains the attribute_name and attribure_value fields as the sub-fields. The nested type is a specialized version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other. We should always create a mapping for a nested field as demonstrated in this example because Elasticsearch has no concept of inner objects and would flatten object hierarchies into a simple list of field names and values, which is normally not what we want.

To create a document with nested objects, run this query:

To check the document just created:

GET laptops-demo/_doc/1

It will be tedious if we need to add many documents manually. Luckily we have a bulk API that can index a large number of documents in bulk. You can download this JSON file which contains all the documents to be indexed. If you want to learn how to generate this JSON file and understand the meaning of the JSON file, you check All you need to know about using Elasticsearch in Python.

Let’s add all the documents in this JSON file to our index:

--

--

Lynn Kwong

I’m a Software Developer (https://superdataminer.com) keen on sharing thoughts, tutorials, and solutions for the best practice of software development.