Skip to content

[Backport master] Support multiple expand_wildcards #5319

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 2 commits into from
Feb 16, 2021
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
45 changes: 45 additions & 0 deletions src/Nest/XPack/Watcher/Input/ExpandWildcardsFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 System.Linq;
using Elasticsearch.Net;
using Nest.Utf8Json;

namespace Nest
{
internal class ExpandWildcardsFormatter : IJsonFormatter<IEnumerable<ExpandWildcards>>
{
public IEnumerable<ExpandWildcards> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
{
var token = reader.GetCurrentJsonToken();
return token == JsonToken.BeginArray
? formatterResolver.GetFormatter<IEnumerable<ExpandWildcards>>().Deserialize(ref reader, formatterResolver)
: new[] { formatterResolver.GetFormatter<ExpandWildcards>().Deserialize(ref reader, formatterResolver) };
}

public void Serialize(ref JsonWriter writer, IEnumerable<ExpandWildcards> value, IJsonFormatterResolver formatterResolver)
{
if (value == null)
{
writer.WriteNull();
return;
}

var wildcards = value.ToArray();

switch (wildcards.Length)
{
case 1:
var singleFormatter = formatterResolver.GetFormatter<ExpandWildcards>();
singleFormatter.Serialize(ref writer, wildcards.First(), formatterResolver);
break;
case > 1:
var formatter = formatterResolver.GetFormatter<IEnumerable<ExpandWildcards>>();
formatter.Serialize(ref writer, wildcards, formatterResolver);
break;
}
}
}
}
21 changes: 17 additions & 4 deletions src/Nest/XPack/Watcher/Input/IndicesOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 System.Runtime.Serialization;
using Elasticsearch.Net;
using Nest.Utf8Json;
Expand All @@ -15,28 +16,40 @@ public interface IIndicesOptions
[DataMember(Name = "allow_no_indices")]
bool? AllowNoIndices { get; set; }

/// <summary>
/// Determines how to expand indices wildcards.
/// <para>NOTE: Elasticsearch 7.10.0 and prior supports only a single value. Elasticsearch 7.10.1 and later support multiple values.</para>
/// </summary>
[DataMember(Name = "expand_wildcards")]
ExpandWildcards? ExpandWildcards { get; set; }
[JsonFormatter(typeof(ExpandWildcardsFormatter))]
IEnumerable<ExpandWildcards> ExpandWildcards { get; set; }

[DataMember(Name = "ignore_unavailable")]
bool? IgnoreUnavailable { get; set; }
}

[DataContract]

public class IndicesOptions : IIndicesOptions
{
public bool? AllowNoIndices { get; set; }
public ExpandWildcards? ExpandWildcards { get; set; }
/// <inheritdoc />
public IEnumerable<ExpandWildcards> ExpandWildcards { get; set; }
public bool? IgnoreUnavailable { get; set; }
}

public class IndicesOptionsDescriptor : DescriptorBase<IndicesOptionsDescriptor, IIndicesOptions>, IIndicesOptions
{
bool? IIndicesOptions.AllowNoIndices { get; set; }
ExpandWildcards? IIndicesOptions.ExpandWildcards { get; set; }
IEnumerable<ExpandWildcards> IIndicesOptions.ExpandWildcards { get; set; }
bool? IIndicesOptions.IgnoreUnavailable { get; set; }

public IndicesOptionsDescriptor ExpandWildcards(ExpandWildcards? expandWildcards) =>
/// <inheritdoc cref="IIndicesOptions.ExpandWildcards"/>
public IndicesOptionsDescriptor ExpandWildcards(IEnumerable<ExpandWildcards> expandWildcards) =>
Assign(expandWildcards, (a, v) => a.ExpandWildcards = v);

/// <inheritdoc cref="IIndicesOptions.ExpandWildcards"/>
public IndicesOptionsDescriptor ExpandWildcards(params ExpandWildcards[] expandWildcards) =>
Assign(expandWildcards, (a, v) => a.ExpandWildcards = v);

public IndicesOptionsDescriptor IgnoreUnavailable(bool? ignoreUnavailable = true) =>
Expand Down
1 change: 1 addition & 0 deletions tests/Tests/XPack/Watcher/GetWatch/GetWatchApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public static void PutWatch(IElasticClient client, CallUniqueValues values)
.Indices(typeof(Project))
.SearchType(SearchType.DfsQueryThenFetch)
.IndicesOptions(io => io
.AllowNoIndices()
.ExpandWildcards(ExpandWildcards.Open)
.IgnoreUnavailable()
)
Expand Down
Loading