From 1867e9b32fa7371e0fef76d90dc3a9d3c0729615 Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Thu, 29 Feb 2024 05:18:33 +1300 Subject: [PATCH] Allow passing `"auto"` as the `slices` parameter (#553) * Allow passing `"auto"` as the `slices` parameter Signed-off-by: Thomas Farr * Update CHANGELOG Signed-off-by: Thomas Farr * Fix license header Signed-off-by: Thomas Farr --------- Signed-off-by: Thomas Farr (cherry picked from commit cd4bd525f6842403aac64b4de1f77cf51bb90ac9) --- CHANGELOG.md | 1 + .../Infer/TrackTotalHits/TrackTotalHits.cs | 95 ------------------- .../TrackTotalHits/TrackTotalHitsFormatter.cs | 29 ------ .../Descriptors.NoNamespace.cs | 6 +- .../Document/Multiple/Slices/Slices.cs | 29 ++++++ .../Multiple/Slices/SlicesFormatter.cs | 31 ++++++ src/OpenSearch.Client/Requests.NoNamespace.cs | 12 +-- .../Search/TrackTotalHits/TrackTotalHits.cs | 31 ++++++ .../TrackTotalHits/TrackTotalHitsFormatter.cs | 30 ++++++ 9 files changed, 131 insertions(+), 133 deletions(-) delete mode 100644 src/OpenSearch.Client/CommonAbstractions/Infer/TrackTotalHits/TrackTotalHits.cs delete mode 100644 src/OpenSearch.Client/CommonAbstractions/Infer/TrackTotalHits/TrackTotalHitsFormatter.cs create mode 100644 src/OpenSearch.Client/Document/Multiple/Slices/Slices.cs create mode 100644 src/OpenSearch.Client/Document/Multiple/Slices/SlicesFormatter.cs create mode 100644 src/OpenSearch.Client/Search/TrackTotalHits/TrackTotalHits.cs create mode 100644 src/OpenSearch.Client/Search/TrackTotalHits/TrackTotalHitsFormatter.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 17e4ae4cba..5809a54a7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Fixed `HttpConnection.ConvertHttpMethod` to support `Patch` method ([#489](https://github.com/opensearch-project/opensearch-net/pull/489)) - Fixed `IEnumerable` property mapping. ([#503](https://github.com/opensearch-project/opensearch-net/pull/503)) - Fixed `ConnectionConfiguration.DefaultMemoryStreamFactory` actually used as default. ([#552](https://github.com/opensearch-project/opensearch-net/pull/552)) +- Fixed passing `"auto"` to the `slices` parameter ([#553](https://github.com/opensearch-project/opensearch-net/pull/553)) ### Dependencies - Bumps `NSwag.Core.Yaml` from 13.19.0 to 14.0.3 diff --git a/src/OpenSearch.Client/CommonAbstractions/Infer/TrackTotalHits/TrackTotalHits.cs b/src/OpenSearch.Client/CommonAbstractions/Infer/TrackTotalHits/TrackTotalHits.cs deleted file mode 100644 index 5c176f7cf4..0000000000 --- a/src/OpenSearch.Client/CommonAbstractions/Infer/TrackTotalHits/TrackTotalHits.cs +++ /dev/null @@ -1,95 +0,0 @@ -/* SPDX-License-Identifier: Apache-2.0 -* -* The OpenSearch Contributors require contributions made to -* this file be licensed under the Apache-2.0 license or a -* compatible open source license. -*/ - -using System; -using System.Diagnostics; -using System.Globalization; -using OpenSearch.Net; -using OpenSearch.Net.Utf8Json; - -namespace OpenSearch.Client -{ - [JsonFormatter(typeof(TrackTotalHitsFormatter))] - [DebuggerDisplay("{DebugDisplay,nq}")] - public class TrackTotalHits : IEquatable, IUrlParameter - { - public TrackTotalHits(bool trackTotalHits) - { - Tag = 0; - BoolValue = trackTotalHits; - } - - public TrackTotalHits(long trackTotalHitsUpTo) - { - Tag = 1; - LongValue = trackTotalHitsUpTo; - } - - public bool Equals(TrackTotalHits other) - { - if (Tag != other?.Tag) return false; - - return Tag switch - { - 0 => BoolValue == other.BoolValue, - 1 => LongValue == other.LongValue, - _ => false - }; - } - - private byte Tag { get; } - - internal bool? BoolValue { get; } - - internal long? LongValue { get; } - - private string BoolOrLongValue => BoolValue?.ToString(CultureInfo.InvariantCulture) ?? LongValue?.ToString(CultureInfo.InvariantCulture); - - private string DebugDisplay => BoolOrLongValue; - - public override string ToString() => BoolOrLongValue; - - public string GetString(IConnectionConfigurationValues settings) => BoolOrLongValue; - - public static implicit operator TrackTotalHits(bool trackTotalHits) => new(trackTotalHits); - public static implicit operator TrackTotalHits(bool? trackTotalHits) => trackTotalHits is {} b ? new TrackTotalHits(b) : null; - - public static implicit operator TrackTotalHits(long trackTotalHitsUpTo) => new(trackTotalHitsUpTo); - public static implicit operator TrackTotalHits(long? trackTotalHitsUpTo) => trackTotalHitsUpTo is {} l ? new TrackTotalHits(l) : null; - - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - - return obj switch - { - TrackTotalHits t => Equals(t), - bool b => Equals(b), - long l => Equals(l), - _ => false - }; - } - - private static int TypeHashCode { get; } = typeof(TrackTotalHits).GetHashCode(); - - public override int GetHashCode() - { - unchecked - { - var result = TypeHashCode; - result = (result * 397) ^ (BoolValue?.GetHashCode() ?? 0); - result = (result * 397) ^ (LongValue?.GetHashCode() ?? 0); - return result; - } - } - - public static bool operator ==(TrackTotalHits left, TrackTotalHits right) => Equals(left, right); - - public static bool operator !=(TrackTotalHits left, TrackTotalHits right) => !Equals(left, right); - } -} diff --git a/src/OpenSearch.Client/CommonAbstractions/Infer/TrackTotalHits/TrackTotalHitsFormatter.cs b/src/OpenSearch.Client/CommonAbstractions/Infer/TrackTotalHits/TrackTotalHitsFormatter.cs deleted file mode 100644 index ac27e26f2c..0000000000 --- a/src/OpenSearch.Client/CommonAbstractions/Infer/TrackTotalHits/TrackTotalHitsFormatter.cs +++ /dev/null @@ -1,29 +0,0 @@ -/* SPDX-License-Identifier: Apache-2.0 -* -* The OpenSearch Contributors require contributions made to -* this file be licensed under the Apache-2.0 license or a -* compatible open source license. -*/ - -using OpenSearch.Net.Utf8Json; - -namespace OpenSearch.Client -{ - internal class TrackTotalHitsFormatter : IJsonFormatter - { - public void Serialize(ref JsonWriter writer, TrackTotalHits value, IJsonFormatterResolver formatterResolver) - { - if (value?.LongValue is { } l) - writer.WriteInt64(l); - else if (value?.BoolValue is { } b) - writer.WriteBoolean(b); - else - writer.WriteNull(); - } - - public TrackTotalHits Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) => - reader.GetCurrentJsonToken() == JsonToken.Number - ? new TrackTotalHits(reader.ReadInt64()) - : new TrackTotalHits(reader.ReadBoolean()); - } -} diff --git a/src/OpenSearch.Client/Descriptors.NoNamespace.cs b/src/OpenSearch.Client/Descriptors.NoNamespace.cs index 2c7739be05..c11847f331 100644 --- a/src/OpenSearch.Client/Descriptors.NoNamespace.cs +++ b/src/OpenSearch.Client/Descriptors.NoNamespace.cs @@ -357,7 +357,7 @@ public DeleteByQueryDescriptor Index() ///Search operation type public DeleteByQueryDescriptor SearchType(SearchType? searchtype) => Qs("search_type", searchtype); ///The number of slices this task should be divided into. Defaults to 1, meaning the task isn't sliced into subtasks. - public DeleteByQueryDescriptor Slices(long? slices) => Qs("slices", slices); + public DeleteByQueryDescriptor Slices(Slices slices) => Qs("slices", slices); ///A comma-separated list of <field>:<direction> pairs public DeleteByQueryDescriptor Sort(params string[] sort) => Qs("sort", sort); ///Whether the _source should be included in the response. @@ -1171,7 +1171,7 @@ public partial class ReindexOnServerDescriptor : RequestDescriptorBaseControl how long to keep the search context alive public ReindexOnServerDescriptor Scroll(Time scroll) => Qs("scroll", scroll); ///The number of slices this task should be divided into. Defaults to 1, meaning the task isn't sliced into subtasks. Can be set to `auto`. - public ReindexOnServerDescriptor Slices(long? slices) => Qs("slices", slices); + public ReindexOnServerDescriptor Slices(Slices slices) => Qs("slices", slices); ///Time each individual bulk request should wait for shards that are unavailable. public ReindexOnServerDescriptor Timeout(Time timeout) => Qs("timeout", timeout); ///Sets the number of shard copies that must be active before proceeding with the reindex operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1) @@ -1651,7 +1651,7 @@ public UpdateByQueryDescriptor Index() ///Search operation type public UpdateByQueryDescriptor SearchType(SearchType? searchtype) => Qs("search_type", searchtype); ///The number of slices this task should be divided into. Defaults to 1, meaning the task isn't sliced into subtasks. Can be set to `auto`. - public UpdateByQueryDescriptor Slices(long? slices) => Qs("slices", slices); + public UpdateByQueryDescriptor Slices(Slices slices) => Qs("slices", slices); ///A comma-separated list of <field>:<direction> pairs public UpdateByQueryDescriptor Sort(params string[] sort) => Qs("sort", sort); ///Whether the _source should be included in the response. diff --git a/src/OpenSearch.Client/Document/Multiple/Slices/Slices.cs b/src/OpenSearch.Client/Document/Multiple/Slices/Slices.cs new file mode 100644 index 0000000000..50d90218da --- /dev/null +++ b/src/OpenSearch.Client/Document/Multiple/Slices/Slices.cs @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: Apache-2.0 +* +* The OpenSearch Contributors require contributions made to +* this file be licensed under the Apache-2.0 license or a +* compatible open source license. +*/ + +using OpenSearch.Net.Utf8Json; + +namespace OpenSearch.Client; + +[JsonFormatter(typeof(SlicesFormatter))] +public class Slices : Union +{ + public Slices(long value) : base(value) { } + + public Slices(string value) : base(value) { } + + public static implicit operator Slices(long value) => new(value); + public static implicit operator Slices(long? value) => value is { } v ? new Slices(v) : null; + public static implicit operator Slices(string value) => value is { } v ? new Slices(value) : null; + + public override string ToString() => Tag switch + { + 0 => Item1.ToString(), + 1 => Item2, + _ => null + }; +} diff --git a/src/OpenSearch.Client/Document/Multiple/Slices/SlicesFormatter.cs b/src/OpenSearch.Client/Document/Multiple/Slices/SlicesFormatter.cs new file mode 100644 index 0000000000..860eab7c75 --- /dev/null +++ b/src/OpenSearch.Client/Document/Multiple/Slices/SlicesFormatter.cs @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: Apache-2.0 +* +* The OpenSearch Contributors require contributions made to +* this file be licensed under the Apache-2.0 license or a +* compatible open source license. +*/ + +using OpenSearch.Net.Utf8Json; + +namespace OpenSearch.Client; + +internal class SlicesFormatter : IJsonFormatter +{ + private static readonly UnionFormatter UnionFormatter = new(); + + public void Serialize(ref JsonWriter writer, Slices value, IJsonFormatterResolver formatterResolver) => + UnionFormatter.Serialize(ref writer, value, formatterResolver); + + public Slices Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) + { + var union = UnionFormatter.Deserialize(ref reader, formatterResolver); + if (union == null) return null; + + return union.Tag switch + { + 0 => new Slices(union.Item1), + 1 => new Slices(union.Item2), + _ => null + }; + } +} diff --git a/src/OpenSearch.Client/Requests.NoNamespace.cs b/src/OpenSearch.Client/Requests.NoNamespace.cs index d4fe6c3400..75b9ba6108 100644 --- a/src/OpenSearch.Client/Requests.NoNamespace.cs +++ b/src/OpenSearch.Client/Requests.NoNamespace.cs @@ -771,9 +771,9 @@ public SearchType? SearchType } ///The number of slices this task should be divided into. Defaults to 1, meaning the task isn't sliced into subtasks. - public long? Slices + public Slices Slices { - get => Q("slices"); + get => Q("slices"); set => Q("slices", value); } @@ -2477,9 +2477,9 @@ public Time Scroll } ///The number of slices this task should be divided into. Defaults to 1, meaning the task isn't sliced into subtasks. Can be set to `auto`. - public long? Slices + public Slices Slices { - get => Q("slices"); + get => Q("slices"); set => Q("slices", value); } @@ -3605,9 +3605,9 @@ public SearchType? SearchType } ///The number of slices this task should be divided into. Defaults to 1, meaning the task isn't sliced into subtasks. Can be set to `auto`. - public long? Slices + public Slices Slices { - get => Q("slices"); + get => Q("slices"); set => Q("slices", value); } diff --git a/src/OpenSearch.Client/Search/TrackTotalHits/TrackTotalHits.cs b/src/OpenSearch.Client/Search/TrackTotalHits/TrackTotalHits.cs new file mode 100644 index 0000000000..7fedaa01f9 --- /dev/null +++ b/src/OpenSearch.Client/Search/TrackTotalHits/TrackTotalHits.cs @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: Apache-2.0 +* +* The OpenSearch Contributors require contributions made to +* this file be licensed under the Apache-2.0 license or a +* compatible open source license. +*/ + +using OpenSearch.Net.Utf8Json; + +namespace OpenSearch.Client; + +[JsonFormatter(typeof(TrackTotalHitsFormatter))] +public class TrackTotalHits : Union +{ + public TrackTotalHits(bool item) : base(item) { } + + public TrackTotalHits(long item) : base(item) { } + + public static implicit operator TrackTotalHits(bool trackTotalHits) => new(trackTotalHits); + public static implicit operator TrackTotalHits(bool? trackTotalHits) => trackTotalHits is {} b ? new TrackTotalHits(b) : null; + + public static implicit operator TrackTotalHits(long trackTotalHitsUpTo) => new(trackTotalHitsUpTo); + public static implicit operator TrackTotalHits(long? trackTotalHitsUpTo) => trackTotalHitsUpTo is {} l ? new TrackTotalHits(l) : null; + + public override string ToString() => Tag switch + { + 0 => Item1.ToString(), + 1 => Item2.ToString(), + _ => null + }; +} diff --git a/src/OpenSearch.Client/Search/TrackTotalHits/TrackTotalHitsFormatter.cs b/src/OpenSearch.Client/Search/TrackTotalHits/TrackTotalHitsFormatter.cs new file mode 100644 index 0000000000..7c3f3a3ffa --- /dev/null +++ b/src/OpenSearch.Client/Search/TrackTotalHits/TrackTotalHitsFormatter.cs @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: Apache-2.0 +* +* The OpenSearch Contributors require contributions made to +* this file be licensed under the Apache-2.0 license or a +* compatible open source license. +*/ + +using OpenSearch.Net.Utf8Json; + +namespace OpenSearch.Client; + +internal class TrackTotalHitsFormatter : IJsonFormatter +{ + private static readonly UnionFormatter UnionFormatter = new(); + + public void Serialize(ref JsonWriter writer, TrackTotalHits value, IJsonFormatterResolver formatterResolver) => + UnionFormatter.Serialize(ref writer, value, formatterResolver); + + public TrackTotalHits Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) + { + var union = UnionFormatter.Deserialize(ref reader, formatterResolver); + if (union == null) return null; + return union.Tag switch + { + 0 => new TrackTotalHits(union.Item1), + 1 => new TrackTotalHits(union.Item2), + _ => null + }; + } +}