Skip to content

Commit 7f3bf4d

Browse files
committed
Merge pull request #4599 from elastic/feature/hidden-indices-aliases
Implement hidden indices and aliases (cherry picked from commit 763ed83)
1 parent c034f1d commit 7f3bf4d

File tree

7 files changed

+164
-6
lines changed

7 files changed

+164
-6
lines changed

src/Nest/IndexModules/IndexSettings/Settings/FixedIndexSettings.cs

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ public static class FixedIndexSettings
1010
public const string NumberOfShards = "index.number_of_shards";
1111
public const string RoutingPartitionSize = "index.routing_partition_size";
1212

13+
/// <summary>
14+
/// Indicates whether the index should be hidden by default.
15+
/// Hidden indices are not returned by default when using a wildcard expression.
16+
/// </summary>
17+
public const string Hidden = "index.hidden";
18+
1319
/// <summary>
1420
/// If a field referred to in a percolator query does not exist,
1521
/// it will be handled as a default text field so that adding the percolator query doesn't fail.

src/Nest/IndexModules/IndexSettings/Settings/IndexSettings.cs

+13
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public interface IIndexSettings : IDynamicIndexSettings
3939
/// </summary>
4040
int? RoutingPartitionSize { get; set; }
4141

42+
/// <summary>
43+
/// Indicates whether the index should be hidden by default.
44+
/// Hidden indices are not returned by default when using a wildcard expression.
45+
/// </summary>
46+
bool? Hidden { get; set; }
47+
4248
/// <summary>
4349
/// Settings associated with index sorting.
4450
/// https://www.elastic.co/guide/en/elasticsearch/reference/6.0/index-modules-index-sorting.html
@@ -71,6 +77,9 @@ public IndexSettings(IDictionary<string, object> container) : base(container) {
7177
/// <inheritdoc cref="IIndexSettings.RoutingPartitionSize" />
7278
public int? RoutingPartitionSize { get; set; }
7379

80+
/// <inheritdoc cref="IIndexSettings.Hidden" />
81+
public bool? Hidden { get; set; }
82+
7483
/// <inheritdoc cref="IIndexSettings.Sorting" />
7584
public ISortingSettings Sorting { get; set; }
7685

@@ -95,6 +104,10 @@ public IndexSettingsDescriptor NumberOfRoutingShards(int? numberOfRoutingShards)
95104
public IndexSettingsDescriptor RoutingPartitionSize(int? routingPartitionSize) =>
96105
Assign(routingPartitionSize, (a, v) => a.RoutingPartitionSize = v);
97106

107+
/// <inheritdoc cref="IIndexSettings.Hidden" />
108+
public IndexSettingsDescriptor Hidden(bool? hidden = true) =>
109+
Assign(hidden, (a, v) => a.Hidden = v);
110+
98111
/// <inheritdoc cref="IIndexSettings.FileSystemStorageImplementation" />
99112
public IndexSettingsDescriptor FileSystemStorageImplementation(FileSystemStorageImplementation? fs) =>
100113
Assign(fs, (a, v) => a.FileSystemStorageImplementation = v);

src/Nest/IndexModules/IndexSettings/Settings/IndexSettingsFormatter.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ void Set(string knownKey, object newValue)
124124
Set(NumberOfShards, indexSettings.NumberOfShards);
125125
Set(NumberOfRoutingShards, indexSettings.NumberOfRoutingShards);
126126
Set(RoutingPartitionSize, indexSettings.RoutingPartitionSize);
127+
Set(Hidden, indexSettings.Hidden);
127128
if (indexSettings.SoftDeletes != null)
128129
{
129130
#pragma warning disable 618
@@ -159,7 +160,7 @@ private static Dictionary<string, object> Flatten(Dictionary<string, object> ori
159160
Dictionary<string, object> current = null
160161
)
161162
{
162-
current = current ?? new Dictionary<string, object>();
163+
current ??= new Dictionary<string, object>();
163164
foreach (var property in original)
164165
{
165166
if (property.Value is Dictionary<string, object> objects &&
@@ -251,6 +252,7 @@ private static void SetKnownIndexSettings(ref JsonReader reader, IJsonFormatterR
251252
Set<int?>(s, settings, NumberOfShards, v => s.NumberOfShards = v, formatterResolver);
252253
Set<int?>(s, settings, NumberOfRoutingShards, v => s.NumberOfRoutingShards = v, formatterResolver);
253254
Set<int?>(s, settings, RoutingPartitionSize, v => s.RoutingPartitionSize = v, formatterResolver);
255+
Set<bool?>(s, settings, Hidden, v => s.Hidden = v, formatterResolver);
254256
Set<FileSystemStorageImplementation?>(s, settings, StoreType, v => s.FileSystemStorageImplementation = v, formatterResolver);
255257

256258
var sorting = s.Sorting = new SortingSettings();

src/Nest/Indices/AliasManagement/Alias.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,23 @@ public interface IAlias
2323
[DataMember(Name = "index_routing")]
2424
Routing IndexRouting { get; set; }
2525

26-
/// <inheritdoc cref="AliasAddOperation.IsWriteIndex" />
26+
/// <summary>
27+
/// If an alias points to multiple indices, Elasticsearch will reject the write operations
28+
/// unless one is explicitly marked as the write alias using this property.
29+
/// </summary>
2730
[DataMember(Name = "is_write_index")]
2831
bool? IsWriteIndex { get; set; }
2932

33+
/// <summary>
34+
/// If true, the alias will be excluded from wildcard expressions by default, unless overriden in the request using
35+
/// the expand_wildcards parameter, similar to hidden indices.
36+
/// This property must be set to the same value on all indices that share an alias. Defaults to false.
37+
/// <para />
38+
/// Available in Elasticsearch 7.7.0+
39+
/// </summary>
40+
[DataMember(Name = "is_hidden")]
41+
bool? IsHidden { get; set; }
42+
3043
/// <summary>
3144
/// Associates routing values with aliases for both index and search operations. This feature can be used together
3245
/// with filtering aliases in order to avoid unnecessary shard operations.
@@ -51,6 +64,9 @@ public class Alias : IAlias
5164
public Routing IndexRouting { get; set; }
5265
/// <inheritdoc />
5366
public bool? IsWriteIndex { get; set; }
67+
/// <inheritdoc />
68+
public bool? IsHidden { get; set; }
69+
5470
/// <inheritdoc />
5571
public Routing Routing { get; set; }
5672
/// <inheritdoc />
@@ -65,6 +81,7 @@ public class AliasDescriptor : DescriptorBase<AliasDescriptor, IAlias>, IAlias
6581
bool? IAlias.IsWriteIndex { get; set; }
6682
Routing IAlias.Routing { get; set; }
6783
Routing IAlias.SearchRouting { get; set; }
84+
bool? IAlias.IsHidden { get; set; }
6885

6986
/// <inheritdoc cref="IAlias.Filter" />
7087
public AliasDescriptor Filter<T>(Func<QueryContainerDescriptor<T>, QueryContainer> filterSelector) where T : class =>
@@ -76,6 +93,9 @@ public AliasDescriptor Filter<T>(Func<QueryContainerDescriptor<T>, QueryContaine
7693
/// <inheritdoc cref="IAlias.IsWriteIndex" />
7794
public AliasDescriptor IsWriteIndex(bool? isWriteIndex = true) => Assign(isWriteIndex, (a, v) => a.IsWriteIndex = v);
7895

96+
/// <inheritdoc cref="IAlias.IsHidden" />
97+
public AliasDescriptor IsHidden(bool? isHidden = true) => Assign(isHidden, (a, v) => a.IsHidden = v);
98+
7999
/// <inheritdoc cref="IAlias.Routing" />
80100
public AliasDescriptor Routing(Routing routing) => Assign(routing, (a, v) => a.Routing = v);
81101

src/Nest/Indices/AliasManagement/Alias/Actions/AliasAdd.cs

+16
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,70 @@ public class AliasAddDescriptor : DescriptorBase<AliasAddDescriptor, IAliasAddAc
2222

2323
AliasAddOperation IAliasAddAction.Add { get; set; }
2424

25+
/// <inheritdoc cref="AliasAddOperation.Index"/>
2526
public AliasAddDescriptor Index(string index)
2627
{
2728
Self.Add.Index = index;
2829
return this;
2930
}
3031

32+
/// <inheritdoc cref="AliasAddOperation.Index"/>
3133
public AliasAddDescriptor Index(Type index)
3234
{
3335
Self.Add.Index = index;
3436
return this;
3537
}
3638

39+
/// <inheritdoc cref="AliasAddOperation.Index"/>
3740
public AliasAddDescriptor Index<T>() where T : class
3841
{
3942
Self.Add.Index = typeof(T);
4043
return this;
4144
}
4245

46+
/// <inheritdoc cref="AliasAddOperation.Alias"/>
4347
public AliasAddDescriptor Alias(string alias)
4448
{
4549
Self.Add.Alias = alias;
4650
return this;
4751
}
4852

53+
/// <inheritdoc cref="AliasAddOperation.Routing"/>
4954
public AliasAddDescriptor Routing(string routing)
5055
{
5156
Self.Add.Routing = routing;
5257
return this;
5358
}
5459

60+
/// <inheritdoc cref="AliasAddOperation.IndexRouting"/>
5561
public AliasAddDescriptor IndexRouting(string indexRouting)
5662
{
5763
Self.Add.IndexRouting = indexRouting;
5864
return this;
5965
}
6066

67+
/// <inheritdoc cref="AliasAddOperation.SearchRouting"/>
6168
public AliasAddDescriptor SearchRouting(string searchRouting)
6269
{
6370
Self.Add.SearchRouting = searchRouting;
6471
return this;
6572
}
6673

74+
/// <inheritdoc cref="AliasAddOperation.IsWriteIndex"/>
6775
public AliasAddDescriptor IsWriteIndex(bool? isWriteIndex = true)
6876
{
6977
Self.Add.IsWriteIndex = isWriteIndex;
7078
return this;
7179
}
7280

81+
/// <inheritdoc cref="AliasAddOperation.IsHidden"/>
82+
public AliasAddDescriptor IsHidden(bool? isHidden = true)
83+
{
84+
Self.Add.IsHidden = isHidden;
85+
return this;
86+
}
87+
88+
/// <inheritdoc cref="AliasAddOperation.Filter"/>
7389
public AliasAddDescriptor Filter<T>(Func<QueryContainerDescriptor<T>, QueryContainer> filterSelector)
7490
where T : class
7591
{

src/Nest/Indices/AliasManagement/Alias/Actions/AliasAddOperation.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,42 @@ namespace Nest
44
{
55
public class AliasAddOperation
66
{
7+
/// <summary>
8+
/// The name of the alias
9+
/// </summary>
710
[DataMember(Name ="alias")]
811
public string Alias { get; set; }
912

13+
/// <summary>
14+
/// Filter query used to limit the index alias.
15+
/// If specified, the index alias only applies to documents returned by the filter.
16+
/// </summary>
1017
[DataMember(Name ="filter")]
1118
public QueryContainer Filter { get; set; }
1219

20+
/// <summary>
21+
/// The index to which to add the alias
22+
/// </summary>
1323
[DataMember(Name ="index")]
1424
public IndexName Index { get; set; }
1525

26+
/// <inheritdoc cref="IAlias.IndexRouting"/>
1627
[DataMember(Name ="index_routing")]
1728
public string IndexRouting { get; set; }
1829

19-
/// <summary>
20-
/// If an alias points to multiple indices, Elasticsearch will reject the write operations
21-
/// unless one is explicitly marked as the write alias using this property.
22-
/// </summary>
30+
/// <inheritdoc cref="IAlias.IsWriteIndex"/>
2331
[DataMember(Name ="is_write_index")]
2432
public bool? IsWriteIndex { get; set; }
2533

34+
/// <inheritdoc cref="IAlias.IsHidden"/>
35+
[DataMember(Name ="is_hidden")]
36+
public bool? IsHidden { get; set; }
37+
38+
/// <inheritdoc cref="IAlias.Routing"/>
2639
[DataMember(Name ="routing")]
2740
public string Routing { get; set; }
2841

42+
/// <inheritdoc cref="IAlias.SearchRouting"/>
2943
[DataMember(Name ="search_routing")]
3044
public string SearchRouting { get; set; }
3145
}

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

+87
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using Elastic.Xunit.XunitPlumbing;
34
using Elasticsearch.Net;
45
using FluentAssertions;
56
using Nest;
@@ -317,4 +318,90 @@ protected override void ExpectResponse(CreateIndexResponse response)
317318
aliases[CallIsolatedValue + "-alias"].IsWriteIndex.Should().BeTrue();
318319
}
319320
}
321+
322+
323+
[SkipVersion("<7.7.0", "hidden indices and aliases introduced in 7.7.0")]
324+
public class CreateHiddenIndexApiTests
325+
: ApiIntegrationTestBase<WritableCluster, CreateIndexResponse, ICreateIndexRequest, CreateIndexDescriptor, CreateIndexRequest>
326+
{
327+
public CreateHiddenIndexApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
328+
329+
protected override bool ExpectIsValid => true;
330+
331+
protected override object ExpectJson => new
332+
{
333+
settings = new Dictionary<string, object>
334+
{
335+
{ "index.number_of_replicas", 0 },
336+
{ "index.number_of_shards", 1 },
337+
{ "index.hidden", true }
338+
},
339+
aliases = new Dictionary<string, object>
340+
{
341+
{ CallIsolatedValue + "-alias", new { is_write_index = true, is_hidden = true } }
342+
}
343+
};
344+
345+
protected override int ExpectStatusCode => 200;
346+
347+
protected override Func<CreateIndexDescriptor, ICreateIndexRequest> Fluent => d => d
348+
.Settings(s => s
349+
.NumberOfReplicas(0)
350+
.NumberOfShards(1)
351+
.Hidden()
352+
)
353+
.Aliases(a => a
354+
.Alias(CallIsolatedValue + "-alias", aa => aa
355+
.IsWriteIndex()
356+
.IsHidden()
357+
)
358+
);
359+
360+
protected override HttpMethod HttpMethod => HttpMethod.PUT;
361+
362+
protected override CreateIndexRequest Initializer => new CreateIndexRequest(CallIsolatedValue)
363+
{
364+
Settings = new Nest.IndexSettings
365+
{
366+
NumberOfReplicas = 0,
367+
NumberOfShards = 1,
368+
Hidden = true
369+
},
370+
Aliases = new Aliases
371+
{
372+
{ CallIsolatedValue + "-alias", new Alias { IsWriteIndex = true, IsHidden = true} }
373+
}
374+
};
375+
376+
protected override string UrlPath => $"/{CallIsolatedValue}";
377+
378+
protected override LazyResponses ClientUsage() => Calls(
379+
(client, f) => client.Indices.Create(CallIsolatedValue, f),
380+
(client, f) => client.Indices.CreateAsync(CallIsolatedValue, f),
381+
(client, r) => client.Indices.Create(r),
382+
(client, r) => client.Indices.CreateAsync(r)
383+
);
384+
385+
protected override CreateIndexDescriptor NewDescriptor() => new CreateIndexDescriptor(CallIsolatedValue);
386+
387+
protected override void ExpectResponse(CreateIndexResponse response)
388+
{
389+
response.ShouldBeValid();
390+
response.Acknowledged.Should().BeTrue();
391+
response.ShardsAcknowledged.Should().BeTrue();
392+
393+
var indexResponse = Client.Indices.Get(CallIsolatedValue);
394+
395+
indexResponse.ShouldBeValid();
396+
indexResponse.Indices.Should().NotBeEmpty().And.ContainKey(CallIsolatedValue);
397+
var index = indexResponse.Indices[CallIsolatedValue];
398+
399+
index.Settings.Hidden.Should().BeTrue();
400+
401+
var aliases = indexResponse.Indices[CallIsolatedValue].Aliases;
402+
aliases.Count.Should().Be(1);
403+
aliases[CallIsolatedValue + "-alias"].IsWriteIndex.Should().BeTrue();
404+
aliases[CallIsolatedValue + "-alias"].IsHidden.Should().BeTrue();
405+
}
406+
}
320407
}

0 commit comments

Comments
 (0)