Skip to content

Commit d024097

Browse files
committed
Correctly serialize _geo_distance sort
This commit updates the SortFormatter to emit a value separator when serializing a _geo_distance only when the IGeoDistanceSort contains properties with values. This is determined by checking the serialized bytes are more than 2 i.e. an empty JSON object, {}. Add additional _geo_distance sort to SortUsage tests to cover when IGeoDistanceSort contains/doesn't contain properties with values. (cherry picked from commit 0564747)
1 parent a2f98b6 commit d024097

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Nest/CommonOptions/Sorting/SortFormatter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public void Serialize(ref JsonWriter writer, ISort value, IJsonFormatterResolver
103103
for (var i = buffer.Offset; i < buffer.Count - 1; i++)
104104
writer.WriteRawUnsafe(buffer.Array[i]);
105105

106-
if (buffer.Count > 0)
106+
// does the IGeoDistanceSort have other properties set i.e. is it more than simply {} ?
107+
if (buffer.Count > 2)
107108
writer.WriteValueSeparator();
108109

109110
writer.WritePropertyName(settings.Inferrer.Field(geo.Field));

src/Tests/Tests/Search/Request/SortUsageTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ public SortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(clust
7979
}
8080
},
8181
new
82+
{
83+
_geo_distance = new
84+
{
85+
locationPoint = new[]
86+
{
87+
new
88+
{
89+
lat = 70.0,
90+
lon = -70.0
91+
},
92+
new
93+
{
94+
lat = -12.0,
95+
lon = 12.0
96+
}
97+
}
98+
}
99+
},
100+
new
82101
{
83102
_script = new
84103
{
@@ -127,6 +146,10 @@ public SortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(clust
127146
.Mode(SortMode.Min)
128147
.Points(new GeoLocation(70, -70), new GeoLocation(-12, 12))
129148
)
149+
.GeoDistance(g => g
150+
.Field(p => p.LocationPoint)
151+
.Points(new GeoLocation(70, -70), new GeoLocation(-12, 12))
152+
)
130153
.Script(sc => sc
131154
.Type("number")
132155
.Ascending()
@@ -175,6 +198,11 @@ public SortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(clust
175198
Mode = SortMode.Min,
176199
Points = new[] { new GeoLocation(70, -70), new GeoLocation(-12, 12) }
177200
},
201+
new GeoDistanceSort
202+
{
203+
Field = "locationPoint",
204+
Points = new[] { new GeoLocation(70, -70), new GeoLocation(-12, 12) }
205+
},
178206
new ScriptSort
179207
{
180208
Type = "number",

0 commit comments

Comments
 (0)