# Java API client - single metric aggregation zero or null deserializer

**URL:** <https://discuss.elastic.co/t/java-api-client-single-metric-aggregation-zero-or-null-deserializer/356207>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 26, 2024, 8:18pm UTC](https://discuss.elastic.co/t/java-api-client-single-metric-aggregation-zero-or-null-deserializer/356207 "2024-03-26T20:18:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Almog\_Oz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/almog_oz/32/125623_2.png) [@Almog\_Oz](https://discuss.elastic.co/u/Almog_Oz)\
**Post date:** [March 26, 2024, 8:18pm UTC](https://discuss.elastic.co/t/java-api-client-single-metric-aggregation-zero-or-null-deserializer/356207/1 "2024-03-26T20:18:11Z")

</div>

I am using elasticsearch-java api client (8.10.3) and facing a small issue and I hope someone has already encountered this issue before.

I am trying to perform a min aggregation on documents that may not contain the field I try to aggregate. In case that all the documents that match my query don't contain the field I try to aggregate, the "value": null is being deserializing to 0 via the JsonpDeserializer.doubleOrNullDeserializer(0) which is defined inside the setupSingleMetricAggregateBaseDeserializer SingleMetricAggregateBase class

> <https://github.com/elastic/elasticsearch-java/blob/main/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/aggregations/SingleMetricAggregateBase.java>

In my opinion, this approach may lead to confusion in our product and I am searching a way to avoid this deserialization but I couldn't find so far. It's ok to return null in that case and I would prefer to achieve this.

any ideas? 🙂

I tried to add also value count aggregation in order to determine if there are values in the field I try to aggregate and it worked but it is a little weird in my opinion.

---

<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:** [April 2, 2024, 2:28pm UTC](https://discuss.elastic.co/t/java-api-client-single-metric-aggregation-zero-or-null-deserializer/356207/2 "2024-04-02T14:28:39Z")

</div>

Hello!  
The Java client defaults to zero when deserializing aggregation results because of a design choice that was made in order to avoid boxing numbers, which could increase memory usage substantially.  
Using the document count provided in the response to make sure that the `0` returned is a "real zero" is a valid approach, and in general since modifying this behaviour would be a breaking change we would prefer to keep it as is for now.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 30, 2024, 2:29pm UTC](https://discuss.elastic.co/t/java-api-client-single-metric-aggregation-zero-or-null-deserializer/356207/3 "2024-04-30T14:29:34Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.

---

<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:** [September 22, 2025, 1:44pm UTC](https://discuss.elastic.co/t/java-api-client-single-metric-aggregation-zero-or-null-deserializer/356207/4 "2025-09-22T13:44:39Z")

</div>

Update: from version 9.0.0 of the Java client, the result of aggregations has been changed so that it reflects exactly what the elasticsearch server sends, so if the server returns `null`, it will be deserialized and returned as `null` by the Java client.
