简体   繁体   中英

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]

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM