Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a Metrics aggregation #92

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/snap/responses/aggregation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ defmodule Snap.Aggregation do
interval
sum_other_doc_count
value
]a
]a ++
[
type: :buckets
]

def new(response) do
%__MODULE__{
Expand All @@ -30,6 +33,7 @@ defmodule Snap.Aggregation do
doc_count_error_upper_bound: integer(),
interval: integer(),
sum_other_doc_count: integer(),
value: integer()
value: integer(),
type: atom()
}
end
19 changes: 19 additions & 0 deletions lib/snap/responses/metrics_aggregation.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Snap.MetricsAggregation do
@moduledoc """
Represents an individual aggregation dictionary from a
[Search Aggregation API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html)
response.
"""
defstruct [:value, type: :metrics]

def new(response) do
%__MODULE__{
value: response
}
end

@type t :: %__MODULE__{
value: map(),
type: atom()
}
end
12 changes: 11 additions & 1 deletion lib/snap/responses/search_response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ defmodule Snap.SearchResponse do
end

def build_aggregations(aggregations) when is_map(aggregations) do
Map.new(aggregations, fn {key, value} -> {key, Snap.Aggregation.new(value)} end)
Map.new(aggregations, fn
{key, %{"buckets" => _} = value} ->
{key, Snap.Aggregation.new(value)}

{key, %{"count" => _, "min" => _, "max" => _, "avg" => _, "sum" => _} = value} ->
{key, Snap.MetricsAggregation.new(value)}

# TODO: handle other MetricsAggregations
{key, value} ->
{key, Snap.MetricsAggregation.new(value)}
end)
end

defimpl Enumerable do
Expand Down
8 changes: 4 additions & 4 deletions test/search_response_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ defmodule Snap.SearchResponseTest do
sum_other_doc_count: 0
}

assert response.aggregations["people"] == %Snap.Aggregation{
value: 8
assert response.aggregations["people"] == %Snap.MetricsAggregation{
value: %{"value" => 8}
}

assert response.aggregations["things"] == %Snap.Aggregation{
doc_count: 9
assert response.aggregations["things"] == %Snap.MetricsAggregation{
value: %{"doc_count" => 9}
}

assert response.aggregations["histogram"] == %Snap.Aggregation{
Expand Down