# How to create a parent-child documents using High Level ES REST client

**URL:** <https://discuss.elastic.co/t/how-to-create-a-parent-child-documents-using-high-level-es-rest-client/135807>\
**Category:** Elasticsearch\
**Created:** [June 13, 2018, 11:13pm UTC](https://discuss.elastic.co/t/how-to-create-a-parent-child-documents-using-high-level-es-rest-client/135807 "2018-06-13T23:13:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudeepkumarnair](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@sudeepkumarnair](https://discuss.elastic.co/u/sudeepkumarnair)\
**Post date:** [June 13, 2018, 11:13pm UTC](https://discuss.elastic.co/t/how-to-create-a-parent-child-documents-using-high-level-es-rest-client/135807/1 "2018-06-13T23:13:56Z")

</div>

Consider the following piece of indexing code and please set know how i can add attributes to link parent and child documents. This piece of documentation is missing on Elasticseach site.

Schema:

PUT discovery-index/\_doc/2  
{  
"dimensions" : [  
{  
"key" : "cluster",  
"val" : "tess33"  
},  
{  
"key" : "pod",  
"val" : "test-pod"  
}  
],  
"values" : [  
{  
"key" : "type",  
"val" : "logs"  
}  
],  
"join\_field": "topo"  
}

POST discovery-index/\_doc/?routing=1&refresh  
{  
"timestamp": 1528063525,  
"name": "stdout",  
"namespace": "tess-apps",  
"type": "logs",  
"join\_field": {  
"name": "document",  
"parent": "1"  
}  
}

```auto
public void writeInBulkRoot(BulkProcessor bulkProcessor, String indexName, String type, String id, String payLoad) {
		IndexRequest indexRequest = (id == null ? new IndexRequest(indexName, type)
				: new IndexRequest(indexName, type, id));
		indexRequest.source(payLoad, XContentType.JSON);
		indexRequest.create(true);
		
             //TODO: How to i set the join field and parent id
		
		try {
			bulkProcessor.add(indexRequest);

		} catch (Exception e) {
			logger.error("Exception while connecting to Elastic. Taking defaults:" + e.getMessage(), e);
		}
	}

```

---

<div class="post-metadata">

**Author:** ![sudeepkumarnair](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@sudeepkumarnair](https://discuss.elastic.co/u/sudeepkumarnair)\
**Post date:** [June 13, 2018, 11:29pm UTC](https://discuss.elastic.co/t/how-to-create-a-parent-child-documents-using-high-level-es-rest-client/135807/2 "2018-06-13T23:29:30Z")

</div>

Am i supposed to put parent -child relation as part of the payload itself ? Confusing , if true

---

<div class="post-metadata">

**Author:** ![sudeepkumarnair](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@sudeepkumarnair](https://discuss.elastic.co/u/sudeepkumarnair)\
**Post date:** [June 14, 2018, 12:26am UTC](https://discuss.elastic.co/t/how-to-create-a-parent-child-documents-using-high-level-es-rest-client/135807/3 "2018-06-14T00:26:34Z")

</div>

Yes, that seems to be case... Not sure why ES REST library allows this in much cleaner way

```auto
private String getRootPayload(MetaDataValue value) {

		// Dimensions
		ArrayNode dimensions = factory.arrayNode();
		for (Map.Entry<String, String> entry : value.getDimensionKeys().entrySet()) {
			ObjectNode jsDimensions = factory.objectNode();
			jsDimensions.put("key", entry.getKey());
			jsDimensions.put("val", entry.getValue());
			dimensions.add(jsDimensions);
		}
		ObjectNode dimensionRoot = factory.objectNode();
		dimensionRoot.set("dimensions", dimensions);

		dimensionRoot.put("join_field", "topo");
		String payload = dimensionRoot.toString();
		return payload;
	}

	private String getChildPayload(MetaDataValue value) {

		ObjectNode childDocument = factory.objectNode();
		childDocument.put("name", value.getName());
		childDocument.put("namespace", value.getName());
		childDocument.put("timestamp", value.getTs());
		childDocument.put("type", value.getType().name());

		ObjectNode joinfield = factory.objectNode();
		joinfield.put("name", "document");
		joinfield.put("parent", value.getId());

		childDocument.set("join_field", joinfield);

		String payload = childDocument.toString();
		return payload;

	}

```

---

<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 12, 2018, 12:26am UTC](https://discuss.elastic.co/t/how-to-create-a-parent-child-documents-using-high-level-es-rest-client/135807/4 "2018-07-12T00:26:34Z")

</div>

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