Elasticsearch Java Api client single metric aggregation deserializer

27 views Asked by At

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.

1

There are 1 answers

1
G0l0s On

You can filter documents without a field by the exist query

Sample documents

POST /filter_without_field/_bulk
{"create":{"_id":1}}
{"comment":"document with field","field": 1}
{"create":{"_id":2}}
{"comment":"document without field"}
{"create":{"_id":3}}
{"comment":"document with field","field": 10}

Query with a filter and an aggregation

GET /filter_without_field/_search?filter_path=aggregations
{
    "query": {
        "bool": {
            "filter": [
                {
                    "exists": {
                        "field": "field"
                    }
                }
            ]
        }
    },
    "aggs": {
        "min_field_value": {
            "min": {
                "field": "field"
            }
        }
    }
}

Response

{
    "aggregations" : {
        "min_field_value" : {
            "value" : 1.0
        }
    }
}

Or you can use the missing parameter with a great number

POST /filter_without_field/_search?filter_path=aggregations
{
    "aggs": {
        "min_field_value": {
            "min": {
                "field": "field",
                "missing": 1000
            }
        }
    }
}

Response is the same

Convert a favorite query to Java code