_parent field

edit

A parent-child relationship can be established between documents in the same index by making one mapping type the parent of another:

PUT my_index
{
  "mappings": {
    "my_parent": {},
    "my_child": {
      "_parent": {
        "type": "my_parent" 
      }
    }
  }
}

PUT my_index/my_parent/1 
{
  "text": "This is a parent document"
}

PUT my_index/my_child/2?parent=1 
{
  "text": "This is a child document"
}

PUT my_index/my_child/3?parent=1&refresh=true 
{
  "text": "This is another child document"
}

GET my_index/my_parent/_search
{
  "query": {
    "has_child": { 
      "type": "my_child",
      "query": {
        "match": {
          "text": "child document"
        }
      }
    }
  }
}

The my_parent type is parent to the my_child type.

Index a parent document.

Index two child documents, specifying the parent document’s ID.

Find all parent documents that have children which match the query.

See the has_child and has_parent queries, the children aggregation, and inner hits for more information.

An additional field that contains the parent _id that the document links to if the document is a child (my_child) and the _id of document if it’s a parent (my_parent) is created in the index. The value of this field is accessible in aggregations and scripts through its entire name (_parent#parent_name) and may be queried with the parent_id query directly:

GET my_index/_search
{
  "query": {
    "parent_id": { 
      "type": "my_child",
      "id": "1"
    }
  },
  "aggs": {
    "parents": {
      "terms": {
        "field": "_parent#my_parent", 
        "size": 10
      }
    }
  },
  "script_fields": {
    "parent": {
      "script": {
         "source": "doc['_parent#my_parent']" 
      }
    }
  }
}

Querying the id of the _parent field (also see the has_parent query and the has_child query)

Aggregating on the _parent#my_parent field (also see the children aggregation)

Accessing the _parent#my_parent field in scripts

Parent-child restrictions

edit
  • The parent and child types must be different — parent-child relationships cannot be established between documents of the same type.
  • The _parent.type setting can only point to a type that doesn’t exist yet. This means that a type cannot become a parent type after it has been created.
  • Parent and child documents must be indexed on the same shard. The parent ID is used as the routing value for the child, to ensure that the child is indexed on the same shard as the parent. This means that the same parent value needs to be provided when getting, deleting, or updating a child document.

Global ordinals

edit

Parent-child uses global ordinals to speed up joins. Global ordinals need to be rebuilt after any change to a shard. The more parent id values are stored in a shard, the longer it takes to rebuild the global ordinals for the _parent field.

Global ordinals, by default, are built eagerly: if the index has changed, global ordinals for the _parent field will be rebuilt as part of the refresh. This can add significant time the refresh. However most of the times this is the right trade-off, otherwise global ordinals are rebuilt when the first parent-child query or aggregation is used. This can introduce a significant latency spike for your users and usually this is worse as multiple global ordinals for the _parent field may be attempt rebuilt within a single refresh interval when many writes are occurring.

When the parent/child is used infrequently and writes occur frequently it may make sense to disable eager loading:

PUT my_index
{
  "mappings": {
    "my_parent": {},
    "my_child": {
      "_parent": {
        "type": "my_parent",
        "eager_global_ordinals": false
      }
    }
  }
}

The amount of heap used by global ordinals can be checked as follows:

# Per-index
GET _stats/fielddata?human&fields=_parent*

# Per-node per-index
GET _nodes/stats/indices/fielddata?human&fields=_parent*