public void RangeGroup() { QueryBuilder builder = new QueryBuilder(); builder.Filtered.Filters.Add(FilterType.Must, new MovingTimeRange("@timestamp", 86400)); var rangeAggregate = new RangeAggregate( "TotalDuration", new Range(null, 100), new Range(100, 500), new Range(500, null) ); var rangeGroup = new SubAggregate(rangeAggregate); rangeGroup.Aggregates.Add("count", new CountAggregate("TotalDuration")); rangeGroup.Aggregates.Add("avg", new AverageAggregate("TotalDuration")); builder.Aggregates.Add("range_group", rangeGroup); builder.PrintQuery(Client.IndexDescriptors); AggregateResult result = Client.ExecuteAggregate(builder); result.PrintResult(); Assert.IsNotNull(result.GetValue("range_group.buckets")); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Dictionary<string, object> wholeDict = serializer.Deserialize<Dictionary<string, object>>(reader); Dictionary<string, object> aggDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(wholeDict.First().Value.ToString()); Dictionary<string, object> fieldDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(aggDict.GetString(AggregationTypeEnum.Range.ToString())); string aggName = wholeDict.First().Key; string field = fieldDict.GetStringOrDefault(_FIELD); Script script = fieldDict.DeserializeObject<Script>(); List<RangeBucket> buckets = new List<RangeBucket>(); List<Dictionary<string, object>> bucketDictList = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(fieldDict.GetString(_RANGES)); foreach (Dictionary<string, object> bucketDict in bucketDictList) { RangeBucket bucket = DeserializeRangeBucket(bucketDict); buckets.Add(bucket); } RangeAggregate agg = null; if (!string.IsNullOrWhiteSpace(field) && script != null) agg = new RangeAggregate(aggName, field, script, buckets); else if (!string.IsNullOrWhiteSpace(field)) agg = new RangeAggregate(aggName, field, buckets); else if (script != null) agg = new RangeAggregate(aggName, script, buckets); else throw new RequiredPropertyMissingException(_FIELD + "/" + Script.SCRIPT); agg.SubAggregations = BucketAggregationBase.DeserializeSubAggregations(aggDict); return agg; }
public void PASS_Serialize() { RangeAggregate agg = new RangeAggregate("name", "field", new List<RangeBucket>() { new RangeBucket() { GreaterThan = 50, LessThan = 100 } }); string json = JsonConvert.SerializeObject(agg); Assert.IsNotNull(json); string expectedJson = "{\"name\":{\"range\":{\"field\":\"field\",\"ranges\":[{\"gt\":50,\"lt\":100}]}}}"; Assert.AreEqual(expectedJson, json); }
public void PASS_Create() { RangeAggregate agg = new RangeAggregate("name", "field", new List<RangeBucket>() { new RangeBucket() { GreaterThan = 50, LessThan = 100 } }); Assert.IsNotNull(agg); Assert.AreEqual("name", agg.Name); Assert.AreEqual("field", agg.Field); Assert.AreEqual((int)50, Convert.ToInt32(agg.Ranges.First().GreaterThan)); Assert.AreEqual((int)100, Convert.ToInt32(agg.Ranges.First().LessThan)); }