Parent ID query
editParent ID query
editReturns child documents joined to a specific parent document. You can use a join field mapping to create parent-child relationships between documents in the same index.
Example request
editIndex setup
editTo use the parent_id
query, your index must include a join
field mapping. To see how you can set up an index for the parent_id
query, try
the following example.
-
Create an index with a join field mapping.
resp = client.indices.create( index="my-index-000001", mappings={ "properties": { "my-join-field": { "type": "join", "relations": { "my-parent": "my-child" } } } }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { "my-join-field": { type: 'join', relations: { "my-parent": 'my-child' } } } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { properties: { "my-join-field": { type: "join", relations: { "my-parent": "my-child", }, }, }, }, }); console.log(response);
PUT /my-index-000001 { "mappings": { "properties": { "my-join-field": { "type": "join", "relations": { "my-parent": "my-child" } } } } }
-
Index a parent document with an ID of
1
.resp = client.index( index="my-index-000001", id="1", refresh=True, document={ "text": "This is a parent document.", "my-join-field": "my-parent" }, ) print(resp)
response = client.index( index: 'my-index-000001', id: 1, refresh: true, body: { text: 'This is a parent document.', "my-join-field": 'my-parent' } ) puts response
const response = await client.index({ index: "my-index-000001", id: 1, refresh: "true", document: { text: "This is a parent document.", "my-join-field": "my-parent", }, }); console.log(response);
PUT /my-index-000001/_doc/1?refresh { "text": "This is a parent document.", "my-join-field": "my-parent" }
-
Index a child document of the parent document.
resp = client.index( index="my-index-000001", id="2", routing="1", refresh=True, document={ "text": "This is a child document.", "my-join-field": { "name": "my-child", "parent": "1" } }, ) print(resp)
const response = await client.index({ index: "my-index-000001", id: 2, routing: 1, refresh: "true", document: { text: "This is a child document.", "my-join-field": { name: "my-child", parent: "1", }, }, }); console.log(response);
PUT /my-index-000001/_doc/2?routing=1&refresh { "text": "This is a child document.", "my-join-field": { "name": "my-child", "parent": "1" } }
Example query
editThe following search returns child documents for a parent document with an ID of
1
.
resp = client.search( index="my-index-000001", query={ "parent_id": { "type": "my-child", "id": "1" } }, ) print(resp)
const response = await client.search({ index: "my-index-000001", query: { parent_id: { type: "my-child", id: "1", }, }, }); console.log(response);
GET /my-index-000001/_search { "query": { "parent_id": { "type": "my-child", "id": "1" } } }
Top-level parameters for parent_id
edit-
type
- (Required, string) Name of the child relationship mapped for the join field.
-
id
- (Required, string) ID of the parent document. The query will return child documents of this parent document.
-
ignore_unmapped
-
(Optional, Boolean) Indicates whether to ignore an unmapped
type
and not return any documents instead of an error. Defaults tofalse
.If
false
, Elasticsearch returns an error if thetype
is unmapped.You can use this parameter to query multiple indices that may not contain the
type
.