public void FAIL_CreateQuery_ChildType()
 {
     try
     {
         HasChildQuery query = new HasChildQuery(null, new TermQuery("field", "value"));
         Assert.Fail();
     }
     catch (ArgumentNullException argEx)
     {
         Assert.AreEqual("childType", argEx.ParamName);
     }
 }
 public void FAIL_CreateQuery_Query()
 {
     try
     {
         HasChildQuery query = new HasChildQuery("type", null);
         Assert.Fail();
     }
     catch (ArgumentNullException argEx)
     {
         Assert.AreEqual("query", argEx.ParamName);
     }
 }
        public void PASS_Serialize()
        {
            HasChildQuery query = new HasChildQuery("type", new TermQuery("field", "value"))
            {
                ScoreType = ScoreTypeEnum.Maximum
            };

            string json = JsonConvert.SerializeObject(query);

            Assert.IsNotNull(json);

            string expectedJson = "{\"has_child\":{\"type\":\"type\",\"score_type\":\"max\",\"query\":{\"term\":{\"field\":\"value\"}}}}";
            Assert.AreEqual(expectedJson, json);
        }
        public void PASS_CreateQuery()
        {
            HasChildQuery query = new HasChildQuery("type", new TermQuery("field", "value"))
            {
                ScoreType = ScoreTypeEnum.Maximum
            };

            Assert.IsNotNull(query);
            Assert.AreEqual("type", query.ChildType);
            Assert.AreEqual(ScoreTypeEnum.Maximum, query.ScoreType);
            Assert.IsTrue(query.Query is TermQuery);
            Assert.AreEqual("field", (query.Query as TermQuery).Field);
            Assert.AreEqual("value", (query.Query as TermQuery).Value);
        }
Пример #5
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Dictionary<string, object> queryDict = serializer.Deserialize<Dictionary<string, object>>(reader);
            if(queryDict.ContainsKey(QueryTypeEnum.HasChild.ToString()))
                queryDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(queryDict.First().Value.ToString());

            string childType = queryDict.GetString(_CHILD_TYPE);
            IQuery query = JsonConvert.DeserializeObject<IQuery>(queryDict.GetString(_QUERY));

            HasChildQuery childQuery = new HasChildQuery(childType, query);
            ScoreTypeEnum scoreType = ScoreTypeEnum.None;
            childQuery.ScoreType = ScoreTypeEnum.Find(queryDict.GetStringOrDefault(_SCORE_TYPE));
            if (scoreType == null)
                throw new Exception(queryDict[_SCORE_TYPE].ToString() + " is not an allowed score type for the has child query.");

            childQuery.QueryName = queryDict.GetStringOrDefault(QuerySerializer._QUERY_NAME);

            return childQuery;
        }