5

I am implementing a bulk update operation using an OpenSearch Java client for existing documents stored in the OpenSearch provisioned by AWS.

The BulkRequest request object looks like this:

BulkRequest request = new BulkRequest.Builder().operations(o -> o.update(u -> u.index(indexName).id(String.valueOf(id)).document(doc))).build();
        
openSearchClient.bulk(request);

The doc object is a POJO object that reflects a document inside the index.

public class ProgramSelectionInfoBase implements Serializable {

    private static final long serialVersionUID = 6880667215923483985L;

    private long id;
    private String status35;
    // other fields, getters, setters.

}

The response I am getting is 400 Bad Request

In the debug mode, the parsed bulk request looks different as it should per the bulk update documentation. It lacks the "doc" parent object, which should wrap the partially updated document:

{ "update" : { "_id" : "361710", "_index" : "program_search" }}
{ "status35": "draft" }. <- doc is missing.


{ "update": { "_index": "movies", "_id": "tt0816711" } }
{ "doc": { "title": "World War Z" } }

If I replace a bulk update request with a single document update request, I get successful results. The parsed request structure matches the documentation one:

UpdateRequest<Object, Object> request = new UpdateRequest.Builder<>().id(String.valueOf(id)).doc(doc).index(indexName).build();
openSearchClient.update(request, Object.class);

{"doc":{"status35":"active"}}

Question: Am I building a BulkRequest object incorrectly? Or this is the client library issue?

UPDATE I am using the latest available OpenSearch Java client version

Hutsul
  • 1,535
  • 4
  • 31
  • 51
  • Looks like someone or the questioner has opened a bug issue ticket. Leaving it here to be traced -> https://github.com/opensearch-project/opensearch-java/issues/590 – Panagiotis Bougioukos Aug 12 '23 at 21:16
  • could you please try the following code and report if the error is the same? `UpdateOperation updateOperation = new UpdateOperation.Builder<>().document(doc).index(indexName).id(id).build(); BulkOperation bulkOperation = new BulkOperation.Builder().update(updateOperation).build(); request.operations().add(bulkOperation);` – Panagiotis Bougioukos Aug 12 '23 at 21:25
  • @PanagiotisBougioukos, thanks for the suggestion. I will try and let you know. – Hutsul Aug 14 '23 at 17:39
  • @PanagiotisBougioukos, I tried your suggestion above and it didn't work I am still getting 400 responses. The parsed JSON looks incorrect. ```{ "update" : { "_id" : "361710", "_index" : "program_search" }} { "status35": "closed" }.``` – Hutsul Aug 15 '23 at 00:08

1 Answers1

0

I come up with the two workarounds while waiting for the response:

1.The current requirements allow me to switch to a single document update option. As mentioned in the question it works successfully. However, there is a high probability that in future I would need to update a set of documents, which is better to do using the bulk option.

2.Create a class wrapper. It will have a doc field which allows receiving a required request structure after converting the object to JSON:

public class UpdateRootDocument<T> {

    T doc;

    public UpdateRootDocument(T doc) {
        this.doc = doc;
    }

    public T getDoc() {
        return doc;
    }

    public void setDoc(T doc) {
        this.doc = doc;
    }
}
Hutsul
  • 1,535
  • 4
  • 31
  • 51