Skip to content

Commit dd0aa42

Browse files
Add support for geo distance sorting. (#7356) (#7357)
* Support geo distance sorting * Add basic bool query test Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent 3b29156 commit dd0aa42

12 files changed

+643
-19
lines changed

src/Elastic.Clients.Elasticsearch/Types/SortOptions.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public partial class SortOptions
1818
internal sealed class SortOptionsConverter : JsonConverter<SortOptions>
1919
{
2020
// We manually define this converter since we simplify SortCombinations union from the spec as SortOptions instance.
21-
// This requires a custom read method to handle deserialisation of the potential union JSON as specified.
21+
// This requires a custom read method to handle deserialization of the potential union JSON as specified.
2222

2323
public override SortOptions Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2424
{
@@ -53,6 +53,13 @@ public override SortOptions Read(ref Utf8JsonReader reader, Type typeToConvert,
5353
return new SortOptions(propertyName, variant);
5454
}
5555

56+
if (propertyName == "_geo_distance")
57+
{
58+
var variant = JsonSerializer.Deserialize<GeoDistanceSort?>(ref reader, options);
59+
reader.Read();
60+
return new SortOptions(propertyName, variant);
61+
}
62+
5663
// For field sorts, the property name will be the field name
5764
{
5865
var variant = JsonSerializer.Deserialize<FieldSort>(ref reader, options);
@@ -104,6 +111,9 @@ public override void Write(Utf8JsonWriter writer, SortOptions value, JsonSeriali
104111
case "_script":
105112
JsonSerializer.Serialize<ScriptSort>(writer, (ScriptSort)value.Variant, options);
106113
break;
114+
case "_geo_distance":
115+
JsonSerializer.Serialize<GeoDistanceSort>(writer, (GeoDistanceSort)value.Variant, options);
116+
break;
107117
default:
108118
JsonSerializer.Serialize<FieldSort>(writer, (FieldSort)value.Variant, options);
109119
break;

src/Elastic.Clients.Elasticsearch/_Generated/Types/Enums/Enums.NoNamespace.g.cs

+42
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,48 @@ public override void Write(Utf8JsonWriter writer, FieldSortNumericType value, Js
451451
}
452452
}
453453

454+
[JsonConverter(typeof(GeoDistanceTypeConverter))]
455+
public enum GeoDistanceType
456+
{
457+
[EnumMember(Value = "plane")]
458+
Plane,
459+
[EnumMember(Value = "arc")]
460+
Arc
461+
}
462+
463+
internal sealed class GeoDistanceTypeConverter : JsonConverter<GeoDistanceType>
464+
{
465+
public override GeoDistanceType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
466+
{
467+
var enumString = reader.GetString();
468+
switch (enumString)
469+
{
470+
case "plane":
471+
return GeoDistanceType.Plane;
472+
case "arc":
473+
return GeoDistanceType.Arc;
474+
}
475+
476+
ThrowHelper.ThrowJsonException();
477+
return default;
478+
}
479+
480+
public override void Write(Utf8JsonWriter writer, GeoDistanceType value, JsonSerializerOptions options)
481+
{
482+
switch (value)
483+
{
484+
case GeoDistanceType.Plane:
485+
writer.WriteStringValue("plane");
486+
return;
487+
case GeoDistanceType.Arc:
488+
writer.WriteStringValue("arc");
489+
return;
490+
}
491+
492+
writer.WriteNullValue();
493+
}
494+
}
495+
454496
[JsonConverter(typeof(HealthStatusConverter))]
455497
public enum HealthStatus
456498
{

0 commit comments

Comments
 (0)