Skip to content

More lenient extended_stats deserialization #4777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions src/Nest/Aggregations/AggregateFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ internal class AggregateFormatter : IJsonFormatter<IAggregate>
{ Parser.Hits, 2 },
};

private static readonly AutomataDictionary ExtendedStatsFields = new AutomataDictionary
{
{ "variance", 0 },
{ "std_deviation", 1 },
{ "std_deviation_bounds", 2 }
};

private static readonly byte[] ValueAsStringField = JsonWriter.GetEncodedPropertyNameWithoutQuotation(Parser.ValueAsString);

static AggregateFormatter()
Expand Down Expand Up @@ -547,10 +554,10 @@ private IAggregate GetStatsAggregate(ref JsonReader reader, IJsonFormatterResolv
if (reader.GetCurrentJsonToken() == JsonToken.EndObject)
return statsMetric;

return GetExtendedStatsAggregate(ref reader, statsMetric, meta);
return GetExtendedStatsAggregate(ref reader, formatterResolver, statsMetric, meta);
}

private IAggregate GetExtendedStatsAggregate(ref JsonReader reader, StatsAggregate statsMetric, IReadOnlyDictionary<string, object> meta)
private IAggregate GetExtendedStatsAggregate(ref JsonReader reader, IJsonFormatterResolver formatterResolver, StatsAggregate statsMetric, IReadOnlyDictionary<string, object> meta)
{
var extendedStatsMetric = new ExtendedStatsAggregate
{
Expand All @@ -564,35 +571,31 @@ private IAggregate GetExtendedStatsAggregate(ref JsonReader reader, StatsAggrega

extendedStatsMetric.SumOfSquares = reader.ReadNullableDouble();
reader.ReadNext(); // ,
reader.ReadNext(); // "variance"
reader.ReadNext(); // :
extendedStatsMetric.Variance = reader.ReadNullableDouble();
reader.ReadNext(); // ,
reader.ReadNext(); // "std_deviation"
reader.ReadNext(); // :
extendedStatsMetric.StdDeviation = reader.ReadNullableDouble();

if (reader.GetCurrentJsonToken() != JsonToken.EndObject)
while (reader.GetCurrentJsonToken() != JsonToken.EndObject)
{
var bounds = new StandardDeviationBounds();
reader.ReadNext(); // ,
reader.ReadNext(); // "std_deviation_bounds"
reader.ReadNext(); // :
reader.ReadNext(); // {
reader.ReadNext(); // "upper"
reader.ReadNext(); // :
bounds.Upper = reader.ReadNullableDouble();
reader.ReadNext(); // ,
reader.ReadNext(); // "lower"
reader.ReadNext(); // :
bounds.Lower = reader.ReadNullableDouble();
reader.ReadNext(); // }
extendedStatsMetric.StdDeviationBounds = bounds;
}
var propertyName = reader.ReadPropertyNameSegmentRaw();
if (ExtendedStatsFields.TryGetValue(propertyName, out var value))
{
switch (value)
{
case 0:
extendedStatsMetric.Variance = reader.ReadNullableDouble();
break;
case 1:
extendedStatsMetric.StdDeviation = reader.ReadNullableDouble();
break;
case 2:
extendedStatsMetric.StdDeviationBounds =
formatterResolver.GetFormatter<StandardDeviationBounds>().Deserialize(ref reader, formatterResolver);
break;
}
}
else
reader.ReadNextBlock();

// read any remaining _as_string fields
while (reader.GetCurrentJsonToken() != JsonToken.EndObject)
reader.ReadNextBlock();
reader.ReadIsValueSeparator();
}

return extendedStatsMetric;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

namespace Nest
using System.Runtime.Serialization;

namespace Nest
{
public class ExtendedStatsAggregate : StatsAggregate
{
Expand All @@ -12,9 +14,13 @@ public class ExtendedStatsAggregate : StatsAggregate
public double? Variance { get; set; }
}

[DataContract]
public class StandardDeviationBounds
{
[DataMember(Name = "lower")]
public double? Lower { get; set; }

[DataMember(Name = "upper")]
public double? Upper { get; set; }
}
}