kaushik mn
kaushik mn

Reputation: 76

MissingRequiredPropertyException: Missing required property 'BulkRequest.operations' Elasticsearch

I am facing the MissingRequiredPropertyException: Missing required property 'BulkRequest.operations' exception. I know what it is. But I don't know how to resolve it.

  if (Some Condition) {
   // Keep adding the optimised objects to bulk requests so can write it back later in one call.
    bulkRequestBuilder.operations(op -> op.index(idx -> idx
        .id(esKey)
        .document(JSON OBJECT)));
  } else {
    log.info("I am not building any bulk request.", esKey);
  }

Later on, I want to write this bulk request to Elastic search. While writing, I need to check if the bulk request has any operations inside it. So I am doing the below thing.

BulkRequest bulkRequest = bulkRequestBuilder.build();
if (!bulkRequest.operations().isEmpty()) {
  BulkResponse bulkResponse = repo.saveToIndexByBulkRequest(bulkRequest);
}

In the above code, .build() is throwing me the MissingRequiredPropertyException. When the bulk Request doesn't have any operations in it, it will throw that exception.

Before building, how can I check if it has any operations inside it?

Let me know if you need more info.

Upvotes: 1

Views: 3904

Answers (1)

Rajesh Lohith
Rajesh Lohith

Reputation: 446

For this particular scenario, you can use exception handling and resolve this like:

try {

  BulkRequest bulkRequest = bulkRequestBuilder.build();
  if (!bulkRequest.operations().isEmpty()) {
    BulkResponse bulkResponse = repo.save(bulkRequest);
    if (bulkResponse.errors()) {
      throw new CustomException(CustomMessage);
    }
  }
} catch (MissingRequiredPropertyException exception) {
   // either throw exception or continue based on requirement.
  log.error("Bulk RequestBuilder has no operations in it.");
}

Upvotes: 1

Related Questions