2.X/6. Modeling Your Data

6-3-2. Indexing Parents and Children

drscg 2017. 9. 23. 14:35

Indexing parent documents is no different from any other document. Parents don’t need to know anything about their children:

부모 document를 색인 하는 것은 다른 document와 다르지 않다. 부모는 그들의 자식에 대해 아무것도 알 필요가 없다.

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

When indexing child documents, you must specify the ID of the associated parent document:

자식 document를 색인할 때, 관련된 부모 document의 ID를 지정해야 한다.

PUT /company/employee/1?parent=london 
{
  "name":  "Alice Smith",
  "dob":   "1970-10-24",
  "hobby": "hiking"
}

employee document는 london 지점의 자식이다.

This parent ID serves two purposes: it creates the link between the parent and the child, and it ensures that the child document is stored on the same shard as the parent.

이 parent ID는 2가지 용도가 있다. 부모와 자식간의 연결을 생성하고, 자식 document가 부모와 동일한 shard에 저장되도록 보장한다.

In Routing a Document to a Shard, we explained how Elasticsearch uses a routing value, which defaults to the _id of the document, to decide which shard a document should belong to. The routing value is plugged into this simple formula:

Routing a Document to a Shard에서, Elasticsearch에서 document가 어느 shard에 포함될지를 결정하는, document의 _id 를 기본값으로 하는, routing 값을 사용하는 방법을 설명했었다. routing 값은 아래의 간단한 수식으로 결정된다.

shard = hash(routing) % number_of_primary_shards

However, if a parent ID is specified, it is used as the routing value instead of the _id. In other words, both the parent and the child use the same routing value—the _id of the parent—and so they are both stored on the same shard.

그러나, parent ID가 지정되면, 그것을 _id 대신 routing 값으로 사용한다. 즉, 부모와 자식 모두는 동일한 routing 값(parent의 _id)을 사용한다. 따라서 둘 모두는 동일한 shard에 저장된다.

The parent ID needs to be specified on all single-document requests: when retrieving a child document with a GET request, or when indexing, updating, or deleting a child document. Unlike a search request, which is forwarded to all shards in an index, these single-document requests are forwarded only to the shard that holds the document—if the parent ID is not specified, the request will probably be forwarded to the wrong shard.

parent ID는 모든 단일 document(single-document) request(자식 document를 GET request로 가져오거나, 자식 document를 색인, 업데이트, 또는 삭제할 경우) 모두에 지정해야 한다. index의 모든 shard에 전달되는 검색 request와 달리, 이러한 단일 document request는 document를 가지고 있는 shard에게만 전달된다. parent ID가 지정되지 않으면, 해당 request는 아마 잘못된 shard로 전달될 것이다.

The parent ID should also be specified when using the bulk API:

parent ID는 bulk API를 사용하는 경우에도 지정해야 한다.

POST /company/employee/_bulk
{ "index": { "_id": 2, "parent": "london" }}
{ "name": "Mark Thomas", "dob": "1982-05-16", "hobby": "diving" }
{ "index": { "_id": 3, "parent": "liverpool" }}
{ "name": "Barry Smith", "dob": "1979-04-01", "hobby": "hiking" }
{ "index": { "_id": 4, "parent": "paris" }}
{ "name": "Adrien Grand", "dob": "1987-05-11", "hobby": "horses" }
Warning

If you want to change the parent value of a child document, it is not sufficient to just reindex or update the child document—the new parent document may be on a different shard. Instead, you must first delete the old child, and then index the new child.

자식 document의 parent 값을 바꾸려면, 새로운 부모 document는 다른 shard에 있을 것이므로, 자식 document를 재색인하거나 업데이트하는 것으로 충분하지 않다. 대신에, 기존 자식을 먼저 삭제하고, 새로운 자식을 색인 해야 한다.


'2.X > 6. Modeling Your Data' 카테고리의 다른 글

6-3. Parent-Child Relationship  (0) 2017.09.23
6-3-1. Parent-Child Mapping  (0) 2017.09.23
6-3-3. Finding Parents by Their Children  (0) 2017.09.23
6-3-4. Finding Children by Their Parents  (0) 2017.09.23
6-3-5. Children Aggregation  (0) 2017.09.23