如何在Elasticsearch中索引嵌套类型的数组?

时间:2012-04-16 07:18:11

标签: elasticsearch

我正在研究ElasticSearch。在这里,我想通过JAVA api索引嵌套类型字段的数组。任何人都可以举一个例子。我尝试了很少但他们失败了。

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

索引数据后,当我读取响应时的数据 特定字段看起来像

“eventnested.event_type”: [ “eyJldmVudF90eXBlIjoiUXVvdGF0aW9uIiwiZXZlbnRfYXR0cmlidXRlX2luc3RhbmNlIjoiMSIsImF0dHJpYnV0ZXMiOlsiZXlKbGRtVnVkRjloZEhSeWFXSjFkR1ZmYm1GdFpTSTZJbkJsY25OdmJpQWlMQ0psZG1WdWRGOWhkSFJ5YVdKMWRHVmZkbUZzZFdVaU9pSkxZWGtnVFdGcmFIVmlaV3hoSW4wPSJdLCJhdHRyaWJ1dGVzIjpbImV5SmxkbVZ1ZEY5aGRIUnlhV0oxZEdWZmJtRnRaU0k2SW5GMWIzUmxJQ0lzSW1WMlpXNTBYMkYwZEhKcFluVjBaVjkyWVd4MVpTSTZJblJvWlNCaWIza2djbUZ3WldRZ2RHaGxJSEJoYVhJZ2IyNGdiblZ0WlhKdmRYTWdiMk5qWVhOcGIyNXpMQ0IzYVhSb0lIUm9aU0JzWVhSbGMzUWdhVzVqYVdSbGJuUWdZbVZwYm1jZ2IyNGdSbkpwWkdGNUluMD0iXX0 =”,“eyJldmVudF90eXBlIjoiUXVvdGF0aW9uIiwiZXZlbnRfYXR0cmlidXRlX2luc3RhbmNlIjoiMSIsImF0dHJpYnV0ZXMiOlsiZXlKbGRtVnVkRjloZEhSeWFXSjFkR1ZmYm1GdFpTSTZJbkJsY25OdmJpQWlMQ0psZG1WdWRGOWhkSFJ5YVdKMWRHVmZkbUZzZFdVaU9pSkxZWGtnVFdGcmFIVmlaV3hoSW4wPSJdLCJhdHRyaWJ1dGVzIjpbImV5SmxkbVZ1ZEY5aGRIUnlhV0oxZEdWZmJtRnRaU0k2SW5GMWIzUmxJQ0lzSW1WMlpXNTBYMkYwZEhKcFluVjBaVjkyWVd4MVpTSTZJblJvWlNCMFpXVnVZV2RsY2lCb1lYTWdZbVZsYmlCd2JHRmpaV1FnYVc0Z1lTQnpZV1psZEhrZ2FHOXRaU0JoYm1RZ2QybHNiQ0JpWlNCamFHRnlaMlZrSUh kcGRHZ2djbUZ3WlNKOSJdfQ ==“

1 个答案:

答案 0 :(得分:21)

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