From 0b74749c727333e7e330b952e01b513bf397a9c1 Mon Sep 17 00:00:00 2001 From: "weiqiang.yuan" Date: Sun, 11 Sep 2016 18:04:27 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=AE=8C=E6=88=90Parent-Child=E7=AC=AC1?= =?UTF-8?q?=E9=83=A8=E5=88=86=E7=9A=84=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 404_Parent_Child/40_Parent_child.asciidoc | 53 ++++++----------------- 1 file changed, 14 insertions(+), 39 deletions(-) diff --git a/404_Parent_Child/40_Parent_child.asciidoc b/404_Parent_Child/40_Parent_child.asciidoc index 15b64a23e..8fa13414d 100644 --- a/404_Parent_Child/40_Parent_child.asciidoc +++ b/404_Parent_Child/40_Parent_child.asciidoc @@ -1,54 +1,29 @@ [[parent-child]] -== Parent-Child Relationship +== 父-子关系文档 -The _parent-child_ relationship is ((("relationships", "parent-child")))((("parent-child relationship")))similar in nature to the -<>: both allow you to associate one entity -with another. ((("nested objects", "parent-child relationships versus")))The difference is that, with nested objects, all entities live -within the same document while, with parent-child, the parent and children -are completely separate documents. +父-子关系文档((("relationships", "parent-child")))((("parent-child relationship")))和 + <> 有一个相似点:允许将一个对象实体和另外一个对象实体关联起来。((("nested objects", "parent-child relationships versus")))而这两种类型的主要区别是:在 <> 文档中,所有对象都是在同一个文档中,而在父-子关系文档中,父对象和子对象都是完全独立的文档。 -The parent-child functionality allows you to associate one document type with -another, in a _one-to-many_ relationship--one parent to many children.((("one-to-many relationships"))) The -advantages that parent-child has over <> are as follows: +父-子关系的主要作用是允许把一个 type 的文档和另外一个 type 的文档关联起来,构成1对n的关系:一个父文档可以对应多个子文档((("one-to-many relationships")))。与 <> 相比,父-子关系的主要优势有: -* The parent document can be updated without reindexing the children. +* 更新父文档时,不会重新索引子文档。 +* 创建,修改或删除子文档时,不会影响父文档和其他子文档。这一点在这种场景下尤其有用:子文档数量较多,并且子文档创建和修改的频率高时。 +* 子文档可以作为搜索结果,独立返回。 -* Child documents can be added, changed, or deleted without affecting either - the parent or other children. This is especially useful when child documents - are large in number and need to be added or changed frequently. +Elasticsearch 维护了一个父文档和子文档的映射关系——ID maps,实现了快速的父-子文档查询操作。但是,有一点需要指出:父文档和其所有子文档,都必须要存储在同一个分片中。 -* Child documents can be returned as the results of a search request. +ID maps 维护在 <> 中。通过 <>,可以将热点查询的 ID maps 维护在内存中;当 ID maps 变得非常大时,还可以将其扩展加载到硬盘中。 -Elasticsearch maintains a map of which parents are associated with -which children. It is thanks to this map that query-time joins are fast, but -it does place a limitation on the parent-child relationship: _the parent -document and all of its children must live on the same shard_. - -The parent-child ID maps are stored in <>, which allows them to execute -quickly when fully hot in memory, but scalable enough to spill to disk when -the map is very large. [[parent-child-mapping]] -=== Parent-Child Mapping +=== 父-子映射 -All that is needed in order to establish the parent-child relationship is to -specify which document type should be the parent of a child type.((("mapping (types)", "parent-child")))((("parent-child relationship", "parent-child mapping"))) This must -be done at index creation time, or with the `update-mapping` API before the -child type has been created. +建立父-子映射关系时需要指定某一个文档 type 是另一个文档 type 的父亲。((("mapping (types)", "parent-child")))((("parent-child relationship", "parent-child mapping")))该关系可以在如下两个时间点设置:1)创建索引时;2)在子文档的 type 创建之前,通过更新父文档的 mapping。 -As an example, let's say that we have a company that has branches in many -cities. We would like to associate employees with the branch where they work. -We need to be able to search for branches, individual employees, and employees -who work for particular branches, so the nested model will not help. We -could, of course, -use <> or -<> here instead, but for demonstration -purposes we will use parent-child. +举例说明,有一个公司在多个城市有分公司,并且每一个分公司下面都有很多员工。有这样的需求:按照分公司、员工的维度去搜索,并且搜索某些特定分公司下的员工。针对该需求,用嵌套模型是无法实现的。当然,如果使用 <> 或者 <> 也是可以实现的。 -All that we have to do is to tell Elasticsearch that the `employee` type has -the `branch` document type as its `_parent`, which we can do when we create -the index: +在这里我们使用父-子文档,首先需要父子文档的映射关系,我们在创建员工 `employee` 文档 type 时,指定分公司 `branch` 的文档 type 为其父亲。 [source,json] ------------------------- @@ -64,4 +39,4 @@ PUT /company } } ------------------------- -<1> Documents of type `employee` are children of type `branch`. +<1> `employee` 文档 是 `branch` 文档的子文档。 From 181a79d569457a504618b70e7b6c1c176e235669 Mon Sep 17 00:00:00 2001 From: "weiqiang.yuan" Date: Sun, 11 Sep 2016 18:06:56 +0800 Subject: [PATCH 2/6] revise nested model name --- 404_Parent_Child/40_Parent_child.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/404_Parent_Child/40_Parent_child.asciidoc b/404_Parent_Child/40_Parent_child.asciidoc index 8fa13414d..1d804520a 100644 --- a/404_Parent_Child/40_Parent_child.asciidoc +++ b/404_Parent_Child/40_Parent_child.asciidoc @@ -2,7 +2,7 @@ == 父-子关系文档 父-子关系文档((("relationships", "parent-child")))((("parent-child relationship")))和 - <> 有一个相似点:允许将一个对象实体和另外一个对象实体关联起来。((("nested objects", "parent-child relationships versus")))而这两种类型的主要区别是:在 <> 文档中,所有对象都是在同一个文档中,而在父-子关系文档中,父对象和子对象都是完全独立的文档。 + <> 有一个相似点:允许将一个对象实体和另外一个对象实体关联起来。((("nested objects", "parent-child relationships versus")))而这两种类型的主要区别是:在 在 <> 文档中,所有对象都是在同一个文档中,而在父-子关系文档中,父对象和子对象都是完全独立的文档。 父-子关系的主要作用是允许把一个 type 的文档和另外一个 type 的文档关联起来,构成1对n的关系:一个父文档可以对应多个子文档((("one-to-many relationships")))。与 <> 相比,父-子关系的主要优势有: From 8f44a6fff0c029a7e559205ec8090b76f1a10368 Mon Sep 17 00:00:00 2001 From: "weiqiang.yuan" Date: Sun, 25 Sep 2016 16:41:30 +0800 Subject: [PATCH 3/6] reisve pull request according to first inner team review --- 404_Parent_Child/40_Parent_child.asciidoc | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/404_Parent_Child/40_Parent_child.asciidoc b/404_Parent_Child/40_Parent_child.asciidoc index 1d804520a..902a1980d 100644 --- a/404_Parent_Child/40_Parent_child.asciidoc +++ b/404_Parent_Child/40_Parent_child.asciidoc @@ -1,29 +1,27 @@ [[parent-child]] == 父-子关系文档 -父-子关系文档((("relationships", "parent-child")))((("parent-child relationship")))和 - <> 有一个相似点:允许将一个对象实体和另外一个对象实体关联起来。((("nested objects", "parent-child relationships versus")))而这两种类型的主要区别是:在 在 <> 文档中,所有对象都是在同一个文档中,而在父-子关系文档中,父对象和子对象都是完全独立的文档。 +父-子关系文档 ((("relationships", "parent-child"))) ((("parent-child relationship"))) 在实质上类似于 <> :允许将一个对象实体和另外一个对象实体关联起来。((("nested objects", "parent-child relationships versus")))而这两种类型的主要区别是:在 <> 文档中,所有对象都是在同一个文档中,而在父-子关系文档中,父对象和子对象都是完全独立的文档。 -父-子关系的主要作用是允许把一个 type 的文档和另外一个 type 的文档关联起来,构成1对n的关系:一个父文档可以对应多个子文档((("one-to-many relationships")))。与 <> 相比,父-子关系的主要优势有: +父-子关系的主要作用是允许把一个 type 的文档和另外一个 type 的文档关联起来,构成1对多的关系:一个父文档可以对应多个子文档 ((("one-to-many relationships"))) 。与 <> 相比,父-子关系的主要优势有: * 更新父文档时,不会重新索引子文档。 -* 创建,修改或删除子文档时,不会影响父文档和其他子文档。这一点在这种场景下尤其有用:子文档数量较多,并且子文档创建和修改的频率高时。 -* 子文档可以作为搜索结果,独立返回。 +* 创建,修改或删除子文档时,不会影响父文档或其他子文档。这一点在这种场景下尤其有用:子文档数量较多,并且子文档创建和修改的频率高时。 +* 子文档可以作为搜索结果独立返回。 -Elasticsearch 维护了一个父文档和子文档的映射关系——ID maps,实现了快速的父-子文档查询操作。但是,有一点需要指出:父文档和其所有子文档,都必须要存储在同一个分片中。 +Elasticsearch 维护了一个父文档和子文档的映射关系,得益于这个映射,父-子文档关联查询操作非常快。但是这个映射也对父-子文档关系有个限制条件:父文档和其所有子文档,都必须要存储在同一个分片中。 -ID maps 维护在 <> 中。通过 <>,可以将热点查询的 ID maps 维护在内存中;当 ID maps 变得非常大时,还可以将其扩展加载到硬盘中。 +父-子文档ID映射存储在 <> 中。通过 <>,可以将热点查询的 ID maps 维护在内存中;当 ID maps 变得非常大时,还可以将其扩展加载到硬盘中。 [[parent-child-mapping]] -=== 父-子映射 +=== 父-子关系文档映射 -建立父-子映射关系时需要指定某一个文档 type 是另一个文档 type 的父亲。((("mapping (types)", "parent-child")))((("parent-child relationship", "parent-child mapping")))该关系可以在如下两个时间点设置:1)创建索引时;2)在子文档的 type 创建之前,通过更新父文档的 mapping。 +建立父-子文档映射关系时只需要指定某一个文档 type 是另一个文档 type 的父亲。 ((("mapping (types)", "parent-child"))) ((("parent-child relationship", "parent-child mapping"))) 该关系可以在如下两个时间点设置:1)创建索引时;2)在子文档 type 创建之前更新父文档的 mapping。 -举例说明,有一个公司在多个城市有分公司,并且每一个分公司下面都有很多员工。有这样的需求:按照分公司、员工的维度去搜索,并且搜索某些特定分公司下的员工。针对该需求,用嵌套模型是无法实现的。当然,如果使用 <> 或者 <> 也是可以实现的。 - -在这里我们使用父-子文档,首先需要父子文档的映射关系,我们在创建员工 `employee` 文档 type 时,指定分公司 `branch` 的文档 type 为其父亲。 +举例说明,有一个公司在多个城市有分公司,并且每一个分公司下面都有很多员工。有这样的需求:按照分公司、员工的维度去搜索,并且把员工和他们工作的分公司联系起来。针对该需求,用嵌套模型是无法实现的。当然,如果使用 <> 或者 <> 也是可以实现的,但是为了演示的目的,在这里我们使用父-子文档。 +我们需要告诉Elasticsearch,在创建员工 `employee` 文档 type 时,指定分公司 `branch` 的文档 type 为其父亲。 [source,json] ------------------------- From 655eb6da7e818251339ddeda8015710703c18bd2 Mon Sep 17 00:00:00 2001 From: "weiqiang.yuan" Date: Sun, 25 Sep 2016 16:43:57 +0800 Subject: [PATCH 4/6] reisve pull request according to first inner team review --- 404_Parent_Child/40_Parent_child.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/404_Parent_Child/40_Parent_child.asciidoc b/404_Parent_Child/40_Parent_child.asciidoc index 902a1980d..0b93cba5a 100644 --- a/404_Parent_Child/40_Parent_child.asciidoc +++ b/404_Parent_Child/40_Parent_child.asciidoc @@ -21,6 +21,7 @@ Elasticsearch 维护了一个父文档和子文档的映射关系,得益于这 建立父-子文档映射关系时只需要指定某一个文档 type 是另一个文档 type 的父亲。 ((("mapping (types)", "parent-child"))) ((("parent-child relationship", "parent-child mapping"))) 该关系可以在如下两个时间点设置:1)创建索引时;2)在子文档 type 创建之前更新父文档的 mapping。 举例说明,有一个公司在多个城市有分公司,并且每一个分公司下面都有很多员工。有这样的需求:按照分公司、员工的维度去搜索,并且把员工和他们工作的分公司联系起来。针对该需求,用嵌套模型是无法实现的。当然,如果使用 <> 或者 <> 也是可以实现的,但是为了演示的目的,在这里我们使用父-子文档。 + 我们需要告诉Elasticsearch,在创建员工 `employee` 文档 type 时,指定分公司 `branch` 的文档 type 为其父亲。 [source,json] From 4bf0ce85c591e6ef946737540938a4806bb0c430 Mon Sep 17 00:00:00 2001 From: "weiqiang.yuan" Date: Sun, 25 Sep 2016 18:31:35 +0800 Subject: [PATCH 5/6] revise docvalue and maps --- 404_Parent_Child/40_Parent_child.asciidoc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/404_Parent_Child/40_Parent_child.asciidoc b/404_Parent_Child/40_Parent_child.asciidoc index 0b93cba5a..0d671723d 100644 --- a/404_Parent_Child/40_Parent_child.asciidoc +++ b/404_Parent_Child/40_Parent_child.asciidoc @@ -11,9 +11,7 @@ Elasticsearch 维护了一个父文档和子文档的映射关系,得益于这个映射,父-子文档关联查询操作非常快。但是这个映射也对父-子文档关系有个限制条件:父文档和其所有子文档,都必须要存储在同一个分片中。 -父-子文档ID映射存储在 <> 中。通过 <>,可以将热点查询的 ID maps 维护在内存中;当 ID maps 变得非常大时,还可以将其扩展加载到硬盘中。 - - +父-子文档ID映射存储在 <> 中。当映射完全在内存中时, <> 提供对映射的快速处理能力,另一方面当映射非常大时,可以通过溢出到磁盘提供足够的扩展能力 [[parent-child-mapping]] === 父-子关系文档映射 From b9172c189a047436df61b8672be204a66ceb4c19 Mon Sep 17 00:00:00 2001 From: "weiqiang.yuan" Date: Mon, 26 Sep 2016 21:03:51 +0800 Subject: [PATCH 6/6] =?UTF-8?q?revise=201=20to=20=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 404_Parent_Child/40_Parent_child.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/404_Parent_Child/40_Parent_child.asciidoc b/404_Parent_Child/40_Parent_child.asciidoc index 0d671723d..87cc431f1 100644 --- a/404_Parent_Child/40_Parent_child.asciidoc +++ b/404_Parent_Child/40_Parent_child.asciidoc @@ -3,7 +3,7 @@ 父-子关系文档 ((("relationships", "parent-child"))) ((("parent-child relationship"))) 在实质上类似于 <> :允许将一个对象实体和另外一个对象实体关联起来。((("nested objects", "parent-child relationships versus")))而这两种类型的主要区别是:在 <> 文档中,所有对象都是在同一个文档中,而在父-子关系文档中,父对象和子对象都是完全独立的文档。 -父-子关系的主要作用是允许把一个 type 的文档和另外一个 type 的文档关联起来,构成1对多的关系:一个父文档可以对应多个子文档 ((("one-to-many relationships"))) 。与 <> 相比,父-子关系的主要优势有: +父-子关系的主要作用是允许把一个 type 的文档和另外一个 type 的文档关联起来,构成一对多的关系:一个父文档可以对应多个子文档 ((("one-to-many relationships"))) 。与 <> 相比,父-子关系的主要优势有: * 更新父文档时,不会重新索引子文档。 * 创建,修改或删除子文档时,不会影响父文档或其他子文档。这一点在这种场景下尤其有用:子文档数量较多,并且子文档创建和修改的频率高时。