public void PASS_Create()
 {
     HistogramAggregate agg = new HistogramAggregate("name", "field", 50);
     Assert.IsNotNull(agg);
     Assert.AreEqual("name", agg.Name);
     Assert.AreEqual("field", agg.Field);
     Assert.AreEqual((int)50, Convert.ToInt32(agg.Interval));
 }
        public void PASS_Serialize()
        {
            HistogramAggregate agg = new HistogramAggregate("name", "field", 50)
            {
                MinimumDocumentCount = 2,
                AreBucketsKeyed = true
            };

            string json = JsonConvert.SerializeObject(agg);
            Assert.IsNotNull(json);

            string expectedJson = "{\"name\":{\"histogram\":{\"field\":\"field\",\"interval\":50.0,\"min_doc_count\":2,\"keyed\":true}}}";
            Assert.AreEqual(expectedJson, json);
        }
Exemplo n.º 3
0
        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.Histogram.ToString()));

            string aggName = wholeDict.First().Key;
            string field = fieldDict.GetStringOrDefault(_FIELD);
            Script script = fieldDict.DeserializeObject<Script>();
            Double interval = fieldDict.GetDouble(_INTERVAL);

            HistogramAggregate agg = new HistogramAggregate(
                wholeDict.First().Key,
                fieldDict.GetStringOrDefault(_FIELD),
                fieldDict.GetDouble(_INTERVAL));

            if (!string.IsNullOrWhiteSpace(field) && script != null)
                agg = new HistogramAggregate(aggName, field, script, interval);
            else if (!string.IsNullOrWhiteSpace(field))
                agg = new HistogramAggregate(aggName, field, interval);
            else if (script != null)
                agg = new HistogramAggregate(aggName, script, interval);
            else
                throw new RequiredPropertyMissingException(_FIELD + "/" + Script.SCRIPT); 

            agg.MinimumDocumentCount = fieldDict.GetInt32(_MINIMUM_DOCUMENT_COUNT, _MINIMUM_DOCUMENT_COUNT_DEFAULT);
            if (fieldDict.ContainsKey(_ORDER))
            {
                Dictionary<string, object> orderDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(fieldDict.GetString(_ORDER));
                if (orderDict.Count != 1)
                    throw new Exception("The order parameter must be a dictionary of one key value pair.");

                agg.SortValue = orderDict.First().Key;
                agg.SortOrder = SortOrderEnum.Find(orderDict.First().Value.ToString());
            }
            agg.AreBucketsKeyed = fieldDict.GetBool(_KEYED, _KEYED_DEFAULT);
            agg.SubAggregations = BucketAggregationBase.DeserializeSubAggregations(aggDict);
            return agg;
        }