Skip to content

Failing to parse empty geo_centroid #3000

@MaJaHa95

Description

@MaJaHa95

NEST/Elasticsearch.Net version: 5.6.0

Elasticsearch version: 6.1.1

Description of the problem including expected versus actual behavior:

Geo Centroid aggregate fails to parse with no results.

Essentially, NEST is unable to parse responses that look like this:

{
  ...
  "hits" : {
    "total" : 0
    ...
  },
  "aggregations" : {
    "centroid" : {
      "count" : 0
    }
  }
}

This resulted from a query that returned no results, with a Geo Centroid aggregation applied. But my actual issue first showed up in an empty sub-aggregation on a Date Histogram. Either way, I believe same root cause (albeit different exceptions).

Looking into the code and stack trace, I believe the issue is happening here:

var min = reader.Value as double?;

The ReadAggregate function in AggregateJsonConverter is looking at the name of the first property in the aggregation result, finding count, and incorrectly concluding that it's a Stats Aggregation. When it tries to parse it as a stats aggregation, it's surprised to find that that's the only property, so an exception is thrown.

Essentially, Elasticsearch doesn't include the centroid property when there are no records present, which is logical enough. But NEST doesn't parse that response properly.

I believe the issue itself is relatively clear, but looking at this all, I'm still not sure how to resolve it on my end. Because the parsing is occurring at query time, I'm not given the opportunity to stop it when doc_count is 0 on the parent aggregation.

As for a PR, the best I can think would be to check for whether the only property is count, and parse it as a GeoCentroidAggregate if so, with a null Location. But assuming an aggregate with only that property is inherently a geo_centroid seems error-prone, even if it does happen to be correct for now.

Any suggestions on a workaround in my code? I'm open to ideas. I might go with the arithmetic mean of the results of a geo_bounds, since it doesn't have this same limitation. Of course, that's not necessarily the centroid, but it's probably real-world close enough.

For reference, geo_bounds works because it does not include the count property (or any properties) when the parent is empty, so it just parses as null. Convenient, but not helpful for us here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions