父 ID 查询

编辑

返回与特定父文档连接的子文档。您可以使用 join 字段映射在同一索引中的文档之间创建父子关系。

示例请求

编辑

索引设置

编辑

要使用 parent_id 查询,您的索引必须包含 join 字段映射。要了解如何为 parent_id 查询设置索引,请尝试以下示例。

  1. 创建一个具有 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"
            }
          }
        }
      }
    }
  2. 索引一个 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"
    }
  3. 索引父文档的一个子文档。

    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"
      }
  }
}

parent_id 的顶层参数

编辑
type
(必需,字符串)为 join 字段映射的子关系名称。
id
(必需,字符串)父文档的 ID。查询将返回此父文档的子文档。
ignore_unmapped

(可选,布尔值)指示是否忽略未映射的 type,并且不返回任何文档而不是错误。默认为 false

如果为 false,则如果 type 未映射,Elasticsearch 将返回错误。

您可以使用此参数查询可能不包含 type 的多个索引。