Reputation: 205
When i tried to update a document in elasticsearch without specifying some fields,it updates that fields to null . Here is the code i used.
public class DocumentModel {
@Id
private String id;
private Integer name;
private String gender;
private String url;
private String documentID;
------------------
------------------
getters and setters
}
Code used for indexing document is :
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();
Suppose the input(documentModel) is(indexing the document first time):
{"id":1,"name":"tom","gender":"male","url":"http://www.google.com","documentID":1}
it will index as :
{
"_index": "index",
"_type": "type",
"_id": "1",
"_score": 1,
"_source": {
"name":"tom",
"gender":"male",
"url":"http://www.google.com",
"documentID":1
}
}
But when i tried to update same document with input :
{"id":1,"name":"archana","gender":"female"}
it will update as :
{
"_index": "index",
"_type": "type",
"_id": "1",
"_score": 1,
"_source": {
"name":"archana",
"gender":"female",
"url":null,
"documentID":null
}
}
The problem is fields that are not given as input (eg 'url','documentID') is set to null while updating document.but i want that field remains to be with the old value unless the value is not null(eg as "url":"http://www.google.com").
Upvotes: 2
Views: 5642
Reputation: 1494
I think your problem is serializeNulls
. The docs say:
Configure Gson to serialize null fields. By default, Gson omits all fields that are null during serialization.
I think that means you're sending a request to Elasticsearch that looks something like:
POST /index/type/1/_update
{
"doc": {
"name":"archana",
"gender":"female",
"url":null,
"documentID":null
}
}
Which tells Elasticsearch you want to write over those values with null
. If you don't want to write over them, you need to leave them completely out of the request, which I think you can accomplish just by getting rid of the serializeNulls()
call.
This is something you have to handle in your code, or in a scripted update (though I can't think of a reason you would want to do that, unless you don't control the code making the request), or you could write a plugin to add this behavior.
Upvotes: 3
Reputation: 116
Doing delta updates is supported by Elastic by using the _update Rest API call, so I assume the Java API should do the same if you do
updateRequest.update(indexRequest)
instead of
updateRequest.upsert(indexRequest)
Note that an explicit update would fail if the document don't exist, so you would need to check that before issuing the update command.
Providing Elastic and Java API versions would be useful
Upvotes: 0