1.失败,使用TransportClient

    public static void bulkInsert(TransportClient client) throws IOException {
List<Person> ps=new ArrayList<Person>();
Person one=new Person();
one.setId("1");
one.setAddress("add");
one.setMobile("1111");
one.setSex("f");
one.setUsername("www");
ps.add(one); Person two=new Person();
two.setId("2");
two.setAddress("add");
two.setMobile("2222");
two.setSex("f");
two.setUsername("www");
ps.add(two); Persons pss=new Persons();
pss.setPersons(ps);
pss.setUuid("uiid"); String jsonStr=JSON.toJSONString(pss);
System.out.println(jsonStr); IndexResponse response = client.prepareIndex("www", "www").setSource(jsonStr).get();
System.out.println("创建成功!"); }

原因是:setSource方法不支持json,源码如下:

    /**
* Constructs a simple document with a field name and value pairs.
* <p>
* <b>Note: the number of objects passed to this method must be an even
* number. Also the first argument in each pair (the field name) must have a
* valid String representation.</b>
* </p>
*/
public IndexRequestBuilder setSource(Object... source) {
request.source(source);
return this;
}

2.成功,使用RestHighLevelClient 可以直接使用json

    public static void InsertByJson(RestHighLevelClient client) throws IOException {
List<Person> ps=new ArrayList<Person>();
Person one=new Person();
one.setId("1");
one.setAddress("add");
one.setMobile("1111");
one.setSex("f");
one.setUsername("www");
ps.add(one); Person two=new Person();
two.setId("2");
two.setAddress("add");
two.setMobile("2222");
two.setSex("f");
two.setUsername("www");
ps.add(two); Persons pss=new Persons();
pss.setPersons(ps);
pss.setUuid("uiid"); String jsonStr=JSON.toJSONString(pss);
System.out.println(jsonStr);
IndexRequest req = new IndexRequest("www", "www");
req.source(jsonStr, XContentType.JSON);
IndexResponse response = client.index(req);
System.out.println("创建成功!"); }

The number of object passed must be even but was [1]的更多相关文章

  1. ES添加文档 踩坑之 —— The number of object passed must be even but was [1]

    读取文件,获取json格式的数据doc, 然后使用 bulkRequestBuilder.add(client.prepareIndex(index, type, id).setSource(doc) ...

  2. ElasticSearch-The number of object passed must be even but was [1]-问题解决

    ES版本:6.4.3 1.The number of object passed must be even but was [1] 问题代码: IndexRequest indexRequest = ...

  3. Javascript中类型: undefined, number ,string ,object ,boolean

    var a1; var a2 = true;var a3 = 1;var a4 = "Hello";var a5 = new Object();var a6 = null;var ...

  4. [bug] Unrecognized token 'code': was expecting (JSON String, Number, Array, Object,'true', 'false' or 'null')

    JSON格式有误,需用JSON.stringify()函数转换一下 参考 https://www.cnblogs.com/sunyanblog/p/13788740.html https://www. ...

  5. elasticsearch-java异常

    1. Unsupported major.minor version 52.0 java的jdk版本过低导致,需要更换为jdk1.8+ 2. elasticsearch 的version在pom中提示 ...

  6. elasticsearch 导入基础数据并索引之 geo_point

    elasticsearch 中的地理信息存储, 有geo_point形式和geo_shape两种形式 此篇只叙述geo_point, 地理位置需要声明为特殊的类型, 不显示在mapping中定义的话, ...

  7. SAP NUMBER RANGE维护配置object FBN1 Deletion only possible if status is initial

    背景: 错误日志: SAP FBN1 Deletion only possible if status is initial 场景: 如果目标机已有NUMBER RANGE 不为0,需要删除配置年为9 ...

  8. Chapter 3 Discovering Classes and Object

    Chatper 3 Discovering Classes and Object Exercises: 1.What is a class? A class is a template for man ...

  9. Directive Definition Object

    不知道为什么这个我并没有想翻译过来的欲望,或许我并没有都看熟透,不好误人子弟,原版奉上. Here's an example directive declared with a Directive D ...

随机推荐

  1. Could not load file or assembly '$SharePoint.Project.AssemblyFullName$'

    The fix is simple, do the following: 1.  Open your project file in NotePad 2.  Find the PropertyGrou ...

  2. C++一些函数的意义

    1.重载 : C++ 允许多个函数拥有相同的名字,只要它们的参数列表不同就可以,这就是函数的重载 2.隐藏和覆盖的区别 IF 子类的函数与父类的名称相同,但是参数不同 父类函数被隐藏(还存在) ELS ...

  3. 面向对象进阶-类的内置方法 __str__ 、__repr__、__len__、__del__、__call__(三)

    # 内置的类方法 和 内置的函数之间有着千丝万缕的联系# 双下方法# obj.__str__ str(obj)# obj.__repr__ repr(obj) # def __str__(self): ...

  4. IAP远程在线升级

    IAP远程在线升级 在上一篇中实现了LWIP网口通讯,那么肯定要加个在线升级功能,这个功能所占用的资源很少,但在物联网中很重要也很实用.在线升级就是像手机一样,先下载好系统,然后点击升级~然后就没然后 ...

  5. 646. Maximum Length of Pair Chain

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  6. C#后端接收form-data,创建实体类

    public class Para_list //实体类 { public long ParemeterID { get; set; } public string Name { get; set; ...

  7. 字符串(tjoi2016,heoi2016,bzoj4556)(sam(后缀自动机)+线段树合并+倍增+二分答案)

    佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了 一个长为\(n\)的字符串\(s\),和\(m\)个问题.佳媛姐姐必须正确回答这\(m\)个问题, ...

  8. Python 将字典的元素按照键或者值的大小进行排序

    在开发的过程中有时遇到这样的需求,一个字典里保存了一份完整的数据,其中键是一个id,值是时间,需要获取最新的5条数据,处理方式如下: 假设字典数据的变量名为my_dict data_list = so ...

  9. 标准的sql执行顺序

    正常情况下是先join再进行where过滤

  10. hello lua

    http://manual.luaer.cn/ http://www.lua.org/pil/contents.html #include <cstdio> #include <st ...