diff --git a/src/Elastic.Clients.Elasticsearch/Serialization/SingleOrManySerializationHelper.cs b/src/Elastic.Clients.Elasticsearch/Serialization/SingleOrManySerializationHelper.cs index c88b001f205..ea3c40f20d3 100644 --- a/src/Elastic.Clients.Elasticsearch/Serialization/SingleOrManySerializationHelper.cs +++ b/src/Elastic.Clients.Elasticsearch/Serialization/SingleOrManySerializationHelper.cs @@ -37,8 +37,7 @@ public static IEnumerable Deserialize(ref Utf8JsonReader reader, J // } if (reader.TokenType == JsonTokenType.String) { - var value = reader.GetString(); - var item = (TItem)Activator.CreateInstance(typeof(TItem), value); + var item = (TItem)JsonSerializer.Deserialize(ref reader, typeof(TItem), options); return new TItem[] { item }; } diff --git a/src/Elastic.Clients.Elasticsearch/Types/FieldSort.cs b/src/Elastic.Clients.Elasticsearch/Types/FieldSort.cs new file mode 100644 index 00000000000..ea4cd931ea7 --- /dev/null +++ b/src/Elastic.Clients.Elasticsearch/Types/FieldSort.cs @@ -0,0 +1,146 @@ +// Licensed to Elasticsearch B.V under one or more agreements. +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information. + +using System; +using System.Text.Json; +using System.Text.Json.Serialization; +using Elastic.Clients.Elasticsearch.Mapping; + +namespace Elastic.Clients.Elasticsearch; + +[JsonConverter(typeof(FieldSortConverter))] +public partial class FieldSort +{ + public static FieldSort Empty { get; } = new(); +} + +internal sealed class FieldSortConverter : JsonConverter +{ + // This is temporarily a manual converter until we code-gen for shortcut properties. + // This serves as the template for those converters. + + public override FieldSort? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.StartObject) + { + string? format = null; + FieldValue? missing = null; + SortMode? mode = null; + NestedSortValue? nested = null; + FieldSortNumericType? numericType = null; + SortOrder? order = null; + FieldType? unmappedType = null; + + while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) + { + if (reader.TokenType == JsonTokenType.PropertyName) + { + var propertyName = reader.GetString(); + reader.Read(); + + switch (propertyName) + { + case "format": + format = reader.GetString(); + break; + case "missing": + missing = JsonSerializer.Deserialize(ref reader, options); + break; + case "mode": + mode = JsonSerializer.Deserialize(ref reader, options); + break; + case "nested": + nested = JsonSerializer.Deserialize(ref reader, options); + break; + case "numeric_type": + numericType = JsonSerializer.Deserialize(ref reader, options); + break; + case "order": + order = JsonSerializer.Deserialize(ref reader, options); + break; + case "unmapped_type": + unmappedType = JsonSerializer.Deserialize(ref reader, options); + break; + default: + throw new JsonException("Unexpected property while reading `Field`."); + } + } + } + + return new FieldSort + { + Format = format, + Missing = missing, + Mode = mode, + Nested = nested, + NumericType = numericType, + Order = order, + UnmappedType = unmappedType + }; + } + + else if (reader.TokenType == JsonTokenType.String) // Shortcut property + { + var sortOrder = JsonSerializer.Deserialize(ref reader, options); + return new FieldSort { Order = sortOrder }; + } + + throw new JsonException($"Unexpected JSON token '{reader.TokenType}' encountered while deserializing FieldSort."); + } + + public override void Write(Utf8JsonWriter writer, FieldSort value, JsonSerializerOptions options) + { + if (value is null) + { + writer.WriteNullValue(); + return; + } + + writer.WriteStartObject(); + + if (value.Format is not null) + { + writer.WritePropertyName("format"); + writer.WriteStringValue(value.Format); + } + + if (value.Missing.HasValue) + { + writer.WritePropertyName("missing"); + JsonSerializer.Serialize(writer, value.Missing.Value, options); + } + + if (value.Mode.HasValue) + { + writer.WritePropertyName("mode"); + JsonSerializer.Serialize(writer, value.Mode.Value, options); + } + + if (value.Nested is not null) + { + writer.WritePropertyName("nested"); + JsonSerializer.Serialize(writer, value.Nested, options); + } + + if (value.NumericType.HasValue) + { + writer.WritePropertyName("numeric_type"); + JsonSerializer.Serialize(writer, value.NumericType.Value, options); + } + + if (value.Order.HasValue) + { + writer.WritePropertyName("order"); + JsonSerializer.Serialize(writer, value.Order.Value, options); + } + + if (value.UnmappedType.HasValue) + { + writer.WritePropertyName("unmapped_type"); + JsonSerializer.Serialize(writer, value.UnmappedType.Value, options); + } + + writer.WriteEndObject(); + } +} diff --git a/src/Elastic.Clients.Elasticsearch/Types/SortCombinations.cs b/src/Elastic.Clients.Elasticsearch/Types/SortCombinations.cs deleted file mode 100644 index addf0638b05..00000000000 --- a/src/Elastic.Clients.Elasticsearch/Types/SortCombinations.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Licensed to Elasticsearch B.V under one or more agreements. -// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information. - -namespace Elastic.Clients.Elasticsearch; - -public partial class SortCombinations -{ - public SortCombinations(string field) : base(field) - { - } -} diff --git a/src/Elastic.Clients.Elasticsearch/Types/SortOptions.cs b/src/Elastic.Clients.Elasticsearch/Types/SortOptions.cs new file mode 100644 index 00000000000..57dfd5e8a7d --- /dev/null +++ b/src/Elastic.Clients.Elasticsearch/Types/SortOptions.cs @@ -0,0 +1,114 @@ +// Licensed to Elasticsearch B.V under one or more agreements. +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information. + +using System; +using System.Text.Json; +using System.Text.Json.Serialization; +using Elastic.Clients.Elasticsearch.Serialization; +using Elastic.Transport; + +namespace Elastic.Clients.Elasticsearch; + +public partial class SortOptions +{ + public static SortOptions Field(Field field) => new(field, FieldSort.Empty); +} + +internal sealed class SortOptionsConverter : JsonConverter +{ + // 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. + + public override SortOptions Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.StartObject) + { + reader.Read(); + if (reader.TokenType != JsonTokenType.PropertyName) + { + throw new JsonException("Expected a property name token representing the variant held within this container."); + } + + var propertyName = reader.GetString(); + reader.Read(); + if (propertyName == "_doc") + { + var variant = JsonSerializer.Deserialize(ref reader, options); + reader.Read(); + return new SortOptions(propertyName, variant); + } + + if (propertyName == "_score") + { + var variant = JsonSerializer.Deserialize(ref reader, options); + reader.Read(); + return new SortOptions(propertyName, variant); + } + + if (propertyName == "_script") + { + var variant = JsonSerializer.Deserialize(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(ref reader, options); + reader.Read(); + return new SortOptions(propertyName, variant); + } + } + + else if (reader.TokenType == JsonTokenType.String) + { + var fieldName = reader.GetString(); + return SortOptions.Field(fieldName, FieldSort.Empty); + } + + throw new JsonException($"Unexpected JSON token '{reader.TokenType}' encountered while deserializing SortOptions."); + } + + public override void Write(Utf8JsonWriter writer, SortOptions value, JsonSerializerOptions options) + { + if (!options.TryGetClientSettings(out var settings)) + throw new JsonException("Unable to retrieve IElasticsearchClientSettings."); + + string? fieldName = null; + + if (value.AdditionalPropertyName is IUrlParameter urlParameter) + { + fieldName = urlParameter.GetString(settings); + } + + // Special handling for shortcut on sorting with a basic field sort + if (value.Variant.Equals(FieldSort.Empty)) + { + writer.WriteStringValue(fieldName ?? value.VariantName); + return; + } + + writer.WriteStartObject(); + + writer.WritePropertyName(fieldName ?? value.VariantName); + + switch (value.VariantName) + { + case "_doc": + JsonSerializer.Serialize(writer, (ScoreSort)value.Variant, options); + break; + case "_score": + JsonSerializer.Serialize(writer, (ScoreSort)value.Variant, options); + break; + case "_script": + JsonSerializer.Serialize(writer, (ScriptSort)value.Variant, options); + break; + default: + JsonSerializer.Serialize(writer, (FieldSort)value.Variant, options); + break; + } + + writer.WriteEndObject(); + } +} diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Api/AsyncSearch/AsyncSearchSubmitRequest.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Api/AsyncSearch/AsyncSearchSubmitRequest.g.cs index d1fa397cc8f..0fbe0ed81fc 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Api/AsyncSearch/AsyncSearchSubmitRequest.g.cs +++ b/src/Elastic.Clients.Elasticsearch/_Generated/Api/AsyncSearch/AsyncSearchSubmitRequest.g.cs @@ -254,7 +254,7 @@ public override AsyncSearchSubmitRequest Read(ref Utf8JsonReader reader, Type ty if (property == "sort") { - variant.Sort = JsonSerializer.Deserialize?>(ref reader, options); + variant.Sort = JsonSerializer.Deserialize?>(ref reader, options); continue; } @@ -723,7 +723,7 @@ public AsyncSearchSubmitRequest(Elastic.Clients.Elasticsearch.Indices? indices) [JsonInclude] [JsonPropertyName("sort")] [JsonConverter(typeof(SortConverter))] - public IEnumerable? Sort { get; set; } + public IEnumerable? Sort { get; set; } [JsonInclude] [JsonPropertyName("_source")] @@ -925,7 +925,7 @@ public AsyncSearchSubmitRequestDescriptor Indices(Elastic.Clients.Ela private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private IEnumerable? StatsValue { get; set; } @@ -1319,7 +1319,7 @@ public AsyncSearchSubmitRequestDescriptor Size(int? size) return Self; } - public AsyncSearchSubmitRequestDescriptor Sort(IEnumerable? sort) + public AsyncSearchSubmitRequestDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -1690,7 +1690,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StatsValue is not null) @@ -1899,7 +1899,7 @@ public AsyncSearchSubmitRequestDescriptor Indices(Elastic.Clients.Elasticsearch. private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private IEnumerable? StatsValue { get; set; } @@ -2293,7 +2293,7 @@ public AsyncSearchSubmitRequestDescriptor Size(int? size) return Self; } - public AsyncSearchSubmitRequestDescriptor Sort(IEnumerable? sort) + public AsyncSearchSubmitRequestDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -2664,7 +2664,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StatsValue is not null) diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Api/SearchRequest.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Api/SearchRequest.g.cs index a9beb6b7228..9aca98f1f5f 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Api/SearchRequest.g.cs +++ b/src/Elastic.Clients.Elasticsearch/_Generated/Api/SearchRequest.g.cs @@ -245,7 +245,7 @@ public override SearchRequest Read(ref Utf8JsonReader reader, Type typeToConvert if (property == "sort") { - variant.Sort = JsonSerializer.Deserialize?>(ref reader, options); + variant.Sort = JsonSerializer.Deserialize?>(ref reader, options); continue; } @@ -705,7 +705,7 @@ public SearchRequest(Elastic.Clients.Elasticsearch.Indices? indices) : base(r => [JsonInclude] [JsonPropertyName("sort")] [JsonConverter(typeof(SortConverter))] - public IEnumerable? Sort { get; set; } + public IEnumerable? Sort { get; set; } [JsonInclude] [JsonPropertyName("_source")] @@ -908,7 +908,7 @@ public SearchRequestDescriptor Indices(Elastic.Clients.Elasticsearch. private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private IEnumerable? StatsValue { get; set; } @@ -1302,7 +1302,7 @@ public SearchRequestDescriptor Size(int? size) return Self; } - public SearchRequestDescriptor Sort(IEnumerable? sort) + public SearchRequestDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -1673,7 +1673,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StatsValue is not null) @@ -1883,7 +1883,7 @@ public SearchRequestDescriptor Indices(Elastic.Clients.Elasticsearch.Indices? in private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private IEnumerable? StatsValue { get; set; } @@ -2277,7 +2277,7 @@ public SearchRequestDescriptor Size(int? size) return Self; } - public SearchRequestDescriptor Sort(IEnumerable? sort) + public SearchRequestDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -2648,7 +2648,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StatsValue is not null) diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TopHitsAggregation.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TopHitsAggregation.g.cs index cc6f128c5f6..f9b199244d8 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TopHitsAggregation.g.cs +++ b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TopHitsAggregation.g.cs @@ -175,7 +175,7 @@ public override TopHitsAggregation Read(ref Utf8JsonReader reader, Type typeToCo if (reader.ValueTextEquals("sort")) { reader.Read(); - var value = SingleOrManySerializationHelper.Deserialize(ref reader, options); + var value = SingleOrManySerializationHelper.Deserialize(ref reader, options); if (value is not null) { agg.Sort = value; @@ -316,7 +316,7 @@ public override void Write(Utf8JsonWriter writer, TopHitsAggregation value, Json if (value.Sort is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(value.Sort, writer, options); + SingleOrManySerializationHelper.Serialize(value.Sort, writer, options); } if (value.StoredFields is not null) @@ -384,7 +384,7 @@ internal TopHitsAggregation() public int? Size { get; set; } [JsonConverter(typeof(SortConverter))] - public IEnumerable? Sort { get; set; } + public IEnumerable? Sort { get; set; } public Elastic.Clients.Elasticsearch.Fields? StoredFields { get; set; } @@ -428,7 +428,7 @@ public TopHitsAggregationDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private Elastic.Clients.Elasticsearch.Fields? StoredFieldsValue { get; set; } @@ -532,7 +532,7 @@ public TopHitsAggregationDescriptor Size(int? size) return Self; } - public TopHitsAggregationDescriptor Sort(IEnumerable? sort) + public TopHitsAggregationDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -640,7 +640,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StoredFieldsValue is not null) @@ -707,7 +707,7 @@ public TopHitsAggregationDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private Elastic.Clients.Elasticsearch.Fields? StoredFieldsValue { get; set; } @@ -817,7 +817,7 @@ public TopHitsAggregationDescriptor Size(int? size) return Self; } - public TopHitsAggregationDescriptor Sort(IEnumerable? sort) + public TopHitsAggregationDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -925,7 +925,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StoredFieldsValue is not null) diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TopMetricsAggregation.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TopMetricsAggregation.g.cs index e1e81a78ebf..def9e3c8884 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TopMetricsAggregation.g.cs +++ b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TopMetricsAggregation.g.cs @@ -103,7 +103,7 @@ public override TopMetricsAggregation Read(ref Utf8JsonReader reader, Type typeT if (reader.ValueTextEquals("sort")) { reader.Read(); - var value = SingleOrManySerializationHelper.Deserialize(ref reader, options); + var value = SingleOrManySerializationHelper.Deserialize(ref reader, options); if (value is not null) { agg.Sort = value; @@ -172,7 +172,7 @@ public override void Write(Utf8JsonWriter writer, TopMetricsAggregation value, J if (value.Sort is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(value.Sort, writer, options); + SingleOrManySerializationHelper.Serialize(value.Sort, writer, options); } writer.WriteEndObject(); @@ -210,7 +210,7 @@ internal TopMetricsAggregation() public int? Size { get; set; } [JsonConverter(typeof(SortConverter))] - public IEnumerable? Sort { get; set; } + public IEnumerable? Sort { get; set; } } public sealed partial class TopMetricsAggregationDescriptor : SerializableDescriptor> @@ -238,7 +238,7 @@ public TopMetricsAggregationDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } public TopMetricsAggregationDescriptor Metrics(IEnumerable? metrics) { @@ -312,7 +312,7 @@ public TopMetricsAggregationDescriptor Size(int? size) return Self; } - public TopMetricsAggregationDescriptor Sort(IEnumerable? sort) + public TopMetricsAggregationDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -381,7 +381,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } writer.WriteEndObject(); @@ -420,7 +420,7 @@ public TopMetricsAggregationDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } public TopMetricsAggregationDescriptor Metrics(IEnumerable? metrics) { @@ -500,7 +500,7 @@ public TopMetricsAggregationDescriptor Size(int? size) return Self; } - public TopMetricsAggregationDescriptor Sort(IEnumerable? sort) + public TopMetricsAggregationDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -569,7 +569,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } writer.WriteEndObject(); diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/MSearch/MultisearchBody.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/MSearch/MultisearchBody.g.cs index 822d1629bb6..799ac8d0b16 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/MSearch/MultisearchBody.g.cs +++ b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/MSearch/MultisearchBody.g.cs @@ -171,7 +171,7 @@ public override MultisearchBody Read(ref Utf8JsonReader reader, Type typeToConve if (property == "sort") { - variant.Sort = JsonSerializer.Deserialize?>(ref reader, options); + variant.Sort = JsonSerializer.Deserialize?>(ref reader, options); continue; } @@ -469,7 +469,7 @@ public sealed partial class MultisearchBody public int? Size { get; set; } [JsonConverter(typeof(SortConverter))] - public IEnumerable? Sort { get; set; } + public IEnumerable? Sort { get; set; } public IEnumerable? Stats { get; set; } @@ -581,7 +581,7 @@ public MultisearchBodyDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private IEnumerable? StatsValue { get; set; } @@ -933,7 +933,7 @@ public MultisearchBodyDescriptor Size(int? size) return Self; } - public MultisearchBodyDescriptor Sort(IEnumerable? sort) + public MultisearchBodyDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -1278,7 +1278,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StatsValue is not null) @@ -1436,7 +1436,7 @@ public MultisearchBodyDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private IEnumerable? StatsValue { get; set; } @@ -1788,7 +1788,7 @@ public MultisearchBodyDescriptor Size(int? size) return Self; } - public MultisearchBodyDescriptor Sort(IEnumerable? sort) + public MultisearchBodyDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -2133,7 +2133,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StatsValue is not null) diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/Reindex/Source.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/Reindex/Source.g.cs index c7eec539d17..5c63b7b9853 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/Reindex/Source.g.cs +++ b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/Reindex/Source.g.cs @@ -58,7 +58,7 @@ public sealed partial class Source [JsonInclude] [JsonPropertyName("sort")] [JsonConverter(typeof(SortConverter))] - public IEnumerable? Sort { get; set; } + public IEnumerable? Sort { get; set; } } public sealed partial class SourceDescriptor : SerializableDescriptor> @@ -94,7 +94,7 @@ public SourceDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } public SourceDescriptor Query(Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? query) { @@ -192,7 +192,7 @@ public SourceDescriptor Size(int? size) return Self; } - public SourceDescriptor Sort(IEnumerable? sort) + public SourceDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -272,7 +272,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } writer.WriteEndObject(); @@ -312,7 +312,7 @@ public SourceDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } public SourceDescriptor Query(Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? query) { @@ -410,7 +410,7 @@ public SourceDescriptor Size(int? size) return Self; } - public SourceDescriptor Sort(IEnumerable? sort) + public SourceDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -490,7 +490,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } writer.WriteEndObject(); diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/Search/InnerHits.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/Search/InnerHits.g.cs index b297623f547..9c94c120743 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/Search/InnerHits.g.cs +++ b/src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/Search/InnerHits.g.cs @@ -78,7 +78,7 @@ public sealed partial class InnerHits [JsonInclude] [JsonPropertyName("sort")] [JsonConverter(typeof(SortConverter))] - public IEnumerable? Sort { get; set; } + public IEnumerable? Sort { get; set; } [JsonInclude] [JsonPropertyName("stored_field")] @@ -138,7 +138,7 @@ public InnerHitsDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private Elastic.Clients.Elasticsearch.Fields? StoredFieldValue { get; set; } @@ -284,7 +284,7 @@ public InnerHitsDescriptor Size(int? size) return Self; } - public InnerHitsDescriptor Sort(IEnumerable? sort) + public InnerHitsDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -431,7 +431,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StoredFieldValue is not null) @@ -501,7 +501,7 @@ public InnerHitsDescriptor() : base() private int? SizeValue { get; set; } - private IEnumerable? SortValue { get; set; } + private IEnumerable? SortValue { get; set; } private Elastic.Clients.Elasticsearch.Fields? StoredFieldValue { get; set; } @@ -647,7 +647,7 @@ public InnerHitsDescriptor Size(int? size) return Self; } - public InnerHitsDescriptor Sort(IEnumerable? sort) + public InnerHitsDescriptor Sort(IEnumerable? sort) { SortValue = sort; return Self; @@ -794,7 +794,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o if (SortValue is not null) { writer.WritePropertyName("sort"); - SingleOrManySerializationHelper.Serialize(SortValue, writer, options); + SingleOrManySerializationHelper.Serialize(SortValue, writer, options); } if (StoredFieldValue is not null) diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortCombinations.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortCombinations.g.cs deleted file mode 100644 index f66e586f9b8..00000000000 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortCombinations.g.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Licensed to Elasticsearch B.V under one or more agreements. -// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information. -// -// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗ -// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝ -// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗ -// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝ -// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗ -// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝ -// ------------------------------------------------ -// -// This file is automatically generated. -// Please do not edit these files manually. -// -// ------------------------------------------------ - -using Elastic.Clients.Elasticsearch.Fluent; -using Elastic.Clients.Elasticsearch.Serialization; -using Elastic.Transport; -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Text.Json; -using System.Text.Json.Serialization; - -#nullable restore -namespace Elastic.Clients.Elasticsearch; -public partial class SortCombinations : Union -{ - public SortCombinations(Elastic.Clients.Elasticsearch.Field field) : base(field) - { - } - - public SortCombinations(Elastic.Clients.Elasticsearch.SortOptions sortOptions) : base(sortOptions) - { - } -} \ No newline at end of file diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortConverter.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortConverter.g.cs index cdc7b56d2fd..1272f62e4a9 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortConverter.g.cs +++ b/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortConverter.g.cs @@ -26,6 +26,6 @@ #nullable restore namespace Elastic.Clients.Elasticsearch; -internal sealed class SortConverter : IEnumerableSingleOrManyConverter +internal sealed class SortConverter : IEnumerableSingleOrManyConverter { } \ No newline at end of file diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortOptions.g.cs b/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortOptions.g.cs index 9b29dbbf91e..045ce6653a4 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortOptions.g.cs +++ b/src/Elastic.Clients.Elasticsearch/_Generated/Types/SortOptions.g.cs @@ -63,87 +63,6 @@ internal SortOptions(Elastic.Clients.Elasticsearch.Field field, object variant) public static SortOptions Field(Elastic.Clients.Elasticsearch.Field field, Elastic.Clients.Elasticsearch.FieldSort fieldSort) => new SortOptions(field, fieldSort); } -internal sealed class SortOptionsConverter : JsonConverter -{ - public override SortOptions Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType != JsonTokenType.StartObject) - { - throw new JsonException("Expected start token."); - } - - reader.Read(); - if (reader.TokenType != JsonTokenType.PropertyName) - { - throw new JsonException("Expected a property name token representing the variant held within this container."); - } - - var propertyName = reader.GetString(); - reader.Read(); - if (propertyName == "_doc") - { - var variant = JsonSerializer.Deserialize(ref reader, options); - reader.Read(); - return new SortOptions(propertyName, variant); - } - - if (propertyName == "_score") - { - var variant = JsonSerializer.Deserialize(ref reader, options); - reader.Read(); - return new SortOptions(propertyName, variant); - } - - if (propertyName == "_script") - { - var variant = JsonSerializer.Deserialize(ref reader, options); - reader.Read(); - return new SortOptions(propertyName, variant); - } - - { - var variant = JsonSerializer.Deserialize(ref reader, options); - reader.Read(); - return new SortOptions(propertyName, variant); - } - - throw new JsonException(); - } - - public override void Write(Utf8JsonWriter writer, SortOptions value, JsonSerializerOptions options) - { - writer.WriteStartObject(); - if (value.AdditionalPropertyName is IUrlParameter urlParameter) - { - var extraData = options.GetConverter(typeof(ExtraSerializationData)) as ExtraSerializationData; - var propertyName = urlParameter.GetString(extraData.Settings); - writer.WritePropertyName(propertyName); - } - else - { - writer.WritePropertyName(value.VariantName); - } - - switch (value.VariantName) - { - case "_doc": - JsonSerializer.Serialize(writer, (Elastic.Clients.Elasticsearch.ScoreSort)value.Variant, options); - break; - case "_score": - JsonSerializer.Serialize(writer, (Elastic.Clients.Elasticsearch.ScoreSort)value.Variant, options); - break; - case "_script": - JsonSerializer.Serialize(writer, (Elastic.Clients.Elasticsearch.ScriptSort)value.Variant, options); - break; - default: - JsonSerializer.Serialize(writer, (Elastic.Clients.Elasticsearch.FieldSort)value.Variant, options); - break; - } - - writer.WriteEndObject(); - } -} - public sealed partial class SortOptionsDescriptor : SerializableDescriptor> { internal SortOptionsDescriptor(Action> configure) => configure.Invoke(this); diff --git a/tests/Tests/Search/Scroll/Scroll/SlicedScrollSearchApiTests.cs b/tests/Tests/Search/Scroll/Scroll/SlicedScrollSearchApiTests.cs index 374a8768eaf..9fa04955b63 100644 --- a/tests/Tests/Search/Scroll/Scroll/SlicedScrollSearchApiTests.cs +++ b/tests/Tests/Search/Scroll/Scroll/SlicedScrollSearchApiTests.cs @@ -54,7 +54,7 @@ protected override void OnBeforeCall(ElasticsearchClient client) .Slice(ss => ss.Max(maxSlices).Id(currentSlice)) .Sort(new[] { - new SortCombinations(SortOptions.Field("_doc", new FieldSort { Order = SortOrder.Asc })) + SortOptions.Field("_doc", new FieldSort { Order = SortOrder.Asc }) }) ); if (!response.IsValid) diff --git a/tests/Tests/Search/Search/BasicSortUsageTests.cs b/tests/Tests/Search/Search/BasicSortUsageTests.cs index 552b87c2f18..51b3bde9eec 100644 --- a/tests/Tests/Search/Search/BasicSortUsageTests.cs +++ b/tests/Tests/Search/Search/BasicSortUsageTests.cs @@ -20,11 +20,11 @@ public BasicSortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base( }; protected override Action> Fluent => s => s - .Sort(new[] { new SortCombinations("startedOn") }); + .Sort(new[] { SortOptions.Field("startedOn") }); protected override SearchRequest Initializer => new() { - Sort = new [] { new SortCombinations("startedOn") } + Sort = new [] { SortOptions.Field("startedOn") } }; } diff --git a/tests/Tests/Search/Search/SortUsageTests.cs b/tests/Tests/Search/Search/SortUsageTests.cs index 194c852ee59..d7f5741566d 100644 --- a/tests/Tests/Search/Search/SortUsageTests.cs +++ b/tests/Tests/Search/Search/SortUsageTests.cs @@ -27,8 +27,8 @@ public SortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(clust protected override Action> Fluent => s => s .Sort(new[] { - new SortCombinations(SortOptions.Field(Infer.Field(f => f.StartedOn), new FieldSort { Order = SortOrder.Asc })), - new SortCombinations(SortOptions.Field(Infer.Field(f => f.Name), new FieldSort { Order = SortOrder.Desc })) + SortOptions.Field(Infer.Field(f => f.StartedOn), new FieldSort { Order = SortOrder.Asc }), + SortOptions.Field(Infer.Field(f => f.Name), new FieldSort { Order = SortOrder.Desc }) }); protected override SearchRequest Initializer => @@ -36,8 +36,8 @@ public SortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(clust { Sort = new[] { - new SortCombinations(SortOptions.Field(Infer.Field(f => f.StartedOn), new FieldSort { Order = SortOrder.Asc })), - new SortCombinations(SortOptions.Field(Infer.Field(f => f.Name), new FieldSort { Order = SortOrder.Desc })) + SortOptions.Field(Infer.Field(f => f.StartedOn), new FieldSort { Order = SortOrder.Asc }), + SortOptions.Field(Infer.Field(f => f.Name), new FieldSort { Order = SortOrder.Desc }) } }; } diff --git a/tests/Tests/Serialization/SortCombinationsSerializationTests.cs b/tests/Tests/Serialization/SortCombinationsSerializationTests.cs deleted file mode 100644 index 8d0918a626a..00000000000 --- a/tests/Tests/Serialization/SortCombinationsSerializationTests.cs +++ /dev/null @@ -1,57 +0,0 @@ -// Licensed to Elasticsearch B.V under one or more agreements. -// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information. - -using System.Threading.Tasks; -using Tests.Core.Xunit; -using Tests.Domain; -using VerifyXunit; - -namespace Tests.Serialization; - -[UsesVerify] -[SystemTextJsonOnly] -public class SortCombinationsSerializationTests : SerializerTestBase -{ - [U] - public async Task SerializesFieldSort_WithStringField() - { - var dictionary = new SortCombinations(SortOptions.Field("string-field", new FieldSort { Order = SortOrder.Asc })); - var json = await SerializeAndGetJsonStringAsync(dictionary); - await Verifier.VerifyJson(json); - } - - [U] - public async Task SerializesFieldSort_WithInferredField() - { - var dictionary = new SortCombinations(SortOptions.Field(Infer.Field(p => p.Name), new FieldSort { Order = SortOrder.Asc })); - var json = await SerializeAndGetJsonStringAsync(dictionary); - await Verifier.VerifyJson(json); - } - - [U] - public void DeserializesFieldSort_WithStringField() - { - var json = @"{""name"":{""order"":""asc""}}"; - var result = DeserializeJsonString(json); - - var sortOptions = result.Item2; - - sortOptions.Should().NotBeNull(); - var fieldSort = sortOptions.Variant.Should().BeOfType().Subject; - - fieldSort.Order.Should().Be(SortOrder.Asc); - } - - [U] - public void DeserializesFieldString() - { - var json = @"""a-field"""; - var result = DeserializeJsonString(json); - - var field = result.Item1; - - field.Should().NotBeNull(); - field.Name.Should().Be("a-field"); - } -} diff --git a/tests/Tests/Serialization/Types/SortOptionsSerializationTests.cs b/tests/Tests/Serialization/Types/SortOptionsSerializationTests.cs new file mode 100644 index 00000000000..2a377dd0002 --- /dev/null +++ b/tests/Tests/Serialization/Types/SortOptionsSerializationTests.cs @@ -0,0 +1,80 @@ +// Licensed to Elasticsearch B.V under one or more agreements. +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using VerifyXunit; + +namespace Tests.Serialization.Types; + +[UsesVerify] +public class SortOptionsSerializationTests : SerializerTestBase +{ + [U] + public void DeserializesSortCombinations_WithFieldSorts_AsSortOptions() + { + var json = @"{""sort"":[{""post_date"":{""order"":""asc"",""format"":""strict_date_optional_time_nanos""}},""user"",{""name"":""desc""},{""age"":""desc""},""_score""]}"; + + var testClass = DeserializeJsonString(json); + + var sort = testClass.Sort[0]; + sort.VariantName.Should().Be("post_date"); + var fieldSortVariant = sort.Variant.Should().BeOfType().Subject; + fieldSortVariant.Format.Should().Be("strict_date_optional_time_nanos"); + fieldSortVariant.Missing.Should().BeNull(); + fieldSortVariant.Mode.Should().BeNull(); + fieldSortVariant.Nested.Should().BeNull(); + fieldSortVariant.NumericType.Should().BeNull(); + fieldSortVariant.Order.Should().Be(SortOrder.Asc); + fieldSortVariant.UnmappedType.Should().BeNull(); + + sort = testClass.Sort[1]; + sort.AdditionalPropertyName.Should().Be("user"); + fieldSortVariant = sort.Variant.Should().BeOfType().Subject; + fieldSortVariant.Format.Should().BeNull(); + fieldSortVariant.Missing.Should().BeNull(); + fieldSortVariant.Mode.Should().BeNull(); + fieldSortVariant.Nested.Should().BeNull(); + fieldSortVariant.NumericType.Should().BeNull(); + fieldSortVariant.Order.Should().BeNull(); + fieldSortVariant.UnmappedType.Should().BeNull(); + + sort = testClass.Sort[2]; + sort.VariantName.Should().Be("name"); + fieldSortVariant = sort.Variant.Should().BeOfType().Subject; + fieldSortVariant.Format.Should().BeNull(); + fieldSortVariant.Missing.Should().BeNull(); + fieldSortVariant.Mode.Should().BeNull(); + fieldSortVariant.Nested.Should().BeNull(); + fieldSortVariant.NumericType.Should().BeNull(); + fieldSortVariant.Order.Should().Be(SortOrder.Desc); + fieldSortVariant.UnmappedType.Should().BeNull(); + + sort = testClass.Sort[3]; + sort.VariantName.Should().Be("age"); + fieldSortVariant = sort.Variant.Should().BeOfType().Subject; + fieldSortVariant.Format.Should().BeNull(); + fieldSortVariant.Missing.Should().BeNull(); + fieldSortVariant.Mode.Should().BeNull(); + fieldSortVariant.Nested.Should().BeNull(); + fieldSortVariant.NumericType.Should().BeNull(); + fieldSortVariant.Order.Should().Be(SortOrder.Desc); + fieldSortVariant.UnmappedType.Should().BeNull(); + + sort = testClass.Sort[4]; + sort.AdditionalPropertyName.Should().Be("_score"); + fieldSortVariant = sort.Variant.Should().BeOfType().Subject; + fieldSortVariant.Format.Should().BeNull(); + fieldSortVariant.Missing.Should().BeNull(); + fieldSortVariant.Mode.Should().BeNull(); + fieldSortVariant.Nested.Should().BeNull(); + fieldSortVariant.NumericType.Should().BeNull(); + fieldSortVariant.Order.Should().BeNull(); + fieldSortVariant.UnmappedType.Should().BeNull(); + } + + private class TestClass + { + public IList Sort { get; set; } + } +}