【发布时间】:2017-08-22 10:32:06
【问题描述】:
当我尝试在 elasticsearch 中更新文档而不指定某些字段时,它会将这些字段更新为 null 。这是我使用的代码。
public class DocumentModel {
@Id
private String id;
private Integer name;
private String gender;
private String url;
private String documentID;
------------------
------------------
getters and setters
}
用于索引文档的代码是:
Gson gson = new GsonBuilder().serializeNulls().create();
String json = gson.toJson(documentModel);
IndexRequest indexRequest = new IndexRequest(indexName, typeName, documentModel.getId());
indexRequest.source(json);
UpdateRequest updateRequest = new UpdateRequest(indexName, typeName, documentModel.getId());
updateRequest.doc(json);
updateRequest.upsert(indexRequest);
updateRequest.fields("documentID");
UpdateResponse updateResponse = elasticsearchTemplate.getClient().update(updateRequest).actionGet();
假设输入(documentModel)是(第一次索引文档):
{"id":1,"name":"tom","gender":"male","url":"http://www.google.com","documentID":1}
它将索引为:
{
"_index": "index",
"_type": "type",
"_id": "1",
"_score": 1,
"_source": {
"name":"tom",
"gender":"male",
"url":"http://www.google.com",
"documentID":1
}
}
但是当我尝试使用输入更新同一个文档时:
{"id":1,"name":"archana","gender":"female"}
它将更新为:
{
"_index": "index",
"_type": "type",
"_id": "1",
"_score": 1,
"_source": {
"name":"archana",
"gender":"female",
"url":null,
"documentID":null
}
}
问题是未作为输入给出的字段(例如'url','documentID')在更新文档时设置为null。但我希望该字段保持旧值,除非该值不为null(例如 "url":"http://www.google.com")。
【问题讨论】:
标签: java elasticsearch