Elasticsearch Exception:The number of object passed must be even but was [1]

Elasticsearch java批量导入的对象必须是map?
使用Person实体类做了个实验:

List list = getWords();//包含bean的List
BulkResponse response;
String Id = "";
if(!list.isEmpty()) {
	Iterator iterator = list.iterator();
	BulkRequestBuilder bulkRequest=client.prepareBulk();
	while (iterator.hasNext()) {
		Person person = (Person) iterator.next();
		bulkRequest.add(client.prepareIndex("experience", "Person").setSource(person));
		response = bulkRequest.execute().actionGet();
		Id = response.hasFailures()?"failed":"success";
	}
}

结果抛出了异常。
Exception:The number of object passed must be even but was [1]

找到org.elasticsearch.action.index.IndexRequest类
发现public IndexRequest source(XContentType xContentType, Object… source)的第二行中
source需要有length属性,所以传入的source对象必须有length。

public IndexRequest source(XContentType xContentType, Object... source) {
        if(source.length % 2 != 0) {
            throw new IllegalArgumentException("The number of object passed must be even but was [" + source.length + "]");
        } else if(source.length == 2 && source[0] instanceof BytesReference && source[1] instanceof Boolean) {
            throw new IllegalArgumentException("you are using the removed method for source with bytes and unsafe flag, the unsafe flag was removed, please just use source(BytesReference)");
        } else {
            try {
                XContentBuilder e = XContentFactory.contentBuilder(xContentType);
                e.startObject();

                for(int i = 0; i < source.length; ++i) {
                    e.field(source[i++].toString(), source[i]);
                }

                e.endObject();
                return this.source(e);
            } catch (IOException var5) {
                throw new ElasticsearchGenerationException("Failed to generate", var5);
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值