Skip to content

Commit 7abd1ea

Browse files
stevejgordonflobernd
authored andcommitted
Add WithAlias extension methods for CreateIndexRequestDescriptor
1 parent 8a074ef commit 7abd1ea

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
7+
#if ELASTICSEARCH_SERVERLESS
8+
namespace Elastic.Clients.Elasticsearch.Serverless.IndexManagement;
9+
#else
10+
namespace Elastic.Clients.Elasticsearch.IndexManagement;
11+
#endif
12+
13+
public static class CreateIndexRequestDescriptorExtensions
14+
{
15+
/// <summary>
16+
/// Add multiple aliases to the index at creation time.
17+
/// </summary>
18+
/// <param name="descriptor">A descriptor for an index request.</param>
19+
/// <param name="aliasName">The name of the alias.</param>
20+
/// <returns>The <see cref="CreateIndexRequestDescriptor"/> to allow fluent chaining of calls to configure the indexing request.</returns>
21+
public static CreateIndexRequestDescriptor WithAlias(this CreateIndexRequestDescriptor descriptor, string aliasName)
22+
{
23+
#if NET8_0_OR_GREATER
24+
ArgumentException.ThrowIfNullOrEmpty(aliasName);
25+
#else
26+
if (string.IsNullOrEmpty(aliasName))
27+
throw new ArgumentNullException(nameof(aliasName));
28+
#endif
29+
30+
descriptor.Aliases(a => a.Add(aliasName, static _ => { }));
31+
return descriptor;
32+
}
33+
34+
/// <summary>
35+
/// Add multiple aliases to the index at creation time.
36+
/// </summary>
37+
/// <param name="descriptor">A descriptor for an index request.</param>
38+
/// <param name="aliasNames">The names of the aliases.</param>
39+
/// <returns>The <see cref="CreateIndexRequestDescriptor"/> to allow fluent chaining of calls to configure the indexing request.</returns>
40+
public static CreateIndexRequestDescriptor WithAliases(this CreateIndexRequestDescriptor descriptor, params ReadOnlySpan<string> aliasNames)
41+
{
42+
foreach (var name in aliasNames)
43+
descriptor.Aliases(a => a.Add(name, static _ => { }));
44+
45+
return descriptor;
46+
}
47+
48+
/// <summary>
49+
/// Add multiple aliases to the index at creation time.
50+
/// </summary>
51+
/// <param name="descriptor">A descriptor for an index request.</param>
52+
/// <param name="aliasNames">The names of the aliases.</param>
53+
/// <returns>The <see cref="CreateIndexRequestDescriptor"/> to allow fluent chaining of calls to configure the indexing request.</returns>
54+
public static CreateIndexRequestDescriptor WithAliases(this CreateIndexRequestDescriptor descriptor, params string[] aliasNames)
55+
{
56+
foreach (var name in aliasNames)
57+
descriptor.Aliases(a => a.Add(name, static _ => { }));
58+
59+
return descriptor;
60+
}
61+
62+
/// <summary>
63+
/// Adds an alias to the index at creation time.
64+
/// </summary>
65+
/// <typeparam name="TDocument">The type representing documents stored in this index.</typeparam>
66+
/// <param name="descriptor">A fluent descriptor for an index request.</param>
67+
/// <param name="aliasName">The name of the alias.</param>
68+
/// <returns>The <see cref="CreateIndexRequestDescriptor{TDocument}"/> to allow fluent chaining of calls to configure the indexing request.</returns>
69+
public static CreateIndexRequestDescriptor<TDocument> WithAlias<TDocument>(this CreateIndexRequestDescriptor<TDocument> descriptor, string aliasName)
70+
{
71+
#if NET8_0_OR_GREATER
72+
ArgumentException.ThrowIfNullOrEmpty(aliasName);
73+
#else
74+
if (string.IsNullOrEmpty(aliasName))
75+
throw new ArgumentNullException(nameof(aliasName));
76+
#endif
77+
78+
descriptor.Aliases(a => a.Add(aliasName, static _ => { }));
79+
return descriptor;
80+
}
81+
82+
/// <summary>
83+
/// Add multiple aliases to the index at creation time.
84+
/// </summary>
85+
/// <typeparam name="TDocument">The type representing documents stored in this index.</typeparam>
86+
/// <param name="descriptor">A fluent descriptor for an index request.</param>
87+
/// <param name="aliasNames">The names of the aliases.</param>
88+
/// <returns>The <see cref="CreateIndexRequestDescriptor{TDocument}"/> to allow fluent chaining of calls to configure the indexing request.</returns>
89+
public static CreateIndexRequestDescriptor<TDocument> WithAliases<TDocument>(this CreateIndexRequestDescriptor<TDocument> descriptor, params ReadOnlySpan<string> aliasNames)
90+
{
91+
foreach (var name in aliasNames)
92+
descriptor.Aliases(a => a.Add(name, static _ => { }));
93+
94+
return descriptor;
95+
}
96+
97+
/// <summary>
98+
/// Add multiple aliases to the index at creation time.
99+
/// </summary>
100+
/// <typeparam name="TDocument">The type representing documents stored in this index.</typeparam>
101+
/// <param name="descriptor">A fluent descriptor for an index request.</param>
102+
/// <param name="aliasNames">The names of the aliases.</param>
103+
/// <returns>The <see cref="CreateIndexRequestDescriptor{TDocument}"/> to allow fluent chaining of calls to configure the indexing request.</returns>
104+
public static CreateIndexRequestDescriptor<TDocument> WithAliases<TDocument>(this CreateIndexRequestDescriptor<TDocument> descriptor, params string[] aliasNames)
105+
{
106+
foreach (var name in aliasNames)
107+
descriptor.Aliases(a => a.Add(name, static _ => { }));
108+
109+
return descriptor;
110+
}
111+
}

0 commit comments

Comments
 (0)