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

(Sudeepkumarnair) #1

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.


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"

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);
             //TODO: How to i set the join field and parent id
		try {

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

(Sudeepkumarnair) #2

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

(Sudeepkumarnair) #3

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

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());
		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;


(system) #4

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