# How can I set a parent / child relationship

**URL:** https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679
**Category:** Elasticsearch
**Created:** [April 18, 2017, 9:39am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679 "2017-04-18T09:39:56Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ToujouAya](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@ToujouAya](https://discuss.elastic.co/u/ToujouAya)
#### Post date: [April 18, 2017, 9:39am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/1 "2017-04-18T09:39:56Z")

</div>

I'm trying to create two documents, one build a child of the other. My mapping config is like this

```
'mappings' => [
            "roadType" => [
                "properties" => [
                ]
            ],
            "carType" => [
                "_parent" => [
                    "type" => "roadType"
                ],
                "properties" => [
                    "make" => [
                        "type" => "string"
                    ],
                    "model" => [
                        "type" => "string"
                    ]
                ]
            ]
        ]

```

I got this error message .  
`{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"No handler for type [roadType] declared on fie ld [_parent]"}],"type":"mapper_parsing_exception","reason":"No handler for type [roadType] declared on field [_pare nt]"},"status":400}`  
Does anyone have any idea. Thank you

---

<div class="post-metadata">

### Author: ![Nilabhsagar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nilabhsagar/32/10647_2.png) [@Nilabhsagar](https://discuss.elastic.co/u/Nilabhsagar)
#### Post date: [April 18, 2017, 1:04pm UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/2 "2017-04-18T13:04:39Z")

</div>

Hi, Can you let us know as on which version of Elasticsearch your are getting this error?

---

<div class="post-metadata">

### Author: ![ToujouAya](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@ToujouAya](https://discuss.elastic.co/u/ToujouAya)
#### Post date: [April 19, 2017, 1:15am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/3 "2017-04-19T01:15:58Z")

</div>

My Elasticsearch version is 5.3.0 and I am using this package [https://github.com/basemkhirat/elasticsearch](https://github.com/basemkhirat/elasticsearch) to communicate with Elasticsearch.

---

<div class="post-metadata">

### Author: ![Nilabhsagar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nilabhsagar/32/10647_2.png) [@Nilabhsagar](https://discuss.elastic.co/u/Nilabhsagar)
#### Post date: [April 19, 2017, 11:53am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/4 "2017-04-19T11:53:51Z")

</div>

The mapping looks fine and works properly when used directly in Elasticsearch. I feel the problem is in the client which you are using to communicate with Elasticsearch. You can try creating the child type first and then the parent; see if this helps.

Also, just for your information - In the newer version of Elasticsearch type: string has been replaced with type: text or keyword.

---

<div class="post-metadata">

### Author: ![ToujouAya](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@ToujouAya](https://discuss.elastic.co/u/ToujouAya)
#### Post date: [April 20, 2017, 1:20am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/5 "2017-04-20T01:20:46Z")

</div>

Thank you. I have figured out why it is not work.  
When create the index , the mapping of parent and child must create in same time. So this is a problem. The client which i am using to communicate with Elasticsearch will sent request to ES server one by one for each mapping type.

Do you know how to create parent/child when the parent type is already exist ?

---

<div class="post-metadata">

### Author: ![Nilabhsagar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nilabhsagar/32/10647_2.png) [@Nilabhsagar](https://discuss.elastic.co/u/Nilabhsagar)
#### Post date: [April 20, 2017, 2:03am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/6 "2017-04-20T02:03:02Z")

</div>

Its not the same time; the rule is - parent should not exist before child creation, thats why i pointed that the problem is in the client.

What you can try is to change the order of your type creation in the mapping. Put child mapping before parent. It should work.

Your mapping will be like below:

```
'mappings' => [
        "carType" => [
            "_parent" => [
                "type" => "roadType"
            ],
            "properties" => [
                "make" => [
                    "type" => "text"
                ],
                "model" => [
                    "type" => "text"
                ]
            ]
        ],
        "roadType" => [
            "properties" => [
            ]
        ]
    ]
```

---

<div class="post-metadata">

### Author: ![ToujouAya](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@ToujouAya](https://discuss.elastic.co/u/ToujouAya)
#### Post date: [April 20, 2017, 6:09am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/7 "2017-04-20T06:09:16Z")

</div>

Thanks I missed this point. But  
Do you know how to create parent/child if the parent type is already exist ? Is it possible to do it ?

---

<div class="post-metadata">

### Author: ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)
#### Post date: [April 20, 2017, 6:41am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/8 "2017-04-20T06:41:55Z")

</div>

See [https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html#\_parent\_child\_restrictions](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html#_parent_child_restrictions)

---

<div class="post-metadata">

### Author: ![ToujouAya](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@ToujouAya](https://discuss.elastic.co/u/ToujouAya)
#### Post date: [April 20, 2017, 7:59am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/9 "2017-04-20T07:59:12Z")

</div>

Mmm. Thanks . So there is no way to create child type if parent type is already exist .

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [May 18, 2017, 8:07am UTC](https://discuss.elastic.co/t/how-can-i-set-a-parent-child-relationship/82679/10 "2017-05-18T08:07:34Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
