# Inserting nested fields into Document using Java API? \[Code Provided\]

**URL:** <https://discuss.elastic.co/t/inserting-nested-fields-into-document-using-java-api-code-provided/89781>\
**Category:** Elasticsearch\
**Created:** [June 16, 2017, 9:55pm UTC](https://discuss.elastic.co/t/inserting-nested-fields-into-document-using-java-api-code-provided/89781 "2017-06-16T21:55:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaushah](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@kaushah](https://discuss.elastic.co/u/kaushah)\
**Post date:** [June 16, 2017, 9:55pm UTC](https://discuss.elastic.co/t/inserting-nested-fields-into-document-using-java-api-code-provided/89781/1 "2017-06-16T21:55:19Z")

</div>

```
public static void createIndex() throws IOException 
{
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
        			        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

final CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate("test");
final XContentBuilder mappingBuilder = jsonBuilder().startObject().startObject("testing")
        	                .startObject("properties").startObject("Tag").field("type", "nested").endObject().startObject("Address").field("type","nested").endObject()
        	                .endObject().endObject().endObject();
        	       
createIndexRequestBuilder.addMapping("testing", mappingBuilder);
createIndexRequestBuilder.execute().actionGet(); 

```

}  
Above is how I am creating the index and mapping.

and inserting the document as:

```
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("test", "testing", "1");
indexRequestBuilder.setSource(Data);
indexRequestBuilder.execute().actionGet();

```

where Data is a HashMap (String, String).

How do I make sure that Tag and Address are of nested type?  
Is this the correct way to insert the document with nested type fields?

---

<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 14, 2017, 9:55pm UTC](https://discuss.elastic.co/t/inserting-nested-fields-into-document-using-java-api-code-provided/89781/2 "2017-07-14T21:55:40Z")

</div>

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