Reputation: 205
Here is a part of my code for index or update document. I want to get a specific field value 'documentID' which is in '_source'.Here 'json' is JSONObject of document model.
String jsonForUpdate = json.toString();
String uuid = UUID.randomUUID().toString();
json.put("documentID", uuid);
String jsonForIndex = json.toString();
IndexRequest indexRequest = new IndexRequest(indexName, typeName, documentModel.getId());
indexRequest.source(jsonForIndex);
UpdateResponse updateResponse = elasticsearchTemplate.getClient().prepareUpdate(indexName, typeName , documentModel.getId()).setDoc(jsonForUpdate).setUpsert(indexRequest).setFields("documentID").get();
I tried this code to get value for document field.
updateResponse.getGetResult().getFields().get("documentID").getValue().toString();
But it didnt work for me during updating the document.works fine during indexing the document.
Upvotes: 2
Views: 606
Reputation: 1862
If you look at: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
you can see the following parameter that can be passed on update:
_source
Allows to control if and how the updated source should be returned in the response. By default the updated source is not returned. See source filtering for details.
This mean that in your prepareUpdate
you can use the setFetchSource(true)
method, although this will bring the full document.
Update: For version 2.3.3 you can still use:
/**
* Explicitly specify the fields that will be returned. By default, nothing is returned.
*/
public UpdateRequestBuilder setFields(String... fields) {
request.fields(fields);
return this;
}
Elasticsearch doc: https://www.elastic.co/guide/en/elasticsearch/reference/2.3/docs-update.html
The method is available on the UpdateRequestBuilder, which is the class of the object returned by your prepareUpdate.
Upvotes: 1