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

时间:2019-07-10 13:33:34

标签: elasticsearch

我在how to search child documents with the same parent id in Elasticsearch?中阅读了答案,但是没有一个人可以在6.2.4上解决它,有人可以告诉我如何在6.2.4上做同样的工作吗?

1 个答案:

答案 0 :(得分:0)

从Elasticsearch 6.x开始,将父/子作为单独的类型存储已被join datatype取代。这从根本上不会改变您对这些文档建立索引和查询的方式,但是会确实改变语法。

注意:在准备完全删除type时进行了这组更改,此更改发生在7.x

初始化映射(使用模板)

我们将为所有文档创建一个名为join的{​​{1}}字段,而不是指定单独的类型。 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来指定每个文档位于关系的哪一侧。

branch_join

要索引孩子,我们需要做两件事: 1.我们需要再次指定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"}} 字段,但不仅要指定连接的哪一侧是(branch_join,还需要指定父级的employee要加入的文件。 2.为了确保子代在与父代相同的分片上建立索引,我们将设置_id参数等于父文档的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