# Elasticsearch Java Client (8.13.2) building a zero result reponse

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-client-8-13-2-building-a-zero-result-reponse/357704>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 18, 2024, 12:03pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-8-13-2-building-a-zero-result-reponse/357704 "2024-04-18T12:03:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SvenS4342](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@SvenS4342](https://discuss.elastic.co/u/SvenS4342)\
**Post date:** [April 18, 2024, 12:03pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-8-13-2-building-a-zero-result-reponse/357704/1 "2024-04-18T12:03:40Z")

</div>

Hello,

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

```auto
co.elastic.clients.elasticsearch.core.SearchResponse.of(r -> r
			.aggregations(ApiTypeHelper.undefinedMap())
			.suggest(ApiTypeHelper.undefinedMap())
			.fields(ApiTypeHelper.undefinedMap())
			.timedOut(false)
			.hits(h -> h.total(t -> t.value(0).relation(TotalHitsRelation.Eq))
				.hits(ApiTypeHelper.undefinedList()))
			.shards(s -> s.total(1).failed(0).successful(1))
			.took(0));

```

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](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/missing-required-property.html) 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  
Sven

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [May 6, 2024, 1:51pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-8-13-2-building-a-zero-result-reponse/357704/2 "2024-05-06T13:51:49Z")

</div>

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](https://github.com/elastic/elasticsearch-java/issues) 🙂

---

<div class="post-metadata">

**Author:** ![SvenS4342](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@SvenS4342](https://discuss.elastic.co/u/SvenS4342)\
**Post date:** [May 6, 2024, 2:18pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-8-13-2-building-a-zero-result-reponse/357704/3 "2024-05-06T14:18:15Z")

</div>

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](https://github.com/elastic/elasticsearch-java/issues/796)
