/// <summary> /// Aggregate resuts /// </summary> /// <param name="key">Key identifier of results</param> /// <param name="field">Id field uppon the aggregation will be used</param> /// <param name="aggType">Type of Aggregation</param> public FindRequest <T> Aggregation(string key, Expression <Func <T, object> > field, EnAggregationType aggType) { AggregationContainer aggr = null; switch (aggType) { case EnAggregationType.Sum: aggr = new SumAggregation(key, field); break; case EnAggregationType.Average: aggr = new AverageAggregation(key, field); break; case EnAggregationType.Min: aggr = new MinAggregation(key, field); break; case EnAggregationType.Max: aggr = new MaxAggregation(key, field); break; } if (aggr != null) { searchInfo.Aggregations.Add(key, aggr); } return(this); }
public void GetAggregatedValueTest() { var cell1 = new Mock <Cell>(); var cell2 = new Mock <Cell>(); var cell3 = new Mock <Cell>(); cell1.SetupGet(foo => foo.Content).Returns((double)0.5); cell2.SetupGet(foo => foo.Content).Returns((double)0.5); cell3.SetupGet(foo => foo.Content).Returns((double)2); List <Cell> cells = new List <Cell>(); cells.Add(cell1.Object); cells.Add(cell2.Object); AverageAggregation target = new AverageAggregation(); double expected = 0.5; double actual; actual = (double)target.GetAggregatedValue(cells); Assert.AreEqual(expected, actual); cells.Add(cell3.Object); expected = 1; actual = (double)target.GetAggregatedValue(cells); Assert.AreEqual(expected, actual); }
/// <summary> /// Aggregate resuts over a clause /// </summary> /// <param name="groupField">Field that will be used as term to group aggregation results</param> /// <param name="field">Id field uppon the aggregation will be used</param> /// <param name="aggType">Type of Aggregation</param> public FindRequest <T> TermAggregation(Expression <Func <T, object> > groupField, Expression <Func <T, object> > field, EnAggregationType aggType) { //Get term var termName = Utils.ExpressionAttributeName(groupField); TermsAggregation term = null; if (!searchInfo.termDictionary.ContainsKey(termName)) { term = new TermsAggregation(termName) { Field = groupField }; searchInfo.termDictionary.Add(termName, term); } else { term = searchInfo.termDictionary[termName]; } if (term != null) { //if(agg) AggregationContainer aggr = null; string pKey = ""; switch (aggType) { case EnAggregationType.Sum: pKey = string.Format("{0}_sum", termName); aggr = new SumAggregation(pKey, field); break; case EnAggregationType.Average: pKey = string.Format("{0}_avg", termName); aggr = new AverageAggregation(pKey, field); break; case EnAggregationType.Min: pKey = string.Format("{0}_min", termName); aggr = new MinAggregation(pKey, field); break; case EnAggregationType.Max: pKey = string.Format("{0}_max", termName); aggr = new MaxAggregation(pKey, field); break; } if (aggr != null) { if (term.Aggregations == null) { term.Aggregations = new AggregationDictionary(); } term.Aggregations.Add(pKey, aggr); } } return(this); }