diff --git a/src/Elastic.Clients.Elasticsearch.Shared/Types/QueryDsl/RangeQuery.cs b/src/Elastic.Clients.Elasticsearch.Shared/Types/QueryDsl/RangeQuery.cs new file mode 100644 index 00000000000..269e2c247eb --- /dev/null +++ b/src/Elastic.Clients.Elasticsearch.Shared/Types/QueryDsl/RangeQuery.cs @@ -0,0 +1,90 @@ +// 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; + +#if ELASTICSEARCH_SERVERLESS +using Elastic.Clients.Elasticsearch.Serverless.Fluent; +#else +using Elastic.Clients.Elasticsearch.Fluent; +#endif + +#if ELASTICSEARCH_SERVERLESS +namespace Elastic.Clients.Elasticsearch.Serverless.QueryDsl; +#else +namespace Elastic.Clients.Elasticsearch.QueryDsl; +#endif + +// TODO: This should be removed after implementing descriptor generation for union types + +public sealed partial class QueryDescriptor +{ + public QueryDescriptor Range(Action> configure) => ProxiedSet(configure, "range"); + + private QueryDescriptor ProxiedSet(Action descriptorAction, string variantName) where T : ProxiedDescriptor + { + var descriptor = (T)Activator.CreateInstance(typeof(T), true); + descriptorAction?.Invoke(descriptor); + + return Set(descriptor.Result, variantName); + } +} + +public sealed partial class QueryDescriptor +{ + public QueryDescriptor Range(Action configure) => ProxiedSet(configure, "range"); + public QueryDescriptor Range(Action> configure) => ProxiedSet(configure, "range"); + + private QueryDescriptor ProxiedSet(Action descriptorAction, string variantName) where T : ProxiedDescriptor + { + var descriptor = (T)Activator.CreateInstance(typeof(T), true); + descriptorAction?.Invoke(descriptor); + + return Set(descriptor.Result, variantName); + } +} + +public abstract class ProxiedDescriptor : Descriptor + where T : Descriptor +{ + internal Descriptor Result { get; set; } + + protected T SetResult(Action descriptorAction) where TD : Descriptor + { + var descriptor = (TD)Activator.CreateInstance(typeof(TD), true); + descriptorAction?.Invoke(descriptor); + Result = descriptor; + return Self; + } +} + +public sealed class RangeQueryDescriptor : ProxiedDescriptor> +{ + public RangeQueryDescriptor NumberRange(Action> configure) => + SetResult(configure); + + public RangeQueryDescriptor DateRange(Action> configure) => + SetResult(configure); +} + +public sealed class RangeQueryDescriptor : ProxiedDescriptor +{ + public RangeQueryDescriptor NumberRange(Action configure) => SetResult(configure); + + public RangeQueryDescriptor NumberRange(Action> configure) => SetResult(configure); + + public RangeQueryDescriptor DateRange(Action configure) => SetResult(configure); + + public RangeQueryDescriptor DateRange(Action> configure) => SetResult(configure); +} + +public sealed partial class NumberRangeQuery +{ + public static implicit operator Query(NumberRangeQuery numberRangeQuery) => Query.Range(new RangeQuery(numberRangeQuery)); +} + +public sealed partial class DateRangeQuery +{ + public static implicit operator Query(DateRangeQuery dateRangeQuery) => Query.Range(new RangeQuery(dateRangeQuery)); +}