Skip to content

[Backport 8.4] Add support for geo distance sorting. #7360

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
Mar 15, 2023
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
12 changes: 11 additions & 1 deletion src/Elastic.Clients.Elasticsearch/Types/SortOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class SortOptions
internal sealed class SortOptionsConverter : JsonConverter<SortOptions>
{
// We manually define this converter since we simplify SortCombinations union from the spec as SortOptions instance.
// This requires a custom read method to handle deserialisation of the potential union JSON as specified.
// This requires a custom read method to handle deserialization of the potential union JSON as specified.

public override SortOptions Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Expand Down Expand Up @@ -53,6 +53,13 @@ public override SortOptions Read(ref Utf8JsonReader reader, Type typeToConvert,
return new SortOptions(propertyName, variant);
}

if (propertyName == "_geo_distance")
{
var variant = JsonSerializer.Deserialize<GeoDistanceSort?>(ref reader, options);
reader.Read();
return new SortOptions(propertyName, variant);
}

// For field sorts, the property name will be the field name
{
var variant = JsonSerializer.Deserialize<FieldSort>(ref reader, options);
Expand Down Expand Up @@ -104,6 +111,9 @@ public override void Write(Utf8JsonWriter writer, SortOptions value, JsonSeriali
case "_script":
JsonSerializer.Serialize<ScriptSort>(writer, (ScriptSort)value.Variant, options);
break;
case "_geo_distance":
JsonSerializer.Serialize<GeoDistanceSort>(writer, (GeoDistanceSort)value.Variant, options);
break;
default:
JsonSerializer.Serialize<FieldSort>(writer, (FieldSort)value.Variant, options);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,48 @@ public override void Write(Utf8JsonWriter writer, FieldSortNumericType value, Js
}
}

[JsonConverter(typeof(GeoDistanceTypeConverter))]
public enum GeoDistanceType
{
[EnumMember(Value = "plane")]
Plane,
[EnumMember(Value = "arc")]
Arc
}

internal sealed class GeoDistanceTypeConverter : JsonConverter<GeoDistanceType>
{
public override GeoDistanceType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var enumString = reader.GetString();
switch (enumString)
{
case "plane":
return GeoDistanceType.Plane;
case "arc":
return GeoDistanceType.Arc;
}

ThrowHelper.ThrowJsonException();
return default;
}

public override void Write(Utf8JsonWriter writer, GeoDistanceType value, JsonSerializerOptions options)
{
switch (value)
{
case GeoDistanceType.Plane:
writer.WriteStringValue("plane");
return;
case GeoDistanceType.Arc:
writer.WriteStringValue("arc");
return;
}

writer.WriteNullValue();
}
}

[JsonConverter(typeof(HealthStatusConverter))]
public enum HealthStatus
{
Expand Down
Loading