|
24 | 24 | #nullable restore
|
25 | 25 | namespace Elastic.Clients.Elasticsearch
|
26 | 26 | {
|
| 27 | + internal sealed class GeoDistanceSortConverter : JsonConverter<GeoDistanceSort> |
| 28 | + { |
| 29 | + public override GeoDistanceSort Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 30 | + { |
| 31 | + if (reader.TokenType != JsonTokenType.StartObject) |
| 32 | + throw new JsonException("Unexpected JSON detected."); |
| 33 | + var variant = new GeoDistanceSort(); |
| 34 | + while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) |
| 35 | + { |
| 36 | + if (reader.TokenType == JsonTokenType.PropertyName) |
| 37 | + { |
| 38 | + var property = reader.GetString(); |
| 39 | + if (property == "distance_type") |
| 40 | + { |
| 41 | + variant.DistanceType = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.GeoDistanceType?>(ref reader, options); |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + if (property == "ignore_unmapped") |
| 46 | + { |
| 47 | + variant.IgnoreUnmapped = JsonSerializer.Deserialize<bool?>(ref reader, options); |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + if (property == "mode") |
| 52 | + { |
| 53 | + variant.Mode = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.SortMode?>(ref reader, options); |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + if (property == "order") |
| 58 | + { |
| 59 | + variant.Order = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.SortOrder?>(ref reader, options); |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + if (property == "unit") |
| 64 | + { |
| 65 | + variant.Unit = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.DistanceUnit?>(ref reader, options); |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + variant.Field = property; |
| 70 | + reader.Read(); |
| 71 | + variant.Location = JsonSerializer.Deserialize<IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation>>(ref reader, options); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + reader.Read(); |
| 76 | + return variant; |
| 77 | + } |
| 78 | + |
| 79 | + public override void Write(Utf8JsonWriter writer, GeoDistanceSort value, JsonSerializerOptions options) |
| 80 | + { |
| 81 | + writer.WriteStartObject(); |
| 82 | + if (value.Field is not null && value.Location is not null) |
| 83 | + { |
| 84 | + if (!options.TryGetClientSettings(out var settings)) |
| 85 | + { |
| 86 | + throw new JsonException("Unable to retrive client settings for JsonSerializerOptions."); |
| 87 | + } |
| 88 | + |
| 89 | + var propertyName = settings.Inferrer.Field(value.Field); |
| 90 | + writer.WritePropertyName(propertyName); |
| 91 | + JsonSerializer.Serialize(writer, value.Location, options); |
| 92 | + } |
| 93 | + |
| 94 | + if (value.DistanceType is not null) |
| 95 | + { |
| 96 | + writer.WritePropertyName("distance_type"); |
| 97 | + JsonSerializer.Serialize(writer, value.DistanceType, options); |
| 98 | + } |
| 99 | + |
| 100 | + if (value.IgnoreUnmapped.HasValue) |
| 101 | + { |
| 102 | + writer.WritePropertyName("ignore_unmapped"); |
| 103 | + writer.WriteBooleanValue(value.IgnoreUnmapped.Value); |
| 104 | + } |
| 105 | + |
| 106 | + if (value.Mode is not null) |
| 107 | + { |
| 108 | + writer.WritePropertyName("mode"); |
| 109 | + JsonSerializer.Serialize(writer, value.Mode, options); |
| 110 | + } |
| 111 | + |
| 112 | + if (value.Order is not null) |
| 113 | + { |
| 114 | + writer.WritePropertyName("order"); |
| 115 | + JsonSerializer.Serialize(writer, value.Order, options); |
| 116 | + } |
| 117 | + |
| 118 | + if (value.Unit is not null) |
| 119 | + { |
| 120 | + writer.WritePropertyName("unit"); |
| 121 | + JsonSerializer.Serialize(writer, value.Unit, options); |
| 122 | + } |
| 123 | + |
| 124 | + writer.WriteEndObject(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + [JsonConverter(typeof(GeoDistanceSortConverter))] |
27 | 129 | public partial class GeoDistanceSort : ISortOptionsVariant
|
28 | 130 | {
|
29 | 131 | [JsonInclude]
|
30 | 132 | [JsonPropertyName("distance_type")]
|
31 | 133 | public Elastic.Clients.Elasticsearch.GeoDistanceType? DistanceType { get; set; }
|
32 | 134 |
|
| 135 | + [JsonInclude] |
| 136 | + [JsonPropertyName("field")] |
| 137 | + public Elastic.Clients.Elasticsearch.Field Field { get; set; } |
| 138 | + |
33 | 139 | [JsonInclude]
|
34 | 140 | [JsonPropertyName("ignore_unmapped")]
|
35 | 141 | public bool? IgnoreUnmapped { get; set; }
|
36 | 142 |
|
| 143 | + [JsonInclude] |
| 144 | + [JsonPropertyName("location")] |
| 145 | + public IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> Location { get; set; } |
| 146 | + |
37 | 147 | [JsonInclude]
|
38 | 148 | [JsonPropertyName("mode")]
|
39 | 149 | public Elastic.Clients.Elasticsearch.SortMode? Mode { get; set; }
|
@@ -64,6 +174,10 @@ public GeoDistanceSortDescriptor() : base()
|
64 | 174 |
|
65 | 175 | private Elastic.Clients.Elasticsearch.DistanceUnit? UnitValue { get; set; }
|
66 | 176 |
|
| 177 | + private Elastic.Clients.Elasticsearch.Field FieldValue { get; set; } |
| 178 | + |
| 179 | + private IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> LocationValue { get; set; } |
| 180 | + |
67 | 181 | public GeoDistanceSortDescriptor<TDocument> DistanceType(Elastic.Clients.Elasticsearch.GeoDistanceType? distanceType)
|
68 | 182 | {
|
69 | 183 | DistanceTypeValue = distanceType;
|
@@ -94,9 +208,34 @@ public GeoDistanceSortDescriptor<TDocument> Unit(Elastic.Clients.Elasticsearch.D
|
94 | 208 | return Self;
|
95 | 209 | }
|
96 | 210 |
|
| 211 | + public GeoDistanceSortDescriptor<TDocument> Location(IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> location) |
| 212 | + { |
| 213 | + LocationValue = location; |
| 214 | + return Self; |
| 215 | + } |
| 216 | + |
| 217 | + public GeoDistanceSortDescriptor<TDocument> Field(Elastic.Clients.Elasticsearch.Field field) |
| 218 | + { |
| 219 | + FieldValue = field; |
| 220 | + return Self; |
| 221 | + } |
| 222 | + |
| 223 | + public GeoDistanceSortDescriptor<TDocument> Field<TValue>(Expression<Func<TDocument, TValue>> field) |
| 224 | + { |
| 225 | + FieldValue = field; |
| 226 | + return Self; |
| 227 | + } |
| 228 | + |
97 | 229 | protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
|
98 | 230 | {
|
99 | 231 | writer.WriteStartObject();
|
| 232 | + if (FieldValue is not null && LocationValue is not null) |
| 233 | + { |
| 234 | + var propertyName = settings.Inferrer.Field(FieldValue); |
| 235 | + writer.WritePropertyName(propertyName); |
| 236 | + JsonSerializer.Serialize(writer, LocationValue, options); |
| 237 | + } |
| 238 | + |
100 | 239 | if (DistanceTypeValue is not null)
|
101 | 240 | {
|
102 | 241 | writer.WritePropertyName("distance_type");
|
@@ -148,6 +287,10 @@ public GeoDistanceSortDescriptor() : base()
|
148 | 287 |
|
149 | 288 | private Elastic.Clients.Elasticsearch.DistanceUnit? UnitValue { get; set; }
|
150 | 289 |
|
| 290 | + private Elastic.Clients.Elasticsearch.Field FieldValue { get; set; } |
| 291 | + |
| 292 | + private IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> LocationValue { get; set; } |
| 293 | + |
151 | 294 | public GeoDistanceSortDescriptor DistanceType(Elastic.Clients.Elasticsearch.GeoDistanceType? distanceType)
|
152 | 295 | {
|
153 | 296 | DistanceTypeValue = distanceType;
|
@@ -178,9 +321,40 @@ public GeoDistanceSortDescriptor Unit(Elastic.Clients.Elasticsearch.DistanceUnit
|
178 | 321 | return Self;
|
179 | 322 | }
|
180 | 323 |
|
| 324 | + public GeoDistanceSortDescriptor Location(IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> location) |
| 325 | + { |
| 326 | + LocationValue = location; |
| 327 | + return Self; |
| 328 | + } |
| 329 | + |
| 330 | + public GeoDistanceSortDescriptor Field(Elastic.Clients.Elasticsearch.Field field) |
| 331 | + { |
| 332 | + FieldValue = field; |
| 333 | + return Self; |
| 334 | + } |
| 335 | + |
| 336 | + public GeoDistanceSortDescriptor Field<TDocument, TValue>(Expression<Func<TDocument, TValue>> field) |
| 337 | + { |
| 338 | + FieldValue = field; |
| 339 | + return Self; |
| 340 | + } |
| 341 | + |
| 342 | + public GeoDistanceSortDescriptor Field<TDocument>(Expression<Func<TDocument, object>> field) |
| 343 | + { |
| 344 | + FieldValue = field; |
| 345 | + return Self; |
| 346 | + } |
| 347 | + |
181 | 348 | protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
|
182 | 349 | {
|
183 | 350 | writer.WriteStartObject();
|
| 351 | + if (FieldValue is not null && LocationValue is not null) |
| 352 | + { |
| 353 | + var propertyName = settings.Inferrer.Field(FieldValue); |
| 354 | + writer.WritePropertyName(propertyName); |
| 355 | + JsonSerializer.Serialize(writer, LocationValue, options); |
| 356 | + } |
| 357 | + |
184 | 358 | if (DistanceTypeValue is not null)
|
185 | 359 | {
|
186 | 360 | writer.WritePropertyName("distance_type");
|
|
0 commit comments