Пример #1
0
        private static IRangeQuery GetRangeQuery(JsonSerializer serializer, JObject jo)
        {
            var isNumeric = false;
            var isLong    = false;

            foreach (var property in jo.Properties())
            {
                if (property.Name == "format" || property.Name == "time_zone")
                {
                    return(FromJson.ReadAs <DateRangeQuery>(jo.CreateReader(), serializer));
                }

                if (_rangeKeys.Contains(property.Name))
                {
                    if (property.Value.Type == JTokenType.Float)
                    {
                        isNumeric = true;
                    }
                    else if (property.Value.Type == JTokenType.Integer)
                    {
                        isLong = true;
                    }
                }
            }
            if (isNumeric)
            {
                return(FromJson.ReadAs <NumericRangeQuery>(jo.CreateReader(), serializer));
            }
            if (isLong)
            {
                return(FromJson.ReadAs <LongRangeQuery>(jo.CreateReader(), serializer));
            }

            return(FromJson.ReadAs <DateRangeQuery>(jo.CreateReader(), serializer));
        }
        protected override object DeserializeJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            reader.Read();             //property name
            var fieldName = reader.Value as string;

            reader.Read();             // {

            var         jo          = JObject.Load(reader);
            var         typePropery = jo.Property("type");
            IMatchQuery query       = null;
            var         r           = jo.CreateReader();

            switch (typePropery?.Value.Value <string>() ?? "")
            {
            case "phrase":
                query = FromJson.ReadAs <MatchPhraseQuery>(r, objectType, existingValue, serializer);
                break;

            case "phrase_prefix":
                query = FromJson.ReadAs <MatchPhrasePrefixQuery>(r, objectType, existingValue, serializer);
                break;

            default:
                query = FromJson.ReadAs <MatchQuery>(r, objectType, existingValue, serializer);
                break;
            }

            if (query == null)
            {
                return(null);
            }
            query.Field = fieldName;

            return(query);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jo     = JObject.Load(reader);
            var filter = jo.Property("filter")?.Value.ToObject <QueryContainer>(serializer);
            var weight = jo.Property("weight")?.Value.ToObject <double?>();

            ;
            IScoreFunction function = null;

            foreach (var prop in jo.Properties())
            {
                switch (prop.Name)
                {
                case "exp":
                case "gauss":
                case "linear":
                    var properties = prop.Value.Value <JObject>().Properties().ToList();
                    var fieldProp  = properties.First(p => p.Name != "multi_value_mode");
                    var field      = fieldProp.Name;
                    var f          = ReadDecayFunction(prop.Name, fieldProp.Value.Value <JObject>(), serializer);
                    f.Field = field;
                    var mv = properties.FirstOrDefault(p => p.Name == "multi_value_mode")?.Value;
                    if (mv != null)
                    {
                        f.MultiValueMode = serializer.Deserialize <MultiValueMode>(mv.CreateReader());
                    }
                    function = f;

                    break;

                case "random_score":
                    function = FromJson.ReadAs <RandomScoreFunction>(prop.Value.Value <JObject>().CreateReader(), serializer);
                    break;

                case "field_value_factor":
                    function = FromJson.ReadAs <FieldValueFactorFunction>(prop.Value.Value <JObject>().CreateReader(), serializer);
                    break;

                case "script_score":
                    function = FromJson.ReadAs <ScriptScoreFunction>(prop.Value.Value <JObject>().CreateReader(), serializer);
                    break;
                }
            }
            if (function == null && weight.HasValue)
            {
                function = new WeightFunction {
                    Weight = weight
                }
            }
            ;
            else if (function == null)
            {
                return(null);                                   //throw new Exception("error deserializing function score function");
            }
            function.Weight = weight;
            function.Filter = filter;
            return(function);
        }
Пример #4
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var j = JObject.Load(reader);

            if (!j.HasValues)
            {
                return(null);
            }

            var firstProp = j.Properties().FirstOrDefault();

            if (firstProp == null)
            {
                return(null);
            }

            var field = firstProp.Name;
            var jo    = firstProp.Value.Value <JObject>();

            if (jo == null)
            {
                return(null);
            }


            var isNumeric = !jo.Properties().Any(p => p.Name == "format" || p.Name == "time_zone") &&
                            jo.Properties().Any(p => _rangeKeys.Contains(p.Name) && (p.Value.Type == JTokenType.Integer || p.Value.Type == JTokenType.Float));


            IRangeQuery fq;

            if (isNumeric)
            {
                fq = FromJson.ReadAs <NumericRangeQuery>(jo.CreateReader(), serializer);
            }
            else
            {
                fq = FromJson.ReadAs <DateRangeQuery>(jo.CreateReader(), serializer);
            }

            fq.Name  = GetPropValue <string>(jo, "_name");
            fq.Boost = GetPropValue <double?>(jo, "boost");
            fq.Field = field;

            return(fq);
        }
Пример #5
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            ISort sort = null;

            reader.Read();
            var field = reader.Value as string;

            if (field == "_geo_distance")
            {
                reader.Read();
                var jObject         = JObject.Load(reader);
                var geoLocationProp = jObject.Properties().First(p => p.Value.Type == JTokenType.Array);
                using (var r = jObject.CreateReader())
                {
                    var s = FromJson.ReadAs <GeoDistanceSort>(r, objectType, existingValue, serializer);
                    s.Field = geoLocationProp.Name;
                    using (var rr = geoLocationProp.Value.CreateReader())
                        s.Points = FromJson.ReadAs <List <GeoLocation> >(rr, objectType, existingValue, serializer);
                    sort = s;
                }
            }
            else if (field == "_script")
            {
                reader.Read();
                var s = FromJson.ReadAs <ScriptSort>(reader, objectType, existingValue, serializer);
                sort = s;
            }
            else
            {
                reader.Read();
                var s = FromJson.ReadAs <SortField>(reader, objectType, existingValue, serializer);
                s.Field = field;
                sort    = s;
            }
            reader.Read();
            return(sort);
        }
Пример #6
0
 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) =>
 FromJson.ReadAs <T>(reader, serializer);
Пример #7
0
 private TProperty ReadProperty <TProperty>(JObject j, JsonSerializer s)
     where TProperty : IProperty
 {
     return(FromJson.ReadAs <TProperty>(j.CreateReader(), s));
 }