Skip to content

Commit 4bac810

Browse files
Refine AggregationDitionary Add methods (#6748) (#6752)
Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent 57f0d97 commit 4bac810

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Elastic.Clients.Elasticsearch/Common/Aggregations/AggregationDictionary.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static implicit operator AggregationDictionary(Aggregation aggregator)
3636
if (b.Name.IsNullOrEmpty())
3737
throw new ArgumentException($"{aggregator.GetType().Name}.Name is not set!");
3838

39-
dict.Add(b.Name, agg);
39+
dict.Add(agg);
4040
}
4141
return dict;
4242
}
@@ -46,8 +46,16 @@ public static implicit operator AggregationDictionary(Aggregation aggregator)
4646
if (b.Name.IsNullOrEmpty())
4747
throw new ArgumentException($"{aggregator.GetType().Name}.Name is not set!");
4848

49-
return new AggregationDictionary { { b.Name, aggregator } };
49+
return new AggregationDictionary { { aggregator } };
5050
}
5151

5252
public void Add(string key, AggregationContainer value) => BackingDictionary.Add(ValidateKey(key), value);
53+
54+
public void Add(AggregationContainer value)
55+
{
56+
if (value.Variant.Name.IsNullOrEmpty())
57+
throw new ArgumentException($"{value.GetType().Name}.Name is not set!");
58+
59+
BackingDictionary.Add(ValidateKey(value.Variant.Name), value);
60+
}
5361
}

tests/Tests/Serialization/Aggregations/WritingAggregationsTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public async Task CanSerializeAggregationsWrittenInVariousWays_WhichIncludeMulti
3737
var aggs = new AggregationDictionary
3838
{
3939
{
40-
"name_of_child_agg", new ChildrenAggregation("name_of_child_agg")
40+
new ChildrenAggregation("name_of_child_agg")
4141
{
4242
Type = "commits",
4343
Aggregations = new AggregationDictionary
4444
{
45-
{"average_per_child", new AverageAggregation("average_per_child", "confidenceFactor")},
46-
{"max_per_child", new MaxAggregation("max_per_child", "confidenceFactor")},
47-
{"min_per_child", new MinAggregation("min_per_child", "confidenceFactor")},
45+
{new AverageAggregation("average_per_child", "confidenceFactor")},
46+
{new MaxAggregation("max_per_child", "confidenceFactor")},
47+
{new MinAggregation("min_per_child", "confidenceFactor")},
4848
}
4949
}
5050
}

0 commit comments

Comments
 (0)