繁体   English   中英

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

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

我阅读了有关如何在Elasticsearch中搜索具有相同父ID的子文档的答案 ,但是他们都无法在6.2.4上解决它,有人可以告诉我如何在6.2.4上完成相同的工作吗?

从Elasticsearch 6.x开始,将父/子作为单独类型存储已替换为join数据类型 这从根本上不会改变索引和查询这些文档的方式,但是会稍微改变语法。

注意:这组更改是为完全删除type而准备的,这在7.x中发生

初始化映射(使用模板)

除了指定不同的类型,我们将创建一个join指定字段branch_join的所有文件。 relations配置指定了父级->子级关系,键( branch )为父级,值( employee )为子级。

注意:为了示例,我使用名为organization的索引

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

为您的文件编制索引

索引父文档是相似的,但是我们需要通过在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"}}

要为子级建立索引,我们需要做两件事:1.我们需要再次指定branch_join字段,不仅要指定连接的哪一侧是( employee ),还需要指定父级的_id要加入的文件。 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"}}

询问

该查询几乎是相同的,只是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