Skip to content

Commit 24203a3

Browse files
committed
Fix #1407: Add distance type to geo distance sort
1 parent c90ff77 commit 24203a3

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/Nest/DSL/Search/SortGeoDistanceDescriptor.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,25 @@ public interface IGeoDistanceSort : ISort, ICustomJson
1515
string PinLocation { get; set; }
1616
IEnumerable<string> Points { get; set; }
1717
GeoUnit? GeoUnit { get; set; }
18+
GeoDistance? DistanceType { get; set; }
1819
}
1920

2021
public class GeoDistanceSort : SortBase, IGeoDistanceSort
2122
{
22-
internal static List<string> Params = new List<string> { "missing", "mode", "order", "unit" };
23+
internal static List<string> Params = new List<string> { "missing", "mode", "order", "unit", "distance_type" };
2324

2425
internal static int MissingIndex = 0;
2526
internal static int ModeIndex = 1;
2627
internal static int OrderIndex = 2;
2728
internal static int UnitIndex = 3;
29+
internal static int DistanceTypeIndex = 4;
2830

2931
public PropertyPathMarker Field { get; set; }
3032
public string PinLocation { get; set; }
3133
public IEnumerable<string> Points { get; set; }
3234
public GeoUnit? GeoUnit { get; set; }
33-
35+
public GeoDistance? DistanceType { get; set; }
36+
3437
object ICustomJson.GetCustomJson()
3538
{
3639
var sort = this.Points.HasAny() ? (object)this.Points : this.PinLocation;
@@ -40,7 +43,8 @@ object ICustomJson.GetCustomJson()
4043
{ Params[MissingIndex], this.Missing },
4144
{ Params[ModeIndex], this.Mode},
4245
{ Params[OrderIndex], this.Order },
43-
{ Params[UnitIndex], this.GeoUnit }
46+
{ Params[UnitIndex], this.GeoUnit },
47+
{ Params[DistanceTypeIndex], this.DistanceType }
4448
};
4549
}
4650
}
@@ -55,7 +59,9 @@ public class SortGeoDistanceDescriptor<T> : SortDescriptorBase<T, SortGeoDistanc
5559
IEnumerable<string> IGeoDistanceSort.Points { get; set; }
5660

5761
GeoUnit? IGeoDistanceSort.GeoUnit { get; set; }
58-
62+
63+
GeoDistance? IGeoDistanceSort.DistanceType { get; set; }
64+
5965
public SortGeoDistanceDescriptor<T> PinTo(string geoLocationHash)
6066
{
6167
geoLocationHash.ThrowIfNullOrEmpty("geoLocationHash");
@@ -93,6 +99,13 @@ public SortGeoDistanceDescriptor<T> Unit(GeoUnit unit)
9399
return this;
94100
}
95101

102+
public SortGeoDistanceDescriptor<T> DistanceType(GeoDistance distanceType)
103+
{
104+
distanceType.ThrowIfNull("distanceType");
105+
Self.DistanceType = distanceType;
106+
return this;
107+
}
108+
96109
public SortGeoDistanceDescriptor<T> OnField(string field)
97110
{
98111
Self.Field = field;
@@ -129,7 +142,8 @@ object ICustomJson.GetCustomJson()
129142
{ GeoDistanceSort.Params[GeoDistanceSort.MissingIndex], Self.Missing },
130143
{ GeoDistanceSort.Params[GeoDistanceSort.ModeIndex], Self.Mode},
131144
{ GeoDistanceSort.Params[GeoDistanceSort.OrderIndex], Self.Order },
132-
{ GeoDistanceSort.Params[GeoDistanceSort.UnitIndex], Self.GeoUnit }
145+
{ GeoDistanceSort.Params[GeoDistanceSort.UnitIndex], Self.GeoUnit },
146+
{ GeoDistanceSort.Params[GeoDistanceSort.DistanceTypeIndex], Self.DistanceType }
133147
};
134148
}
135149
}

src/Tests/Nest.Tests.Unit/Search/Sorting/SortTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public void TestSortGeo()
211211
.PinTo(40, -70)
212212
.Unit(GeoUnit.Kilometers)
213213
.Mode(SortMode.Max)
214+
.DistanceType(GeoDistance.Arc)
214215
);
215216
var json = TestElasticClient.Serialize(s);
216217
var expected = @"
@@ -224,7 +225,8 @@ public void TestSortGeo()
224225
missing: ""_last"",
225226
mode: ""max"",
226227
order: ""desc"",
227-
unit: ""km""
228+
unit: ""km"",
229+
distance_type: ""arc""
228230
}
229231
}
230232
]

0 commit comments

Comments
 (0)