Skip to content

Commit 7f395cf

Browse files
authored
fix #2673 field stats on geo fields in elasticsearch 5.3 (#2674)
* fix #2673 field stats on geo elasticsearch 5.3 `geo_point` and `geo_shape` field types now return a `min_value` and `max_value` as per Elasticsearch 5.3 but do so as an object. This is breaking for us in 5.x so for now we keep on ignoring them. Included support for `min/max_value_as_string` which will never be null.` * make FieldMinMaxValueJsonConverter internal and move to own file
1 parent 1a43415 commit 7f395cf

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,7 @@
12101210
<Compile Include="Search\Explain\Explanation.cs" />
12111211
<Compile Include="Search\Explain\ExplanationDetail.cs" />
12121212
<Compile Include="Search\FieldStats\ElasticClient-FieldStats.cs" />
1213+
<Compile Include="Search\FieldStats\FieldMinMaxValueJsonConverter.cs" />
12131214
<Compile Include="Search\FieldStats\FieldStatsRequest.cs" />
12141215
<Compile Include="Search\FieldStats\FieldStatsResponse.cs" />
12151216
<Compile Include="Search\FieldStats\IndexConstraint.cs" />
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace Nest
5+
{
6+
internal class FieldMinMaxValueJsonConverter : JsonConverter
7+
{
8+
public override bool CanConvert(Type objectType) => true;
9+
public override bool CanWrite => false;
10+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { }
11+
12+
public override bool CanRead => true;
13+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
14+
{
15+
if (reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.StartArray)
16+
{
17+
var depth = reader.Depth;
18+
do
19+
{
20+
reader.Read();
21+
} while (reader.Depth >= depth && reader.TokenType != JsonToken.EndObject);
22+
return null;
23+
}
24+
return reader.Value.ToString();
25+
}
26+
27+
}
28+
}

src/Nest/Search/FieldStats/FieldStatsResponse.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,31 @@ public class FieldStatsField
5050
[JsonProperty("aggregatable")]
5151
public bool Aggregatable { get; internal set; }
5252

53+
/// <summary>
54+
/// Returns the min value of a field. In NEST 5.x this is always
55+
/// returned as a string which does not work for geo_point and geo_shape
56+
/// typed fields which return an object here since Elasticsearch 5.3
57+
/// so in 5.x this is always null for geo_point and geo_shape. Please use <see cref="MinValueAsString"/>
58+
/// </summary>
5359
[JsonProperty("min_value")]
60+
[JsonConverter(typeof(FieldMinMaxValueJsonConverter))]
5461
public string MinValue { get; internal set; }
5562

63+
[JsonProperty("min_value_as_string")]
64+
public string MinValueAsString { get; internal set; }
65+
66+
/// <summary>
67+
/// Returns the max value of a field. In NEST 5.x this is always
68+
/// returned as a string which does not work for geo_point and geo_shape
69+
/// typed fields which return an object here since Elasticsearch 5.3
70+
/// so in 5.x this is always null for geo_point and geo_shape. Please use <see cref="MaxValueAsString"/>
71+
/// </summary>
5672
[JsonProperty("max_value")]
73+
[JsonConverter(typeof(FieldMinMaxValueJsonConverter))]
5774
public string MaxValue { get; internal set; }
75+
76+
[JsonProperty("max_value_as_string")]
77+
public string MaxValueAsString { get; internal set; }
78+
5879
}
5980
}

src/Tests/tests.default.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# tracked by git).
66

77
# mode either u (unit test), i (integration test) or m (mixed mode)
8-
mode: u
8+
mode: i
99
# the elasticsearch version that should be started
1010
# Can be a snapshot version of sonatype or "latest" to get the latest snapshot of sonatype
1111
elasticsearch_version: 5.3.0-SNAPSHOT

0 commit comments

Comments
 (0)