Esempio n. 1
0
        public IBucket GetRangeBucket(ref JsonReader reader, IJsonFormatterResolver formatterResolver, string key, string propertyName)
        {
            string fromAsString       = null;
            string fromString         = null;
            string toAsString         = null;
            string toString           = null;
            long?  docCount           = null;
            double?toDouble           = null;
            double?fromDouble         = null;
            var    isSubAggregateName = false;

            while (true)
            {
                switch (propertyName)
                {
                case Parser.From:
                    var currentFromJsonToken = reader.GetCurrentJsonToken();
                    if (currentFromJsonToken == JsonToken.Number)
                    {
                        fromDouble = reader.ReadDouble();
                    }
                    else if (currentFromJsonToken == JsonToken.String)
                    {
                        fromString = reader.ReadString();
                    }
                    else
                    {
                        reader.ReadNext();
                    }
                    break;

                case Parser.To:
                    var currentJsonToToken = reader.GetCurrentJsonToken();
                    if (currentJsonToToken == JsonToken.Number)
                    {
                        toDouble = reader.ReadDouble();
                    }
                    else if (currentJsonToToken == JsonToken.String)
                    {
                        toString = reader.ReadString();
                    }
                    else
                    {
                        reader.ReadNext();
                    }
                    break;

                case Parser.Key:
                    key = reader.ReadString();
                    break;

                case Parser.FromAsString:
                    fromAsString = reader.ReadString();
                    break;

                case Parser.ToAsString:
                    toAsString = reader.ReadString();
                    break;

                case Parser.DocCount:
                    docCount = reader.ReadNullableLong().GetValueOrDefault(0);
                    break;

                default:
                    isSubAggregateName = true;
                    break;
                }

                if (isSubAggregateName || reader.GetCurrentJsonToken() == JsonToken.EndObject)
                {
                    break;
                }

                reader.ReadNext();                 // ,
                propertyName = reader.ReadPropertyName();
            }

            Dictionary <string, IAggregate> subAggregates = null;

            if (isSubAggregateName)
            {
                subAggregates = GetSubAggregates(ref reader, propertyName, formatterResolver);
            }

            IBucket bucket;

            if (fromString != null || toString != null)
            {
                bucket = new IpRangeBucket(subAggregates)
                {
                    Key      = key,
                    DocCount = docCount.GetValueOrDefault(),
                    From     = fromString,
                    To       = toString,
                }
            }
            ;
            else
            {
                bucket = new RangeBucket(subAggregates)
                {
                    Key          = key,
                    From         = fromDouble,
                    To           = toDouble,
                    DocCount     = docCount.GetValueOrDefault(),
                    FromAsString = fromAsString,
                    ToAsString   = toAsString,
                }
            };

            return(bucket);
        }
Esempio n. 2
0
        public IBucket GetRangeBucket(JsonReader reader, JsonSerializer serializer, string key = null)
        {
            string fromAsString = null, toAsString = null;
            long?  docCount = null;
            double?toDouble = null, fromDouble = null;

            var readExpectedProperty = true;

            while (readExpectedProperty)
            {
                switch (reader.Value as string)
                {
                case Parser.From:
                    reader.Read();
                    if (reader.ValueType == typeof(double))
                    {
                        fromDouble = (double)reader.Value;
                    }
                    reader.Read();
                    break;

                case Parser.To:
                    reader.Read();
                    if (reader.ValueType == typeof(double))
                    {
                        toDouble = (double)reader.Value;
                    }
                    reader.Read();
                    break;

                case Parser.Key:
                    key = reader.ReadAsString();
                    reader.Read();
                    break;

                case Parser.FromAsString:
                    fromAsString = reader.ReadAsString();
                    reader.Read();
                    break;

                case Parser.ToAsString:
                    toAsString = reader.ReadAsString();
                    reader.Read();
                    break;

                case Parser.DocCount:
                    reader.Read();
                    docCount = (reader.Value as long?).GetValueOrDefault(0);
                    reader.Read();
                    break;

                default:
                    readExpectedProperty = false;
                    break;
                }
            }

            var nestedAggregations = this.GetSubAggregates(reader, serializer);

            var bucket = new RangeBucket(nestedAggregations)
            {
                Key          = key,
                From         = fromDouble,
                To           = toDouble,
                DocCount     = docCount.GetValueOrDefault(),
                FromAsString = fromAsString,
                ToAsString   = toAsString,
            };

            return(bucket);
        }
		public IBucket GetRangeBucket(JsonReader reader, JsonSerializer serializer, string key = null)
		{
			string fromAsString = null, toAsString = null;
			long? docCount = null;
			double? toDouble = null, fromDouble = null;

			var readExpectedProperty = true;
			while (readExpectedProperty)
			{
				switch (reader.Value as string)
				{
					case "from":
						reader.Read();
						if (reader.ValueType == typeof(double))
							fromDouble = (double)reader.Value;
						reader.Read();
						break;
					case "to":
						reader.Read();
						if (reader.ValueType == typeof(double))
							toDouble = (double)reader.Value;
						reader.Read();
						break;
					case "key":
						key = reader.ReadAsString();
						reader.Read();
						break;
					case "from_as_string":
						fromAsString = reader.ReadAsString();
						reader.Read();
						break;
					case "to_as_string":
						toAsString = reader.ReadAsString();
						reader.Read();
						break;
					case "doc_count":
						reader.Read();
						docCount = (reader.Value as long?).GetValueOrDefault(0);
						reader.Read();
						break;
					default:
						readExpectedProperty = false;
						break;
				}
			}

			var bucket = new RangeBucket
			{
				Key = key,
				From = fromDouble,
				To = toDouble,
				DocCount = docCount.GetValueOrDefault(),
				FromAsString = fromAsString,
				ToAsString = toAsString,
				Aggregations = this.GetSubAggregates(reader, serializer)
			};

			return bucket;
		}