Learn advanced CRUD and search queries for nested objects in Elasticsearch from practical examples
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
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:
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: