Parent-Child Relationshipedit

The parent-child relationship is similar in nature to the nested model: both allow you to associate one entity with another. The difference is that, with nested objects, all entities live within the same document while, with parent-child, the parent and children are completely separate documents.

The parent-child functionality allows you to associate one document type with another, in a one-to-many relationship—​one parent to many children. The advantages that parent-child has over nested objects are as follows:

  • The parent document can be updated without reindexing the children.
  • Child documents can be added, changed, or deleted without affecting either the parent or other children. This is especially useful when child documents are large in number and need to be added or changed frequently.
  • Child documents can be returned as the results of a search request.

Elasticsearch maintains a map of which parents are associated with which children. It is thanks to this map that query-time joins are fast, but it does place a limitation on the parent-child relationship: the parent document and all of its children must live on the same shard.

At the time of going to press, the parent-child ID map is held in memory as part of fielddata. There are plans afoot to change the default setting to use doc values by default instead.