Elasticsearch中BulkRequest java API中的错误:"传递的对象数必须是偶数,但是[1]"

时间:2015-06-24 17:59:37

标签: java elasticsearch

我正在尝试使用ElasticSearch Java Api的Bulk Insert 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());
    }   

}

我用

来称呼它
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);

我得到以下异常 传递的对象数必须是偶数,但是[1]

1 个答案:

答案 0 :(得分:3)

好的,我得到了修复: 使用Map<String, Object>代替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);

来自ES java api;

使用地图

地图是一个关键:值对集合。它代表一个JSON结构:

Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");