From 0b1a063c22e380fd0a5e2c3e085c5e282224f8ca Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Thu, 13 Oct 2022 11:10:45 +0100 Subject: [PATCH] Refine AggregationDitionary Add methods --- .../Common/Aggregations/AggregationDictionary.cs | 12 ++++++++++-- .../Aggregations/WritingAggregationsTests.cs | 8 ++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Elastic.Clients.Elasticsearch/Common/Aggregations/AggregationDictionary.cs b/src/Elastic.Clients.Elasticsearch/Common/Aggregations/AggregationDictionary.cs index 63a3f2b0b68..73b47ccbd59 100644 --- a/src/Elastic.Clients.Elasticsearch/Common/Aggregations/AggregationDictionary.cs +++ b/src/Elastic.Clients.Elasticsearch/Common/Aggregations/AggregationDictionary.cs @@ -36,7 +36,7 @@ public static implicit operator AggregationDictionary(Aggregation aggregator) if (b.Name.IsNullOrEmpty()) throw new ArgumentException($"{aggregator.GetType().Name}.Name is not set!"); - dict.Add(b.Name, agg); + dict.Add(agg); } return dict; } @@ -46,8 +46,16 @@ public static implicit operator AggregationDictionary(Aggregation aggregator) if (b.Name.IsNullOrEmpty()) throw new ArgumentException($"{aggregator.GetType().Name}.Name is not set!"); - return new AggregationDictionary { { b.Name, aggregator } }; + return new AggregationDictionary { { aggregator } }; } public void Add(string key, AggregationContainer value) => BackingDictionary.Add(ValidateKey(key), value); + + public void Add(AggregationContainer value) + { + if (value.Variant.Name.IsNullOrEmpty()) + throw new ArgumentException($"{value.GetType().Name}.Name is not set!"); + + BackingDictionary.Add(ValidateKey(value.Variant.Name), value); + } } diff --git a/tests/Tests/Serialization/Aggregations/WritingAggregationsTests.cs b/tests/Tests/Serialization/Aggregations/WritingAggregationsTests.cs index 976b30324b3..64247aecb78 100644 --- a/tests/Tests/Serialization/Aggregations/WritingAggregationsTests.cs +++ b/tests/Tests/Serialization/Aggregations/WritingAggregationsTests.cs @@ -37,14 +37,14 @@ public async Task CanSerializeAggregationsWrittenInVariousWays_WhichIncludeMulti var aggs = new AggregationDictionary { { - "name_of_child_agg", new ChildrenAggregation("name_of_child_agg") + new ChildrenAggregation("name_of_child_agg") { Type = "commits", Aggregations = new AggregationDictionary { - {"average_per_child", new AverageAggregation("average_per_child", "confidenceFactor")}, - {"max_per_child", new MaxAggregation("max_per_child", "confidenceFactor")}, - {"min_per_child", new MinAggregation("min_per_child", "confidenceFactor")}, + {new AverageAggregation("average_per_child", "confidenceFactor")}, + {new MaxAggregation("max_per_child", "confidenceFactor")}, + {new MinAggregation("min_per_child", "confidenceFactor")}, } } }