Example #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Dictionary<string, object> dict = serializer.Deserialize<Dictionary<string, object>>(reader);

            MatchQueryTypeEnum matchType = MatchQueryTypeEnum.Boolean;
            matchType = MatchQueryTypeEnum.Find(dict.First().Key);

            if (matchType != null)
                dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(dict.First().Value.ToString());
            else if (dict.ContainsKey(_FIELDS))
                matchType = MatchQueryTypeEnum.Multi;

            MatchQueryBase query = null;
            if (matchType == MatchQueryTypeEnum.Multi)
            {
                MultiMatchQuery multiMatchQuery = new MultiMatchQuery();
                if (dict.ContainsKey(_FIELDS))
                    multiMatchQuery.Fields = JsonConvert.DeserializeObject<List<string>>(dict[_FIELDS].ToString());
                if (dict.ContainsKey(_QUERY))
                    multiMatchQuery.Query = dict[_QUERY].ToString();
                if (dict.ContainsKey(_USE_DIS_MAX))
                    multiMatchQuery.UseDisMaxQuery = Convert.ToBoolean(dict[_USE_DIS_MAX]);
                else
                    multiMatchQuery.UseDisMaxQuery = _USE_DIS_MAX_DEFAULT;
                if (dict.ContainsKey(_TIE_BREAKER))
                    multiMatchQuery.TieBreakerMultiplier = Convert.ToDouble(dict[_TIE_BREAKER]);
                else
                    multiMatchQuery.TieBreakerMultiplier = _TIE_BREAKER_DEFAULT;

                ReadFieldsDict(dict, multiMatchQuery);

                query = multiMatchQuery;
            }
            else
            {
                QueryTypeEnum queryType = QueryTypeEnum.Match;
                queryType = QueryTypeEnum.Find(matchType.ToString());
                KeyValuePair<string, object> kvp = dict.FirstOrDefault(x => !_FieldKeys.Contains(x.Key));

                query = GenerateMatchQuery(queryType);
                if (!string.IsNullOrWhiteSpace(kvp.Key))
                {
                    query.Field = kvp.Key;
                    query.Query = kvp.Value.ToString();
                }

                ReadFieldsDict(dict, query);
            }

            if (query == null)
                throw new Exception("Unable to deserialize match query.");            

            return query;
        }
 public void PASS_CreateQuery()
 {
     MultiMatchQuery query = new MultiMatchQuery(
         new List<BoostedField>()
         {
             new BoostedField("field1")
         }, "value1");
     Assert.IsNotNull(query);
     Assert.AreEqual("field1", query.Fields.First());
     Assert.AreEqual("value1", query.Query);
 }
 public void FAIL_CreateQuery_Field()
 {
     try
     {
         MultiMatchQuery query = new MultiMatchQuery(new List<BoostedField>(), "value1");
         Assert.Fail();
     }
     catch (ArgumentNullException argEx)
     {
         Assert.AreEqual("boostedFields", argEx.ParamName);
     }
 }
        public void PASS_Serialize()
        {
            MultiMatchQuery query = new MultiMatchQuery(
                new List<BoostedField>()
                {
                    new BoostedField("field1")
                }, "value1");
            string json = JsonConvert.SerializeObject(query);

            Assert.IsNotNull(json);

            string expectedJson = "{\"multi_match\":{\"fields\":[\"field1\"],\"query\":\"value1\"}}";
            Assert.AreEqual(expectedJson, json);
        }
 public void FAIL_CreateQuery_Query()
 {
     try
     {
         MultiMatchQuery query = new MultiMatchQuery(
         new List<BoostedField>()
         {
             new BoostedField("field1")
         }, null);
         Assert.Fail();
     }
     catch (ArgumentNullException argEx)
     {
         Assert.AreEqual("query", argEx.ParamName);
     }
 }