Elasticsearch Java Client (8.13.2) building a zero result reponse


for a use-case I'm creating I want to build a SearchResponse similar to a response which did not retrieve any matched documents.

co.elastic.clients.elasticsearch.core.SearchResponse.of(r -> r
			.hits(h -> h.total(t -> t.value(0).relation(TotalHitsRelation.Eq))
			.shards(s -> s.total(1).failed(0).successful(1))

However when building the response as shown above an exception: co.elastic.clients.util.MissingRequiredPropertyException: Missing required property 'HitsMetadata.hits' will be thrown. According to MissingRequiredPropertyException in a response | Elasticsearch Java API Client [8.13] | Elastic this is due to the properties missing not being optional. However checking an actual 0 documents matched response from ES I did not see the missing properties either which are "id" and "index". After defining the properties "id" and "index" I am able to build the object however it then differs from the actual object parsed from the real ES zero result response.

Maybe I overlooked some utility functions that are designed for this use-case.
Thanks in advance!

Best Regards

Hello and welcome!
You are correct this is indeed a bug in the java client. Passing ApiTypeHelper.undefinedList() as argument should make the builder ignore the details of whatever class the array is made of, such as which fields are nullable or not. Thank you for finding this, feel free to open an issue in the github repo :slight_smile:

Hello Laura,
thanks a lot for your response!
I created the following issue on Github: Building a zero result response declares missing properties as mandatory which should be optional · Issue #796 · elastic/elasticsearch-java · GitHub