breakroom / snap

An Elasticsearch/OpenSearch client for Elixir
MIT License
70 stars 27 forks source link

stats aggregations #33

Open nickkaltner opened 1 year ago

nickkaltner commented 1 year ago

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
    }
  }
tomtaylor commented 10 months ago

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

Ivor commented 9 months ago

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
nickkaltner commented 7 months ago

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 commented 7 months ago

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