2012-04-16 181 views
6

我正在使用ElasticSearch。在這裏,我想通過JAVA api爲嵌套類型字段數組索引。任何人都可以舉這個例子。我嘗試了很少,但他們失敗了。如何在Elasticsearch中爲嵌套類型的數組編制索引?

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」 反應後: [「eyJldmVudF90eXBlIjoiUXVvdGF0aW9uIiwiZXZlbnRfYXR0cmlidXRlX2luc3RhbmNlIjoiMSIsImF0dHJpYnV0ZXMiOlsiZXlKbGRtVnVkRjloZEhSeWFXSjFkR1ZmYm1GdFpTSTZJbkJsY25OdmJpQWlMQ0psZG1WdWRGOWhkSFJ5YVdKMWRHVmZkbUZzZFdVaU9pSkxZWGtnVFdGcmFIVmlaV3hoSW4wPSJdLCJhdHRyaWJ1dGVzIjpbImV5SmxkbVZ1ZEY5aGRIUnlhV0oxZEdWZmJtRnRaU0k2SW5GMWIzUmxJQ0lzSW1WMlpXNTBYMkYwZEhKcFluVjBaVjkyWVd4MVpTSTZJblJvWlNCaWIza2djbUZ3WldRZ2RHaGxJSEJoYVhJZ2IyNGdiblZ0WlhKdmRYTWdiMk5qWVhOcGIyNXpMQ0IzYVhSb0lIUm9aU0JzWVhSbGMzUWdhVzVqYVdSbG JuUWdZbVZwYm1jZ2IyNGdSbkpwWkdGNUluMD0iXX0 =」, 「eyJldmVudF90eXBlIjoiUXVvdGF0aW9uIiwiZXZlbnRfYXR0cmlidXRlX2luc3RhbmNlIjoiMSIsImF0dHJpYnV0ZXMiOlsiZXlKbGRtVnVkRjloZEhSeWFXSjFkR1ZmYm1GdFpTSTZJbkJsY25OdmJpQWlMQ0psZG1WdWRGOWhkSFJ5YVdKMWRHVmZkbUZzZFdVaU9pSkxZWGtnVFdGcmFIVmlaV3hoSW4wPSJdLCJhdHRyaWJ1dGVzIjpbImV5SmxkbVZ1ZEY5aGRIUnlhV0oxZEdWZmJtRnRaU0k2SW5GMWIzUmxJQ0lzSW1WMlpXNTBYMkYwZEhKcFluVjBaVjkyWVd4MVpTSTZJblJvWlNCMFpXVnVZV2RsY2lCb1lYTWdZbVZsYmlCd2JHRmpaV1FnYVc0Z1lTQnpZV1psZEhrZ2FHOXRaU0JoYm1RZ2QybHNiQ0JpWlNCamFHRnlaMlZrSUhkcGRHZ2djbUZ3WlNKOSJdfQ ==」

回答

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

大感謝您的幫助imotov,即工作。順便說一句,索引數據並不像我們索引的結構一樣。我試過cURL http:// localhost:9200/2012-02-16/_search /?pretty = true&fields = eventandfactnested.attributes.event_attribute_name&q = mother – Manoj 2012-04-17 10:18:07

+0

響應顯示如下的數據,而不是Object結構。你可以確認這是否完美 「eventandfactnested.event_type」:[「FamilyRelation」,「Quotation」,「Quotation」,「PersonAttributes」,「Quotation」,「PersonCareer」,「Quotation」,「Quotation」,「Quotation 「,」家庭關係「,」PersonCareer「,」Quotation「,」CompanyLocation「,」PersonRelation「,」Quotation「,」Quotation「,」Quotation「,」Quotation「,」PersonEmailAddress「,」Quotation「 「報價單」,「PersonCareer」,「Quotation」,「PersonCareer」,「PersonCareer」,「Quotation」,「Quotation」] – Manoj 2012-04-17 10:25:30

+0

Manoj,如果我的答案適合您,請將其標記爲已接受。這樣用戶就會知道這個問題得到了回答,並且會提高我的聲望得分。如果您還有其他問題,最好爲它創建一個新問題。評論對於提問和回答問題並不適用,但它們太短。第二個問題的簡短答案是可以在_source字段中找到原始對象。長期的答案是我認爲你的模式從長遠來看可能不適合你。 – imotov 2012-04-17 11:40:03

相關問題