Join field type
editJoin field type
editThe join
data type 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.
We don’t recommend using multiple levels of relations to replicate a relational model. Each level of relation adds an overhead at query time in terms of memory and computation. For better search performance, denormalize your data instead.
A parent/child relation can be defined as follows:
resp = client.indices.create( index="my-index-000001", mappings={ "properties": { "my_id": { "type": "keyword" }, "my_join_field": { "type": "join", "relations": { "question": "answer" } } } }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { my_id: { type: 'keyword' }, my_join_field: { type: 'join', relations: { question: 'answer' } } } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { properties: { my_id: { type: "keyword", }, my_join_field: { type: "join", relations: { question: "answer", }, }, }, }, }); console.log(response);
PUT my-index-000001 { "mappings": { "properties": { "my_id": { "type": "keyword" }, "my_join_field": { "type": "join", "relations": { "question": "answer" } } } } }
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 example creates two parent
documents in the question
context:
resp = client.index( index="my-index-000001", id="1", refresh=True, document={ "my_id": "1", "text": "This is a question", "my_join_field": { "name": "question" } }, ) print(resp) resp1 = client.index( index="my-index-000001", id="2", refresh=True, document={ "my_id": "2", "text": "This is another question", "my_join_field": { "name": "question" } }, ) print(resp1)
response = client.index( index: 'my-index-000001', id: 1, refresh: true, body: { my_id: '1', text: 'This is a question', my_join_field: { name: 'question' } } ) puts response response = client.index( index: 'my-index-000001', id: 2, refresh: true, body: { my_id: '2', text: 'This is another question', my_join_field: { name: 'question' } } ) puts response
const response = await client.index({ index: "my-index-000001", id: 1, refresh: "true", document: { my_id: "1", text: "This is a question", my_join_field: { name: "question", }, }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 2, refresh: "true", document: { my_id: "2", text: "This is another question", my_join_field: { name: "question", }, }, }); console.log(response1);
PUT my-index-000001/_doc/1?refresh { "my_id": "1", "text": "This is a question", "my_join_field": { "name": "question" } } PUT my-index-000001/_doc/2?refresh { "my_id": "2", "text": "This is another question", "my_join_field": { "name": "question" } }
When indexing parent documents, you can choose to specify just the name of the relation as a shortcut instead of encapsulating it in the normal object notation:
resp = client.index( index="my-index-000001", id="1", refresh=True, document={ "my_id": "1", "text": "This is a question", "my_join_field": "question" }, ) print(resp) resp1 = client.index( index="my-index-000001", id="2", refresh=True, document={ "my_id": "2", "text": "This is another question", "my_join_field": "question" }, ) print(resp1)
const response = await client.index({ index: "my-index-000001", id: 1, refresh: "true", document: { my_id: "1", text: "This is a question", my_join_field: "question", }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 2, refresh: "true", document: { my_id: "2", text: "This is another question", my_join_field: "question", }, }); console.log(response1);
PUT my-index-000001/_doc/1?refresh { "my_id": "1", "text": "This is a question", "my_join_field": "question" } PUT my-index-000001/_doc/2?refresh { "my_id": "2", "text": "This is another question", "my_join_field": "question" }
When indexing a child, the name of the relation as well as the parent id of the document
must be added in the _source
.
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 example shows how to index two child
documents:
resp = client.index( index="my-index-000001", id="3", routing="1", refresh=True, document={ "my_id": "3", "text": "This is an answer", "my_join_field": { "name": "answer", "parent": "1" } }, ) print(resp) resp1 = client.index( index="my-index-000001", id="4", routing="1", refresh=True, document={ "my_id": "4", "text": "This is another answer", "my_join_field": { "name": "answer", "parent": "1" } }, ) print(resp1)
response = client.index( index: 'my-index-000001', id: 3, routing: 1, refresh: true, body: { my_id: '3', text: 'This is an answer', my_join_field: { name: 'answer', parent: '1' } } ) puts response response = client.index( index: 'my-index-000001', id: 4, routing: 1, refresh: true, body: { my_id: '4', text: 'This is another answer', my_join_field: { name: 'answer', parent: '1' } } ) puts response
const response = await client.index({ index: "my-index-000001", id: 3, routing: 1, refresh: "true", document: { my_id: "3", text: "This is an answer", my_join_field: { name: "answer", parent: "1", }, }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 4, routing: 1, refresh: "true", document: { my_id: "4", text: "This is another answer", my_join_field: { name: "answer", parent: "1", }, }, }); console.log(response1);
PUT my-index-000001/_doc/3?routing=1&refresh { "my_id": "3", "text": "This is an answer", "my_join_field": { "name": "answer", "parent": "1" } } PUT my-index-000001/_doc/4?routing=1&refresh { "my_id": "4", "text": "This is another answer", "my_join_field": { "name": "answer", "parent": "1" } }
The routing value is mandatory because parent and child documents must be indexed on the same shard |
|
|
|
The parent id of this child document |
Parent-join and performance
editThe join field shouldn’t be used like joins in a relation database. In Elasticsearch the key to good performance
is to de-normalize your data into documents. Each join field, has_child
or has_parent
query adds a
significant tax to your query performance. It can also trigger global ordinals to be built.
The only case where the join field makes sense is if your data contains a one-to-many relationship where one entity significantly outnumbers the other entity. An example of such case is a use case with products and offers for these products. In the case that offers significantly outnumbers the number of products then it makes sense to model the product as parent document and the offer as child document.
Parent-join restrictions
edit-
Only one
join
field mapping is allowed per index. -
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-join
editThe 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:
resp = client.search( index="my-index-000001", query={ "match_all": {} }, sort=[ "my_id" ], ) print(resp)
const response = await client.search({ index: "my-index-000001", query: { match_all: {}, }, sort: ["my_id"], }); console.log(response);
GET my-index-000001/_search { "query": { "match_all": {} }, "sort": ["my_id"] }
Will return:
{ ..., "hits": { "total": { "value": 4, "relation": "eq" }, "max_score": null, "hits": [ { "_index": "my-index-000001", "_id": "1", "_score": null, "_source": { "my_id": "1", "text": "This is a question", "my_join_field": "question" }, "sort": [ "1" ] }, { "_index": "my-index-000001", "_id": "2", "_score": null, "_source": { "my_id": "2", "text": "This is another question", "my_join_field": "question" }, "sort": [ "2" ] }, { "_index": "my-index-000001", "_id": "3", "_score": null, "_routing": "1", "_source": { "my_id": "3", "text": "This is an answer", "my_join_field": { "name": "answer", "parent": "1" } }, "sort": [ "3" ] }, { "_index": "my-index-000001", "_id": "4", "_score": null, "_routing": "1", "_source": { "my_id": "4", "text": "This is another answer", "my_join_field": { "name": "answer", "parent": "1" } }, "sort": [ "4" ] } ] } }
Parent-join queries and aggregations
editSee 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:
resp = client.search( index="my-index-000001", query={ "parent_id": { "type": "answer", "id": "1" } }, aggs={ "parents": { "terms": { "field": "my_join_field#question", "size": 10 } } }, runtime_mappings={ "parent": { "type": "long", "script": "\n emit(Integer.parseInt(doc['my_join_field#question'].value)) \n " } }, fields=[ { "field": "parent" } ], ) print(resp)
const response = await client.search({ index: "my-index-000001", query: { parent_id: { type: "answer", id: "1", }, }, aggs: { parents: { terms: { field: "my_join_field#question", size: 10, }, }, }, runtime_mappings: { parent: { type: "long", script: "\n emit(Integer.parseInt(doc['my_join_field#question'].value)) \n ", }, }, fields: [ { field: "parent", }, ], }); console.log(response);
GET my-index-000001/_search { "query": { "parent_id": { "type": "answer", "id": "1" } }, "aggs": { "parents": { "terms": { "field": "my_join_field#question", "size": 10 } } }, "runtime_mappings": { "parent": { "type": "long", "script": """ emit(Integer.parseInt(doc['my_join_field#question'].value)) """ } }, "fields": [ { "field": "parent" } ] }
Querying the |
|
Aggregating on the |
|
Accessing the |
Global ordinals
editThe 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:
resp = client.indices.create( index="my-index-000001", mappings={ "properties": { "my_join_field": { "type": "join", "relations": { "question": "answer" }, "eager_global_ordinals": False } } }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { my_join_field: { type: 'join', relations: { question: 'answer' }, eager_global_ordinals: false } } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { properties: { my_join_field: { type: "join", relations: { question: "answer", }, eager_global_ordinals: false, }, }, }, }); console.log(response);
PUT my-index-000001 { "mappings": { "properties": { "my_join_field": { "type": "join", "relations": { "question": "answer" }, "eager_global_ordinals": false } } } }
The amount of heap used by global ordinals can be checked per parent relation as follows:
resp = client.indices.stats( metric="fielddata", human=True, fields="my_join_field", ) print(resp) resp1 = client.nodes.stats( metric="indices", index_metric="fielddata", human=True, fields="my_join_field", ) print(resp1)
response = client.indices.stats( metric: 'fielddata', human: true, fields: 'my_join_field' ) puts response response = client.nodes.stats( metric: 'indices', index_metric: 'fielddata', human: true, fields: 'my_join_field' ) puts response
const response = await client.indices.stats({ metric: "fielddata", human: "true", fields: "my_join_field", }); console.log(response); const response1 = await client.nodes.stats({ metric: "indices", index_metric: "fielddata", human: "true", fields: "my_join_field", }); console.log(response1);
# Per-index GET _stats/fielddata?human&fields=my_join_field#question # Per-node per-index GET _nodes/stats/indices/fielddata?human&fields=my_join_field#question
Multiple children per parent
editIt is also possible to define multiple children for a single parent:
resp = client.indices.create( index="my-index-000001", mappings={ "properties": { "my_join_field": { "type": "join", "relations": { "question": [ "answer", "comment" ] } } } }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { my_join_field: { type: 'join', relations: { question: [ 'answer', 'comment' ] } } } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { properties: { my_join_field: { type: "join", relations: { question: ["answer", "comment"], }, }, }, }, }); console.log(response);
Multiple levels of parent join
editWe don’t recommend using multiple levels of relations to replicate a relational model. Each level of relation adds an overhead at query time in terms of memory and computation. For better search performance, denormalize your data instead.
Multiple levels of parent/child:
resp = client.indices.create( index="my-index-000001", mappings={ "properties": { "my_join_field": { "type": "join", "relations": { "question": [ "answer", "comment" ], "answer": "vote" } } } }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { my_join_field: { type: 'join', relations: { question: [ 'answer', 'comment' ], answer: 'vote' } } } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { properties: { my_join_field: { type: "join", relations: { question: ["answer", "comment"], answer: "vote", }, }, }, }, }); console.log(response);
PUT my-index-000001 { "mappings": { "properties": { "my_join_field": { "type": "join", "relations": { "question": ["answer", "comment"], "answer": "vote" } } } } }
The mapping above represents the following tree:
question / \ / \ comment answer | | vote
Indexing a grandchild document requires a routing
value equals
to the grand-parent (the greater parent of the lineage):
resp = client.index( index="my-index-000001", id="3", routing="1", refresh=True, document={ "text": "This is a vote", "my_join_field": { "name": "vote", "parent": "2" } }, ) print(resp)
response = client.index( index: 'my-index-000001', id: 3, routing: 1, refresh: true, body: { text: 'This is a vote', my_join_field: { name: 'vote', parent: '2' } } ) puts response
const response = await client.index({ index: "my-index-000001", id: 3, routing: 1, refresh: "true", document: { text: "This is a vote", my_join_field: { name: "vote", parent: "2", }, }, }); console.log(response);