版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33212500/article/details/81460898
ES中
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id);
jsonObject.put("age", 25);
jsonObject.put("name", "j-" + new Random(100).nextInt());
jsonObject.put("createTime", DateUtil.formatDate(new Date()));
String ids = ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));
当使用这个格式插入数据,
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id);
//jsonObject.put("age", 31);
jsonObject.put("name", "wpfffff");
// jsonObject.put("date", new Date());*/
ElasticsearchUtil.updateDataById(jsonObject, indexName, esType, id);
这种格式修改数据时,
期望:ES中只有这两个字段有值
结果:age和createTime也是有值的
EsDemo demo = new EsDemo();
demo.setId(Long.parseLong(id));
demo.setName("wpf111");
JSONObject jsonObject = (JSONObject)JSONObject.toJSON(demo);
ElasticsearchUtil.updateDataById(jsonObject, indexName, esType, id);
System.out.println(jsonObject);
使用这种格式修改数据时,
其他字段也会被置为null(新增的时候插入的字段的值)
原因,bean再转jsonObject时,bean中未null的值也会被转,只是打印时没看到,但是遍历key时会发现还是有该key,
所以修改时,该key的值在ES中会被置为null