Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in BulkRequest java API in Elasticsearch : "The number of object passed must be even but was [1]"

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]

like image 862
Abhijit Mazumder Avatar asked Jun 24 '15 17:06

Abhijit Mazumder


1 Answers

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");
like image 119
Abhijit Mazumder Avatar answered Nov 17 '22 19:11

Abhijit Mazumder