snap icon indicating copy to clipboard operation
snap copied to clipboard

stats aggregations

Open nickkaltner opened this issue 3 years ago • 5 comments

I can't seem to get stats aggregations to work. I get back something like

"unit_price_stats" => %Snap.Aggregation{
      buckets: nil,
      doc_count: nil,
      doc_count_error_upper_bound: nil,
      interval: nil,
      sum_other_doc_count: nil,
      value: nil
    }

I think the problem is that elasticsearch doesn't return buckets etc. for stats aggregations - it returns something like

"aggregations": {
    "grades_stats": {
      "count": 2,
      "min": 50.0,
      "max": 100.0,
      "avg": 75.0,
      "sum": 150.0
    }
  }

nickkaltner avatar Oct 23 '22 00:10 nickkaltner

Thanks - can you share the query you're using?

tomtaylor avatar Nov 11 '23 13:11 tomtaylor

Hi @tomtaylor this is the documentation for the stats aggregation: https://www.elastic.co/guide/en/elasticsearch/reference/8.11/search-aggregations-metrics-stats-aggregation.html I was surprised that this doesn't follow the standard aggregation. It seems to be a special aggregation.

Maybe an additional struct StatsAggregation? And then one can match on the values of the map to determine whether to return a Snap.Aggregation or a Snap.StatsAggregation?

Something along the lines of:

  def build_aggregations(aggregations) when is_map(aggregations) do
    Map.new(aggregations, fn 
      {key, %{"count" => _, "min" => _, "max" => _, "avg" => _, "sum" => _} = value} -> {key, Snap.StatsAggregation.new(value)}
      {key, value} -> {key, Snap.Aggregation.new(value)}
      end)
  end

Ivor avatar Nov 28 '23 16:11 Ivor

yeah something as simple as that would be awesome, but at a high level elasticsearch/opensearch support 2 types of aggregations, one is called a bucket aggregation which seems to be supported and the other type is a metrics aggregation which isn't.

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html

nickkaltner avatar Feb 11 '24 08:02 nickkaltner

even if we had a generic aggregation where it just returned the ES data as json it would at least mean that there was some way to get it with Snap

nickkaltner avatar Feb 11 '24 08:02 nickkaltner