Elasticsearch java批量导入的对象必须是map?
使用Person实体类做了个实验:
List list = getWords();//包含bean的List
BulkResponse response;
String Id = "";
if(!list.isEmpty()) {
Iterator iterator = list.iterator();
BulkRequestBuilder bulkRequest=client.prepareBulk();
while (iterator.hasNext()) {
Person person = (Person) iterator.next();
bulkRequest.add(client.prepareIndex("experience", "Person").setSource(person));
response = bulkRequest.execute().actionGet();
Id = response.hasFailures()?"failed":"success";
}
}
结果抛出了异常。
Exception:The number of object passed must be even but was [1]
找到org.elasticsearch.action.index.IndexRequest类
发现public IndexRequest source(XContentType xContentType, Object… source)的第二行中
source需要有length属性,所以传入的source对象必须有length。
public IndexRequest source(XContentType xContentType, Object... source) {
if(source.length % 2 != 0) {
throw new IllegalArgumentException("The number of object passed must be even but was [" + source.length + "]");
} else if(source.length == 2 && source[0] instanceof BytesReference && source[1] instanceof Boolean) {
throw new IllegalArgumentException("you are using the removed method for source with bytes and unsafe flag, the unsafe flag was removed, please just use source(BytesReference)");
} else {
try {
XContentBuilder e = XContentFactory.contentBuilder(xContentType);
e.startObject();
for(int i = 0; i < source.length; ++i) {
e.field(source[i++].toString(), source[i]);
}
e.endObject();
return this.source(e);
} catch (IOException var5) {
throw new ElasticsearchGenerationException("Failed to generate", var5);
}
}
}