I am trying to use map with Bulk Insert Api of ElasticSearch Java Api
public void bulkInsert(List<Map<String,String>> listOfObjects ){
BulkRequestBuilder bulkRequest = client.prepareBulk();
Iterator<Map<String,String>> itr = listOfObjects.iterator();
if (itr.hasNext()){
Map<String,String> document = itr.next();
bulkRequest.add(client.prepareIndex(index, type)
.setSource(document));
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
System.out.println(bulkResponse.buildFailureMessage());
}
}
And I am calling this with
Map<String,String> jsonMap = new HashMap<String,String>();
jsonMap.put("name", fullName.toString());
jsonMap.put("file", file);
List<Map<String,String>> listOfObjects = new ArrayList<Map<String,String>>();
listOfObjects.add(jsonMap);
indexService.bulkInsert(listOfObjects);
I am getting following exception The number of object passed must be even but was [1]
Ok I got the fix :
Use Map<String, Object>
instead of Map <String,String>
Map<String,Object> jsonMap = new HashMap<String,Object>();
jsonMap.put("name", fullName.toString());
jsonMap.put("file", file);
List<Map<String,Object>> listOfObjects = new ArrayList<Map<String,Object>>();
listOfObjects.add(jsonMap);
indexService.bulkInsert(listOfObjects);
From ES java api;
Using Map
Map is a key:values pair collection. It represents a JSON structure:
Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With