# Define parent / child mapping via Java API

**URL:** <https://discuss.elastic.co/t/define-parent-child-mapping-via-java-api/63997>\
**Category:** Elasticsearch\
**Created:** [October 26, 2016, 12:04pm UTC](https://discuss.elastic.co/t/define-parent-child-mapping-via-java-api/63997 "2016-10-26T12:04:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_ttonk](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chris\_ttonk](https://discuss.elastic.co/u/chris_ttonk)\
**Post date:** [October 26, 2016, 12:04pm UTC](https://discuss.elastic.co/t/define-parent-child-mapping-via-java-api/63997/1 "2016-10-26T12:04:00Z")

</div>

I want to define the following parent child mapping in Java:

PUT my\_index  
{  
"mappings": {  
"my\_parent": {},  
"my\_child": {  
"\_parent": {  
"type": "my\_parent"  
}  
}  
}  
}

My problem is that the "preparePutMapping(...)" method in the Java API only accepts a single type and a single source. So I can call the method either on the "my\_parent" or on the "my\_child" object. Executing the methods one after the other obviously returns the error that a parent / child mapping can only be created on non existing types. Any advice how to do that in Java?

---

<div class="post-metadata">

**Author:** ![chris\_ttonk](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chris\_ttonk](https://discuss.elastic.co/u/chris_ttonk)\
**Post date:** [October 28, 2016, 7:29am UTC](https://discuss.elastic.co/t/define-parent-child-mapping-via-java-api/63997/2 "2016-10-28T07:29:12Z")

</div>

Found a way around ... I am using index templates now ... there I can assign more types at once without creating them directly

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [October 28, 2016, 10:31am UTC](https://discuss.elastic.co/t/define-parent-child-mapping-via-java-api/63997/3 "2016-10-28T10:31:07Z")

</div>

In the Java API, you don't need to specify the type. It can be left empty ( = null).

---

<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:** [July 5, 2017, 10:08pm UTC](https://discuss.elastic.co/t/define-parent-child-mapping-via-java-api/63997/4 "2017-07-05T22:08:47Z")

</div>


