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

I am trying to use a map with Bulk Insert Api from 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 the following exception The number of the passed object should be even, but it was [1]

+3


source to share


1 answer


Ok I got the fix: Use Map<String, Object>

insteadMap <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 the card

A map is a collection of key: values ​​palettes. It represents the 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");

      

+3


source







All Articles