# Java API for inserting nested documents

**URL:** <https://discuss.elastic.co/t/java-api-for-inserting-nested-documents/35226>\
**Category:** Elasticsearch\
**Created:** [November 21, 2015, 5:37pm UTC](https://discuss.elastic.co/t/java-api-for-inserting-nested-documents/35226 "2015-11-21T17:37:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maarten\_Roosendaal](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@Maarten\_Roosendaal](https://discuss.elastic.co/u/Maarten_Roosendaal)\
**Post date:** [November 21, 2015, 5:37pm UTC](https://discuss.elastic.co/t/java-api-for-inserting-nested-documents/35226/1 "2015-11-21T17:37:41Z")

</div>

Hi,

I'm trying to use the Java API to do insert/updates for nested documents. I have a collection with products and the nested documents are offers. Doing this via a json request works fine:  
{  
"script" : "ctx.\_source.offer=offer",  
"params" : {  
"offer": {"price": 20}  
}  
}

However using the java API is not that trivial, i tried the example in:

> <https://stackoverflow.com/questions/23363801/update-nested-field-in-an-index-of-elasticsearch-with-java-api>

  
but can't get it to work. There is a setScript() method but not a setScriptParameter() method.

So what i have now is:  
UpdateRequestBuilder br = client.prepareUpdate(INDEXNAME, DOCUMENT\_TYPE, PRODUCT\_ID);  
build a json string  
br.setScript(new Script("ctx.\_source.offer = offer"));  
but i can't add the json string anywhere and the documentation seems void of an example.

I use elasticsearch 2.0.0, java 8, windows 7

Hope someone has an idea on how to do this with the Java API.

Thanks,  
Maarten

---

<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, 11:36pm UTC](https://discuss.elastic.co/t/java-api-for-inserting-nested-documents/35226/2 "2017-07-05T23:36:42Z")

</div>


