public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Dictionary<string, object> fieldDict = serializer.Deserialize<Dictionary<string, object>>(reader); if (fieldDict.ContainsKey(QueryTypeEnum.Indices.ToString())) fieldDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(fieldDict.First().Value.ToString()); List<string> indices = new List<string>(); if (fieldDict.ContainsKey(_INDEX)) indices.Add(fieldDict.GetString(_INDEX)); else if (fieldDict.ContainsKey(_INDICES)) indices = JsonConvert.DeserializeObject<List<string>>(fieldDict.GetString(_INDICES)); else throw new RequiredPropertyMissingException(_INDEX + "/" + _INDICES); IQuery matchingQuery = JsonConvert.DeserializeObject<IQuery>(fieldDict[_QUERY].ToString()); IndicesQuery query = null; NonMatchingTypeEnum nonMatching = NonMatchingTypeEnum.None; nonMatching = NonMatchingTypeEnum.Find(fieldDict.GetString(_NON_MATCHING_QUERY)); if (nonMatching != null) query = new IndicesQuery(indices, matchingQuery, nonMatching); else query = new IndicesQuery(indices, matchingQuery, JsonConvert.DeserializeObject<IQuery>(fieldDict.GetString(_NON_MATCHING_QUERY))); query.QueryName = fieldDict.GetStringOrDefault(QuerySerializer._QUERY_NAME); return query; }
public void FAIL_CreateQuery_Indices() { try { IndicesQuery query = new IndicesQuery(null, null, NonMatchingTypeEnum.All); Assert.Fail(); } catch (ArgumentNullException argEx) { Assert.AreEqual("indices", argEx.ParamName); } }
public void PASS_CreateQuery_None() { IndicesQuery query = new IndicesQuery( new List<string>() { "1", "2", "3" }, new TermQuery("field", "value"), NonMatchingTypeEnum.None); Assert.IsNotNull(query); Assert.AreEqual(3, query.Indices.Count()); Assert.IsTrue(query.MatchingQuery is TermQuery); Assert.AreEqual("field", (query.MatchingQuery as TermQuery).Field); Assert.AreEqual("value", (query.MatchingQuery as TermQuery).Value); Assert.AreEqual(NonMatchingTypeEnum.None, query.NonMatchingQueryType); }
public void FAIL_CreateQuery_MatchingQuery() { try { IndicesQuery query = new IndicesQuery( new List<string>() { "1" }, null, NonMatchingTypeEnum.All); Assert.Fail(); } catch (ArgumentNullException argEx) { Assert.AreEqual("matchingQuery", argEx.ParamName); } }
public void FAIL_CreateQuery_NonMatchingQuery() { try { IndicesQuery query = new IndicesQuery( new List<string>() { "1" }, new TermQuery("field", "value"), (IQuery)null); Assert.Fail(); } catch (ArgumentNullException argEx) { Assert.AreEqual("nonMatchingQuery", argEx.ParamName); } }
public void PASS_Serialize_Index() { IndicesQuery query = new IndicesQuery( new List<string>() { "1" }, new TermQuery("field", "value"), new TermQuery("field", "value")); string result = JsonConvert.SerializeObject(query); Assert.IsNotNull(result); string expectedJson = "{\"indices\":{\"index\":\"1\",\"query\":{\"term\":{\"field\":\"value\"}},\"no_match_query\":{\"term\":{\"field\":\"value\"}}}}"; Assert.AreEqual(expectedJson, result); }