简体   繁体   English

如何在ver_6.2.4的Elasticsearch中搜索具有相同父ID的子文档?

[英]how to search child documents with the same parent id in Elasticsearch on ver_6.2.4?

I read answers in how to search child documents with the same parent id in Elasticsearch? 我阅读了有关如何在Elasticsearch中搜索具有相同父ID的子文档的答案 , but none of them can solve it on 6.2.4, can someone tell me how to do the same job on 6.2.4? ,但是他们都无法在6.2.4上解决它,有人可以告诉我如何在6.2.4上完成相同的工作吗?

Starting in Elasticsearch 6.x, storing parent/child as separate types was replaced with a join datatype . 从Elasticsearch 6.x开始,将父/子作为单独类型存储已替换为join数据类型 This doesn't fundamentally change how you'd go about indexing and querying those documents, but does change the syntax a bit. 这从根本上不会改变索引和查询这些文档的方式,但是会稍微改变语法。

NOTE: This set of changes were made in preparation for removal of type entirely, which happens in 7.x 注意:这组更改是为完全删除type而准备的,这在7.x中发生

Initialize Mapping (using a template) 初始化映射(使用模板)

Instead of specifying separate types, we'll create a join field named branch_join for all documents. 除了指定不同的类型,我们将创建一个join指定字段branch_join的所有文件。 The relations configuration specifies the parent -> child relationship, with the key ( branch ) being the parent, and the value ( employee ) being the child. relations配置指定了父级->子级关系,键( branch )为父级,值( employee )为子级。

NOTE: I'm using the index named organization for sake of example 注意:为了示例,我使用名为organization的索引

PUT _template/org_template
{
  "index_patterns": ["organization"],
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "_doc": {
      "properties": {
        "branch_join": {
          "type": "join",
          "relations": {
            "branch": "employee"
          }
        }
      }
    }
  }
}

Index your documents 为您的文件编制索引

Indexing the parent documents is similar, but we need to specify which side of the relation each document is on by specifying branch in the branch_join field. 索引父文档是相似的,但是我们需要通过在branch_join字段中指定branch来指定每个文档位于关系的哪一侧。

POST /organization/_doc/_bulk
{"index": {"_id": "london"}}
{"name": "London Westminster", "city": "London", "country": "UK", "branch_join": {"name":"branch"}}
{"index": {"_id": "liverpool"}}
{"name": "Liverpool Central", "city": "Liverpool", "country": "UK", "branch_join": {"name":"branch"}}
{"index": {"_id": "paris"}}
{"name": "Champs Élysées", "city": "Paris", "country": "France", "branch_join": {"name":"branch"}}

To index the children, we need to do two things: 1. We need to specify the branch_join field again, but not only do we specify which side of the join this is ( employee ), we also need to specify the _id of the parent documents to join on. 要为子级建立索引,我们需要做两件事:1.我们需要再次指定branch_join字段,不仅要指定连接的哪一侧是( employee ),还需要指定父级的_id要加入的文件。 2. To ensure that children get indexed on the same shards as their parents, we'll set the routing parameter equal to the _id of the parent document. 2.为了确保子代与父代在同一分片上建立索引,我们将routing参数设置为等于父代文档的_id

POST /organization/_doc/_bulk
{"index": { "_id": 1, "routing": "london"}}
{"name": "Alice Smith", "dob": "1970-10-24", "hobby": "hiking", "branch_join": {"name":"employee", "parent": "london"}}
{"index": { "_id": 2, "routing": "london"}}
{"name": "Mark Thomas", "dob": "1982-05-16", "hobby": "diving", "branch_join": {"name":"employee", "parent": "london"}}
{"index": { "_id": 3, "routing": "liverpool"}}
{"name": "Barry Smith", "dob": "1979-04-01", "hobby": "hiking", "branch_join": {"name":"employee", "parent": "liverpool"}}
{"index": { "_id": 4, "routing": "paris"}}
{"name": "Adrien Grand", "dob": "1987-05-11", "hobby": "horses", "branch_join": {"name":"employee", "parent": "paris"}}

Query 询问

The query is almost identical, except that type has been renamed parent_type to be more explicit: 该查询几乎是相同的,只是type已更名为parent_type以便更明确:

POST /organization/_search
{
  "query": {
    "has_parent": {
      "parent_type": "branch", 
      "query": {
        "ids": {
          "values" : ["london"]
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM