6

I am working on ElasticSearch. Here I wanted to index an array of nested type fields through an JAVA api. Could any one give an example on this. I tried few yet they failed.

XContentBuilder xb1 =  XContentFactory.jsonBuilder().startObject(); 
XContentBuilder xb2 =  XContentFactory.jsonBuilder().startObject(); 

xb1.startArray("eventnested"); 
for(int j=0;j<2;j++) { 
    xb2.field("event_type",eventType); 
    xb2.field("event_attribute_instance",eventInstance); 
    xb2.startArray("attributes"); 
    for(int i=0;i<2;i++) { 
        XContentBuilder xb3 = XContentFactory.jsonBuilder().startObject(); 
        xb3.field("event_attribute_name", attrName); 
        xb3.field("event_attribute_value", attrValue); 
        xb2.value(xb3.copiedBytes()); 
    } 
    xb1.value(xb2.copiedBytes()); 
}

After indexing the data, when I read the response the data of the particular field looked like

"eventnested.event_type": ["eyJldmVudF90eXBlIjoiUXVvdGF0aW9uIiwiZXZlbnRfYXR0cmlidXRlX2luc3RhbmNlIjoiMSIsImF0dHJpYnV0ZXMiOlsiZXlKbGRtVnVkRjloZEhSeWFXSjFkR1ZmYm1GdFpTSTZJbkJsY25OdmJpQWlMQ0psZG1WdWRGOWhkSFJ5YVdKMWRHVmZkbUZzZFdVaU9pSkxZWGtnVFdGcmFIVmlaV3hoSW4wPSJdLCJhdHRyaWJ1dGVzIjpbImV5SmxkbVZ1ZEY5aGRIUnlhV0oxZEdWZmJtRnRaU0k2SW5GMWIzUmxJQ0lzSW1WMlpXNTBYMkYwZEhKcFluVjBaVjkyWVd4MVpTSTZJblJvWlNCaWIza2djbUZ3WldRZ2RHaGxJSEJoYVhJZ2IyNGdiblZ0WlhKdmRYTWdiMk5qWVhOcGIyNXpMQ0IzYVhSb0lIUm9aU0JzWVhSbGMzUWdhVzVqYVdSbGJuUWdZbVZwYm1jZ2IyNGdSbkpwWkdGNUluMD0iXX0=","eyJldmVudF90eXBlIjoiUXVvdGF0aW9uIiwiZXZlbnRfYXR0cmlidXRlX2luc3RhbmNlIjoiMSIsImF0dHJpYnV0ZXMiOlsiZXlKbGRtVnVkRjloZEhSeWFXSjFkR1ZmYm1GdFpTSTZJbkJsY25OdmJpQWlMQ0psZG1WdWRGOWhkSFJ5YVdKMWRHVmZkbUZzZFdVaU9pSkxZWGtnVFdGcmFIVmlaV3hoSW4wPSJdLCJhdHRyaWJ1dGVzIjpbImV5SmxkbVZ1ZEY5aGRIUnlhV0oxZEdWZmJtRnRaU0k2SW5GMWIzUmxJQ0lzSW1WMlpXNTBYMkYwZEhKcFluVjBaVjkyWVd4MVpTSTZJblJvWlNCMFpXVnVZV2RsY2lCb1lYTWdZbVZsYmlCd2JHRmpaV1FnYVc0Z1lTQnpZV1psZEhrZ2FHOXRaU0JoYm1RZ2QybHNiQ0JpWlNCamFHRnlaMlZrSUhkcGRHZ2djbUZ3WlNKOSJdfQ=="

1 Answer 1

25
XContentBuilder xb =  XContentFactory.jsonBuilder().startObject();

xb.startArray("eventnested");
for(int j=0;j<2;j++) {
    xb.startObject();
    xb.field("event_type", eventType);
    xb.field("event_attribute_instance", eventInstance);
    xb.startArray("attributes");
    for(int i=0;i<2;i++) {
        xb.startObject();
        xb.field("event_attribute_name", attrName);
        xb.field("event_attribute_value", attrValue);
        xb.endObject();
    }
    xb.endArray();
    xb.endObject();
}
xb.endArray();
Sign up to request clarification or add additional context in comments.

3 Comments

Great Thanks for the help imotov, that worked. BTW, the indexed data are not looking to be in the same structure that we indexed. I tried the cURL localhost:9200/2012-02-16/_search/…
response is showing the data like below, instead of an Object structure. Could u confirm if this is perfect "eventandfactnested.event_type" : [ "FamilyRelation", "Quotation", "Quotation", "PersonAttributes", "Quotation", "PersonCareer", "Quotation", "Quotation", "Quotation", "FamilyRelation", "PersonCareer", "Quotation", "CompanyLocation", "PersonRelation", "Quotation", "Quotation", "Quotation", "Quotation", "PersonEmailAddress", "Quotation", "Quotation", "Quotation", "PersonCareer", "Quotation", "PersonCareer", "PersonCareer", "Quotation", "Quotation" ]
Manoj, if my answer worked for you, please mark it as accepted. This way users will know that this question is answered and it will increase my reputation score. If you have another question, it would be better to create a new question for it. Comments don't work well for asking and answering questions, they are just too short. The short answer to your second question is original object can be found in the _source field. The long answer is I think your schema might not work for you in the long run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.