public void PASS_Serialize()
        {
            TermsAggregate agg = new TermsAggregate(
                "name",
                "field",
                new Script("scripttext")
                {
                    Language = "js",
                    Parameters = new List<ScriptParameter>()
                    {
                        new ScriptParameter("name", "value")
                    }
                })
            {
                Exclude = new RegexPattern("excludepattern"),
                ExecutionHint = ExecutionTypeEnum.Map,
                Include = new RegexPattern("includepattern", new List<RegexFlagEnum>()
                    {
                        RegexFlagEnum.Literal,
                        RegexFlagEnum.Multiline
                    }),
                MinimumDocumentCount = 5,
                ShardSize = 10,
                Size = 7,
                SortValue = "_term"
            };

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

            string expectedJson = "{\"name\":{\"terms\":{\"field\":\"field\",\"lang\":\"js\",\"script\":\"scripttext\",\"params\":{\"name\":\"value\"},\"size\":7,\"shard_size\":10,\"min_doc_count\":5,\"order\":{\"_term\":\"asc\"},\"include\":{\"pattern\":\"includepattern\",\"flags\":\"LITERAL|MULTILINE\"},\"exclude\":\"excludepattern\",\"execution_hint\":\"map\"}}}";

            Assert.AreEqual(expectedJson, json);
        }
 public void PASS_Create()
 {
     TermsAggregate agg = new TermsAggregate("name", "field");
     Assert.IsNotNull(agg);
     Assert.AreEqual("name", agg.Name);
     Assert.AreEqual("field", agg.Field);
 }
Esempio 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.Terms.ToString()));

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

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

            if(fieldDict.ContainsKey(_EXECUTION_HINT))
            {
                ExecutionTypeEnum map = ExecutionTypeEnum.Map;
                agg.ExecutionHint = ExecutionTypeEnum.Find(fieldDict.GetString(_EXECUTION_HINT));
            }

            agg.MinimumDocumentCount = fieldDict.GetInt32(_MINIMUM_DOCUMENT_COUNT, _MINIMUM_DOCUMENT_COUNT_DEFAULT);
            agg.Size = fieldDict.GetInt32(_SIZE, _SIZE_DEFAULT);
            agg.ShardSize = fieldDict.GetInt32(_SHARD_SIZE, agg.Size);

            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());
            }

            if (fieldDict.ContainsKey(_EXCLUDE))
            {
                agg.Exclude = DeserializeRegexPattern(fieldDict.GetString(_EXCLUDE));
            }
            if (fieldDict.ContainsKey(_INCLUDE))
            {
                agg.Include = DeserializeRegexPattern(fieldDict.GetString(_INCLUDE));
            }

            agg.SubAggregations = BucketAggregationBase.DeserializeSubAggregations(aggDict);
            return agg;
        }