Skip to content

Commit eba1a5a

Browse files
committed
Add is_write_index to aliases on create index API (#3704)
This commit adds the ability to add an alias with is_write_index when creating an index using the Create Index API. Closes #3686
1 parent c7ffac8 commit eba1a5a

File tree

3 files changed

+147
-18
lines changed

3 files changed

+147
-18
lines changed

src/Nest/Indices/AliasManagement/Alias.cs

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,83 @@
33

44
namespace Nest
55
{
6+
/// <summary>
7+
/// An alias to one or more indices
8+
/// </summary>
69
[ReadAs(typeof(Alias))]
710
public interface IAlias
811
{
9-
[DataMember(Name ="filter")]
12+
/// <summary>
13+
/// Provides an easy way to create different "views" of the same index. A filter can be defined using Query DSL and is
14+
/// applied to all Search, Count, Delete By Query and More Like This operations with this alias.
15+
/// </summary>
16+
[DataMember(Name = "filter")]
1017
QueryContainer Filter { get; set; }
1118

12-
[DataMember(Name ="index_routing")]
19+
/// <summary>
20+
/// Associates routing values with aliases for index operations. This feature can be used together
21+
/// with filtering aliases in order to avoid unnecessary shard operations.
22+
/// </summary>
23+
[DataMember(Name = "index_routing")]
1324
Routing IndexRouting { get; set; }
1425

15-
[DataMember(Name ="routing")]
26+
/// <inheritdoc cref="AliasAddOperation.IsWriteIndex" />
27+
[DataMember(Name = "is_write_index")]
28+
bool? IsWriteIndex { get; set; }
29+
30+
/// <summary>
31+
/// Associates routing values with aliases for both index and search operations. This feature can be used together
32+
/// with filtering aliases in order to avoid unnecessary shard operations.
33+
/// </summary>
34+
[DataMember(Name = "routing")]
1635
Routing Routing { get; set; }
1736

18-
[DataMember(Name ="search_routing")]
37+
/// <summary>
38+
/// Associates routing values with aliases for search operations. This feature can be used together
39+
/// with filtering aliases in order to avoid unnecessary shard operations.
40+
/// </summary>
41+
[DataMember(Name = "search_routing")]
1942
Routing SearchRouting { get; set; }
2043
}
2144

45+
/// <inheritdoc />
2246
public class Alias : IAlias
2347
{
48+
/// <inheritdoc />
2449
public QueryContainer Filter { get; set; }
50+
/// <inheritdoc />
2551
public Routing IndexRouting { get; set; }
52+
/// <inheritdoc />
53+
public bool? IsWriteIndex { get; set; }
54+
/// <inheritdoc />
2655
public Routing Routing { get; set; }
56+
/// <inheritdoc />
2757
public Routing SearchRouting { get; set; }
2858
}
2959

60+
/// <inheritdoc cref="IAlias" />
3061
public class AliasDescriptor : DescriptorBase<AliasDescriptor, IAlias>, IAlias
3162
{
3263
QueryContainer IAlias.Filter { get; set; }
3364
Routing IAlias.IndexRouting { get; set; }
65+
bool? IAlias.IsWriteIndex { get; set; }
3466
Routing IAlias.Routing { get; set; }
3567
Routing IAlias.SearchRouting { get; set; }
3668

37-
public AliasDescriptor Routing(Routing routing) => Assign(routing, (a, v) => a.Routing = v);
69+
/// <inheritdoc cref="IAlias.Filter" />
70+
public AliasDescriptor Filter<T>(Func<QueryContainerDescriptor<T>, QueryContainer> filterSelector) where T : class =>
71+
Assign(filterSelector, (a, v) => a.Filter = v?.Invoke(new QueryContainerDescriptor<T>()));
3872

73+
/// <inheritdoc cref="IAlias.IndexRouting" />
3974
public AliasDescriptor IndexRouting(Routing indexRouting) => Assign(indexRouting, (a, v) => a.IndexRouting = v);
4075

41-
public AliasDescriptor SearchRouting(Routing searchRouting) => Assign(searchRouting, (a, v) => a.SearchRouting = v);
76+
/// <inheritdoc cref="IAlias.IsWriteIndex" />
77+
public AliasDescriptor IsWriteIndex(bool? isWriteIndex = true) => Assign(isWriteIndex, (a, v) => a.IsWriteIndex = v);
4278

43-
public AliasDescriptor Filter<T>(Func<QueryContainerDescriptor<T>, QueryContainer> filterSelector) where T : class =>
44-
Assign(filterSelector, (a, v) => a.Filter = v?.Invoke(new QueryContainerDescriptor<T>()));
79+
/// <inheritdoc cref="IAlias.Routing" />
80+
public AliasDescriptor Routing(Routing routing) => Assign(routing, (a, v) => a.Routing = v);
81+
82+
/// <inheritdoc cref="IAlias.SearchRouting" />
83+
public AliasDescriptor SearchRouting(Routing searchRouting) => Assign(searchRouting, (a, v) => a.SearchRouting = v);
4584
}
4685
}

src/Nest/Indices/AliasManagement/PutAlias/PutAliasRequest.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,45 @@
33

44
namespace Nest
55
{
6+
/// <summary>
7+
/// A request to put an alias to one or more indices
8+
/// </summary>
69
[MapsApi("indices.put_alias.json")]
710
public partial interface IPutAliasRequest
811
{
9-
[DataMember(Name ="filter")]
12+
/// <inheritdoc cref="IAlias.Filter"/>
13+
[DataMember(Name = "filter")]
1014
QueryContainer Filter { get; set; }
1115

12-
[DataMember(Name ="index_routing")]
16+
/// <inheritdoc cref="IAlias.IndexRouting"/>
17+
[DataMember(Name = "index_routing")]
1318
Routing IndexRouting { get; set; }
1419

15-
/// <inheritdoc cref="AliasAddOperation.IsWriteIndex" />
16-
[DataMember(Name ="is_write_index")]
20+
/// <inheritdoc cref="IAlias.IsWriteIndex" />
21+
[DataMember(Name = "is_write_index")]
1722
bool? IsWriteIndex { get; set; }
1823

19-
[DataMember(Name ="routing")]
24+
/// <inheritdoc cref="IAlias.Routing"/>
25+
[DataMember(Name = "routing")]
2026
Routing Routing { get; set; }
2127

22-
[DataMember(Name ="search_routing")]
28+
/// <inheritdoc cref="IAlias.SearchRouting"/>
29+
[DataMember(Name = "search_routing")]
2330
Routing SearchRouting { get; set; }
2431
}
2532

33+
/// <inheritdoc cref="IPutAliasRequest"/>
2634
public partial class PutAliasRequest
2735
{
36+
/// <inheritdoc cref="IPutAliasRequest.Filter"/>
2837
public QueryContainer Filter { get; set; }
38+
/// <inheritdoc cref="IPutAliasRequest.IndexRouting"/>
2939
public Routing IndexRouting { get; set; }
30-
31-
/// <inheritdoc cref="AliasAddOperation.IsWriteIndex" />
40+
/// <inheritdoc cref="IPutAliasRequest.IsWriteIndex" />
3241
public bool? IsWriteIndex { get; set; }
33-
42+
/// <inheritdoc cref="IPutAliasRequest.Routing"/>
3443
public Routing Routing { get; set; }
44+
/// <inheritdoc cref="IPutAliasRequest.SearchRouting"/>
3545
public Routing SearchRouting { get; set; }
3646
}
3747

@@ -43,15 +53,19 @@ public partial class PutAliasDescriptor
4353
Routing IPutAliasRequest.Routing { get; set; }
4454
Routing IPutAliasRequest.SearchRouting { get; set; }
4555

56+
/// <inheritdoc cref="IPutAliasRequest.Routing"/>
4657
public PutAliasDescriptor Routing(Routing routing) => Assign(routing, (a, v) => a.Routing = v);
4758

59+
/// <inheritdoc cref="IPutAliasRequest.IndexRouting"/>
4860
public PutAliasDescriptor IndexRouting(Routing routing) => Assign(routing, (a, v) => a.IndexRouting = v);
4961

62+
/// <inheritdoc cref="IPutAliasRequest.SearchRouting"/>
5063
public PutAliasDescriptor SearchRouting(Routing routing) => Assign(routing, (a, v) => a.SearchRouting = v);
5164

52-
/// <inheritdoc cref="AliasAddOperation.IsWriteIndex" />
65+
/// <inheritdoc cref="IPutAliasRequest.IsWriteIndex" />
5366
public PutAliasDescriptor IsWriteIndex(bool? isWriteIndex = true) => Assign(isWriteIndex, (a, v) => a.IsWriteIndex = v);
5467

68+
/// <inheritdoc cref="IPutAliasRequest.Filter"/>
5569
public PutAliasDescriptor Filter<T>(Func<QueryContainerDescriptor<T>, QueryContainer> filterSelector) where T : class =>
5670
Assign(filterSelector, (a, v) => a.Filter = v?.Invoke(new QueryContainerDescriptor<T>()));
5771
}

src/Tests/Tests/Indices/IndexManagement/CreateIndex/CreateIndexApiTests.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,80 @@ protected override void ExpectResponse(CreateIndexResponse response)
240240
((InlineScript)scriptedSimilarity.Script).Source.Should().NotBeNullOrEmpty();
241241
}
242242
}
243+
244+
public class CreateIndexWithAliasApiTests
245+
: ApiIntegrationTestBase<WritableCluster, CreateIndexResponse, ICreateIndexRequest, CreateIndexDescriptor, CreateIndexRequest>
246+
{
247+
public CreateIndexWithAliasApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
248+
249+
protected override bool ExpectIsValid => true;
250+
251+
protected override object ExpectJson => new
252+
{
253+
settings = new Dictionary<string, object>
254+
{
255+
{ "index.number_of_replicas", 0 },
256+
{ "index.number_of_shards", 1 },
257+
},
258+
aliases = new Dictionary<string, object>
259+
{
260+
{ CallIsolatedValue + "-alias", new { is_write_index = true } }
261+
}
262+
};
263+
264+
protected override int ExpectStatusCode => 200;
265+
266+
protected override Func<CreateIndexDescriptor, ICreateIndexRequest> Fluent => d => d
267+
.Settings(s => s
268+
.NumberOfReplicas(0)
269+
.NumberOfShards(1)
270+
)
271+
.Aliases(a => a
272+
.Alias(CallIsolatedValue + "-alias", aa => aa
273+
.IsWriteIndex()
274+
)
275+
);
276+
277+
protected override HttpMethod HttpMethod => HttpMethod.PUT;
278+
279+
protected override CreateIndexRequest Initializer => new CreateIndexRequest(CallIsolatedValue)
280+
{
281+
Settings = new Nest.IndexSettings
282+
{
283+
NumberOfReplicas = 0,
284+
NumberOfShards = 1,
285+
},
286+
Aliases = new Aliases
287+
{
288+
{ CallIsolatedValue + "-alias", new Alias { IsWriteIndex = true } }
289+
}
290+
};
291+
292+
protected override string UrlPath => $"/{CallIsolatedValue}";
293+
294+
protected override LazyResponses ClientUsage() => Calls(
295+
(client, f) => client.CreateIndex(CallIsolatedValue, f),
296+
(client, f) => client.CreateIndexAsync(CallIsolatedValue, f),
297+
(client, r) => client.CreateIndex(r),
298+
(client, r) => client.CreateIndexAsync(r)
299+
);
300+
301+
protected override CreateIndexDescriptor NewDescriptor() => new CreateIndexDescriptor(CallIsolatedValue);
302+
303+
protected override void ExpectResponse(CreateIndexResponse response)
304+
{
305+
response.ShouldBeValid();
306+
response.Acknowledged.Should().BeTrue();
307+
response.ShardsAcknowledged.Should().BeTrue();
308+
309+
var indexResponse = Client.GetIndex(CallIsolatedValue);
310+
311+
indexResponse.ShouldBeValid();
312+
indexResponse.Indices.Should().NotBeEmpty().And.ContainKey(CallIsolatedValue);
313+
314+
var aliases = indexResponse.Indices[CallIsolatedValue].Aliases;
315+
aliases.Count.Should().Be(1);
316+
aliases[CallIsolatedValue + "-alias"].IsWriteIndex.Should().BeTrue();
317+
}
318+
}
243319
}

0 commit comments

Comments
 (0)