# Create parent/child index with java api

**URL:** https://discuss.elastic.co/t/create-parent-child-index-with-java-api/116079
**Category:** Elasticsearch
**Created:** [January 18, 2018, 2:44pm UTC](https://discuss.elastic.co/t/create-parent-child-index-with-java-api/116079 "2018-01-18T14:44:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Infire](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@Infire](https://discuss.elastic.co/u/Infire)
#### Post date: [January 18, 2018, 2:44pm UTC](https://discuss.elastic.co/t/create-parent-child-index-with-java-api/116079/1 "2018-01-18T14:44:37Z")

</div>

Hi,

I'm trying to create an index with a parent/child relationship but I'm getting an error while creating the child: " **can't specify parent if no parent field has been configured**". I don't know how I am supposed to create the mapping between parent and child.

I'm using the 6.0 API.

Code:

```
    // create parent
    Map<String, Object> parent = new HashMap<String, Object>();
    parent.put("name","Parent1");
    parent.put("age", 40);
    UUID parentId = UUID.randomUUID();
    IndexRequest indexRequest = new IndexRequest(INDEX_JOIN_DATATYPE_TEST, "parent", parentId.toString())
            .source(parent);

    IndexResponse indexResponse = client.index(indexRequest);

    // create children
    Map<String, Object> child1 = new HashMap<String, Object>();
    child1.put("name","Child1");
    child1.put("age", 20);
    child1.put("parentId", parentId.toString());
    indexRequest = new IndexRequest(INDEX_JOIN_DATATYPE_TEST, "child", UUID.randomUUID().toString())
            .source(child1);
    indexRequest.parent("parent");
    indexResponse = client.index(indexRequest);

```

Can anyone tell me how to use the api correctly? The documentation is not sufficient for me ...

Greetings

---

<div class="post-metadata">

### Author: ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)
#### Post date: [January 26, 2018, 10:54am UTC](https://discuss.elastic.co/t/create-parent-child-index-with-java-api/116079/2 "2018-01-26T10:54:36Z")

</div>

did you create your index with the corresponding mapping for parent child? Also if you are on 6.x you should use the [join field](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/parent-join.html) instead.

---

<div class="post-metadata">

### Author: ![Infire](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@Infire](https://discuss.elastic.co/u/Infire)
#### Post date: [January 30, 2018, 3:34pm UTC](https://discuss.elastic.co/t/create-parent-child-index-with-java-api/116079/3 "2018-01-30T15:34:02Z")

</div>

No, this is all the code I used. I would like to create the index and its mapping on the fly, without creating it first via the dev tools. Is this possible using the 6.0 java api? If yes, then HOW do I do it?  
Thanks in advance! 🙂

---

<div class="post-metadata">

### Author: ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)
#### Post date: [January 30, 2018, 3:35pm UTC](https://discuss.elastic.co/t/create-parent-child-index-with-java-api/116079/4 "2018-01-30T15:35:51Z")

</div>

you have to put some mapping for the index in order to use the join field. you can use [index templates](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html) to add some _defaults_ that you want all indices to use.

---

<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: [February 27, 2018, 3:44pm UTC](https://discuss.elastic.co/t/create-parent-child-index-with-java-api/116079/5 "2018-02-27T15:44:40Z")

</div>

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