父 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,则 Elasticsearch 会在type 未映射时返回错误。

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