join datatypeedit

The join datatype is a special field that creates parent/child relation within documents of the same index. The relations section defines a set of possible relations within the documents, each relation being a parent name and a child name. A parent/child relation can be defined as follows:

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "my_join_field": { 
          "type": "join",
          "relations": {
            "my_parent": "my_child" 
          }
        }
      }
    }
  }
}

The name for the field

Defines a single relation where my_parent is parent of my_child.

To index a document with a join, the name of the relation and the optional parent of the document must be provided in the source. For instance the following creates two parent documents in the my_parent context:

PUT my_index/doc/1?refresh
{
  "text": "This is a parent document",
  "my_join_field": "my_parent" 
}

PUT my_index/doc/2?refresh
{
  "text": "This is a another parent document",
  "my_join_field": "my_parent"
}

This document is a my_parent document.

When indexing a child, the name of the relation as well as the parent id of the document must be added in the _source.

Warning

It is required to index the lineage of a parent in the same shard so you must always route child documents using their greater parent id.

For instance the following index two children documents pointing to the same parent 1 with a `routing value equals to the id of the parent:

PUT my_index/doc/3?routing=1&refresh 
{
  "text": "This is a child document",
  "my_join_field": {
    "name": "my_child", 
    "parent": "1" 
  }
}

PUT my_index/doc/4?routing=1&refresh
{
  "text": "This is a another child document",
  "my_join_field": {
    "name": "my_child",
    "parent": "1"
  }
}

This child document must be on the same shard than its parent

my_child is the name of the join for this document

The parent id of this child document

Parent-join restrictionsedit

  • Only one join field is allowed per index mapping.
  • Parent and child documents must be indexed on the same shard. This means that the same routing value needs to be provided when getting, deleting, or updating a child document.
  • An element can have multiple children but only one parent.
  • It is possible to add a new relation to an existing join field.
  • It is also possible to add a child to an existing element but only if the element is already a parent.

Searching with parent-joinedit

The parent-join creates one field to index the name of the relation within the document (my_parent, my_child, …).

It also creates one field per parent/child relation. The name of this field is the name of the join field followed by # and the name of the parent in the relation. So for instance for the my_parent ⇒ [my_child, another_child] relation, the join field creates an additional field named my_join_field#my_parent.

This field contains the parent _id that the document links to if the document is a child (my_child or another_child) and the _id of document if it’s a parent (my_parent).

When searching an index that contains a join field, these two fields are always returned in the search response:

GET my_index/_search
{
  "query": {
    "match_all": {}
  },
  "sort": ["_id"]
}

Will return:

{
    ...,
    "hits": {
        "total": 4,
        "max_score": null,
        "hits": [
            {
                "_index": "my_index",
                "_type": "doc",
                "_id": "1",
                "_score": null,
                "_source": {
                    "text": "This is a parent document",
                    "my_join_field": "my_parent"
                },
                "fields": {
                    "my_join_field": [
                        "my_parent"   
                    ]
                },
                "sort": [
                    "1"
                ]
            },
            {
                "_index": "my_index",
                "_type": "doc",
                "_id": "2",
                "_score": null,
                "_source": {
                    "text": "This is a another parent document",
                    "my_join_field": "my_parent"
                },
                "fields": {
                    "my_join_field": [
                        "my_parent"  
                    ]
                },
                "sort": [
                    "2"
                ]
            },
            {
                "_index": "my_index",
                "_type": "doc",
                "_id": "3",
                "_score": null,
                "_routing": "1",
                "_source": {
                    "text": "This is a child document",
                    "my_join_field": {
                        "name": "my_child", 
                        "parent": "1"  
                    }
                },
                "fields": {
                    "my_join_field": [
                        "my_child"
                    ],
                    "my_join_field#my_parent": [
                        "1"
                    ]
                },
                "sort": [
                    "3"
                ]
            },
            {
                "_index": "my_index",
                "_type": "doc",
                "_id": "4",
                "_score": null,
                "_routing": "1",
                "_source": {
                    "text": "This is a another child document",
                    "my_join_field": {
                        "name": "my_child",
                        "parent": "1"
                    }
                },
                "fields": {
                    "my_join_field": [
                        "my_child"
                    ],
                    "my_join_field#my_parent": [
                        "1"
                    ]
                },
                "sort": [
                    "4"
                ]
            }
        ]
    }
}

This document belongs to the my_parent join

This document belongs to the my_parent join

This document belongs to the my_child join

The linked parent id for the child document

Parent-join queries and aggregationsedit

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

The value of the join field is accessible in aggregations and scripts, and may be queried with the parent_id query:

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

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

Aggregating on the parent id field (also see the children aggregation)

Accessing the parent id` field in scripts

Global ordinalsedit

The join field 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 join field.

Global ordinals, by default, are built eagerly: if the index has changed, global ordinals for the join field will be rebuilt as part of the refresh. This can add significant time to the refresh. However most of the times this is the right trade-off, otherwise global ordinals are rebuilt when the first parent-join 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 join field may be attempt rebuilt within a single refresh interval when many writes are occurring.

When the join field is used infrequently and writes occur frequently it may make sense to disable eager loading:

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "my_join_field": {
          "type": "join",
          "relations": {
             "my_parent": "my_child"
          },
          "eager_global_ordinals": false
        }
      }
    }
  }
}

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

# Per-index
GET _stats/fielddata?human&fields=my_join_field#my_parent

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

Multiple levels of parent joinedit

It is also possible to define multiple children for a single parent:

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "my_join_field": {
          "type": "join",
          "relations": {
            "my_parent": ["my_child", "another_child"]  
          }
        }
      }
    }
  }
}

my_parent is parent of my_child.

And multiple levels of parent/child:

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "my_join_field": {
          "type": "join",
          "relations": {
            "my_parent": ["my_child", "another_child"],  
            "another_child": "grand_child" 
          }
        }
      }
    }
  }
}

my_parent is parent of my_child and another_child

another_child is parent of grand_child

The mapping above represents the following tree:

     my_parent
     /    \
    /      \
my_child  another_child
              |
              |
         grand_child

Indexing a grand child document requires a routing value equals to the grand-parent (the greater parent of the lineage):

PUT my_index/doc/3?routing=1&refresh 
{
  "text": "This is a grand child document",
  "my_join_field": {
    "name": "grand_child",
    "parent": "2" 
  }
}

This child document must be on the same shard than its grandparent and parent

The parent id of this document (must points to an another_child document)