Update attributes inside a Document partially

Versions (relevant - OpenSearch/Dashboard/Server OS/Browser):
OpenSearch: 2.11
opensearch-java: 2.9.1
opensearch-rest-client : 2.13.0

Describe the issue:
I have a Document with following attributes:

  • examTitle
  • examStartDate
  • examEndDate
  • examQuestionText

I wanted to update only examTitle and examStartDate, so I can do that without changing anything to the other fields

Configuration:

Relevant Logs or Screenshots:

You can achieve that by using the update document API which supports partial update:

POST your_index/_update/{doc_id}
{
  "doc": {
    "examTitle" : "xx",
    "examStartDate" : "xx"
  }
}

, here is the documentation: Update document - OpenSearch Documentation.

Hi @gaobinlong ,

Thanks for the reply. I have gone through that document before even putting my question here. But unfortunately I am not able to build the UpdateRequest which I can use for the update function.

Do you have any sample java code which I can take as a reference to build the request ?

Also I am not sure which one is best in my case, update() or updateByQuery().

Thanks,
Deba

Seems that a parameter is missing, try this:

UpdateResponse updateResponse = openSearchClient.update(updateRequest, Index.class);

.
If you want to update only one document, use update(), if update multiple documents filtered by a query, then use updateByQuery().