父 ID 查询
编辑父 ID 查询
编辑返回与特定父文档连接的子文档。您可以使用join 字段映射在同一索引中的文档之间创建父子关系。
示例请求
编辑索引设置
编辑要使用parent_id
查询,您的索引必须包含join 字段映射。要了解如何为parent_id
查询设置索引,请尝试以下示例。
-
创建一个包含join 字段映射的索引。
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" } } } } }
-
索引一个 ID 为
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" }
-
索引父文档的子文档。
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" } }
示例查询
编辑以下搜索将返回 ID 为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" } } }