Skip to content

Commit 13852f9

Browse files
committed
Merge pull request #1372 from robertlyson/RestoreIndexSettings
Support for supplying index settings on restore #1362
2 parents 65bed84 + 874ed5c commit 13852f9

File tree

6 files changed

+214
-41
lines changed

6 files changed

+214
-41
lines changed

src/Nest/DSL/RestoreDescriptor.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public interface IRestoreRequest : IRepositorySnapshotPath<RestoreRequestParamet
1919
string RenamePattern { get; set; }
2020
[JsonProperty("rename_replacement")]
2121
string RenameReplacement { get; set; }
22-
22+
[JsonProperty("index_settings")]
23+
IUpdateSettingsRequest IndexSettings { get; set; }
24+
[JsonProperty("ignore_index_settings")]
25+
List<string> IgnoreIndexSettings { get; set; }
2326
}
2427

2528
internal static class RestorePathInfo
@@ -43,6 +46,8 @@ public RestoreRequest(string repository, string snapshot) : base(repository, sna
4346
public string RenamePattern { get; set; }
4447

4548
public string RenameReplacement { get; set; }
49+
public IUpdateSettingsRequest IndexSettings { get; set; }
50+
public List<string> IgnoreIndexSettings { get; set; }
4651

4752
protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<RestoreRequestParameters> pathInfo)
4853
{
@@ -61,7 +66,9 @@ public partial class RestoreDescriptor : RepositorySnapshotPathDescriptor<Restor
6166
bool? IRestoreRequest.IncludeGlobalState { get; set; }
6267
string IRestoreRequest.RenamePattern { get; set; }
6368
string IRestoreRequest.RenameReplacement { get; set; }
64-
69+
IUpdateSettingsRequest IRestoreRequest.IndexSettings { get; set; }
70+
List<string> IRestoreRequest.IgnoreIndexSettings { get; set; }
71+
6572
public RestoreDescriptor Index(string index)
6673
{
6774
return this.Indices(index);
@@ -104,10 +111,30 @@ public RestoreDescriptor RenameReplacement(string renameReplacement)
104111
return this;
105112
}
106113

114+
public RestoreDescriptor IndexSettings(Func<UpdateSettingsDescriptor, UpdateSettingsDescriptor> settingsSelector)
115+
{
116+
settingsSelector.ThrowIfNull("settings");
117+
Self.IndexSettings = settingsSelector(new UpdateSettingsDescriptor());
118+
return this;
119+
}
120+
121+
public RestoreDescriptor IgnoreIndexSettings(List<string> ignoreIndexSettings)
122+
{
123+
ignoreIndexSettings.ThrowIfNull("ignoreIndexSettings");
124+
Self.IgnoreIndexSettings = ignoreIndexSettings;
125+
return this;
126+
}
127+
128+
public RestoreDescriptor IgnoreIndexSettings(params string[] ignoreIndexSettings)
129+
{
130+
ignoreIndexSettings.ThrowIfNull("ignoreIndexSettings");
131+
this.IgnoreIndexSettings(ignoreIndexSettings.ToList());
132+
return this;
133+
}
134+
107135
protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<RestoreRequestParameters> pathInfo)
108136
{
109137
RestorePathInfo.Update(pathInfo, this);
110138
}
111-
112139
}
113140
}

src/Nest/DSL/UpdateSettingsDescriptor.cs

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,154 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using Elasticsearch.Net;
54
using Newtonsoft.Json;
65

76
namespace Nest
8-
{
7+
{
8+
public class UpdatableSettings
9+
{
10+
public const string NumberOfReplicas = "index.number_of_replicas";
11+
public const string AutoExpandReplicas = "index.auto_expand_replicas";
12+
public const string BlocksReadOnly = "index.blocks.read_only";
13+
public const string BlocksRead = "index.blocks.read";
14+
public const string BlocksWrite = "index.blocks.write";
15+
public const string BlocksMetadata = "index.blocks.metadata";
16+
public const string RefreshInterval = "index.refresh_interval";
17+
public const string IndexConcurrency = "index.index_concurrency";
18+
public const string Codec = "index.codec";
19+
public const string CodecBloomLoad = "index.codec.bloom.load";
20+
public const string FailOnMergeFailure = "index.fail_on_merge_failure";
21+
public const string TranslogFlushTreshHoldOps = "index.translog.flush_threshold_ops";
22+
public const string TranslogFlushThresholdSize = "index.translog.flush_threshold_size";
23+
public const string TranslogFlushThresholdPeriod = "index.translog.flush_threshold_period";
24+
public const string TranslogDisableFlush = "index.translog.disable_flush";
25+
public const string CacheFilterMaxSize = "index.cache.filter.max_size";
26+
public const string CacheFilterExpire = "index.cache.filter.expire";
27+
public const string CacheQueryEnable = "index.cache.query.enable";
28+
public const string GatewaySnapshotInterval = "index.gateway.snapshot_interval";
29+
public const string RoutingAllocationInclude = "index.routing.allocation.include";
30+
public const string RoutingAllocationExclude = "index.routing.allocation.exclude";
31+
public const string RoutingAllocationRequire = "index.routing.allocation.require";
32+
public const string RoutingAllocationEnable = "index.routing.allocation.enable";
33+
public const string RoutingAllocationDisableAllication = "index.routing.allocation.disable_allocation";
34+
public const string RoutingAllocationDisableNewAllocation = "index.routing.allocation.disable_new_allocation";
35+
public const string RoutingAllocationDisableReplicaAllocation = "index.routing.allocation.disable_replica_allocation";
36+
public const string RoutingAllocationTotalShardsPerNode = "index.routing.allocation.total_shards_per_node";
37+
public const string RecoveryInitialShards = "index.recovery.initial_shards";
38+
public const string GcDeletes = "index.gc_deletes";
39+
public const string TtlDisablePurge = "index.ttl.disable_purge";
40+
public const string TranslogFsType = "index.translog.fs.type";
41+
public const string CompoundFormat = "index.compound_format";
42+
public const string CompoundOnFlush = "index.compound_on_flush";
43+
public const string WarmersEnabled = "index.warmer.enabled";
44+
public const string Analysis = "analysis";
45+
}
46+
947
public interface IUpdateSettingsRequest : IIndexOptionalPath<UpdateSettingsRequestParameters>
1048
{
11-
[JsonProperty("index.number_of_replicas")]
49+
[JsonProperty(UpdatableSettings.NumberOfReplicas)]
1250
int? NumberOfReplicas { get; set; }
1351

14-
[JsonProperty("index.auto_expand_replicas")]
52+
[JsonProperty(UpdatableSettings.AutoExpandReplicas)]
1553
object AutoExpandReplicas { get; set; }
1654

17-
[JsonProperty("index.blocks.read_only")]
55+
[JsonProperty(UpdatableSettings.BlocksReadOnly)]
1856
bool? BlocksReadOnly { get; set; }
1957

20-
[JsonProperty("index.blocks.read")]
58+
[JsonProperty(UpdatableSettings.BlocksRead)]
2159
bool? BlocksRead { get; set; }
2260

23-
[JsonProperty("index.blocks.write")]
61+
[JsonProperty(UpdatableSettings.BlocksWrite)]
2462
bool? BlocksWrite { get; set; }
2563

26-
[JsonProperty("index.blocks.metadata")]
64+
[JsonProperty(UpdatableSettings.BlocksMetadata)]
2765
bool? BlocksMetadata { get; set; }
2866

29-
[JsonProperty("index.refresh_interval")]
67+
[JsonProperty(UpdatableSettings.RefreshInterval)]
3068
string RefreshInterval { get; set; }
3169

32-
[JsonProperty("index.index_concurrency")]
70+
[JsonProperty(UpdatableSettings.IndexConcurrency)]
3371
int? IndexConcurrency { get; set; }
3472

35-
[JsonProperty("index.codec")]
73+
[JsonProperty(UpdatableSettings.Codec)]
3674
string Codec { get; set; }
3775

38-
[JsonProperty("index.codec.bloom.load")]
76+
[JsonProperty(UpdatableSettings.CodecBloomLoad)]
3977
bool? CodecBloomLoad { get; set; }
4078

41-
[JsonProperty("index.fail_on_merge_failure")]
79+
[JsonProperty(UpdatableSettings.FailOnMergeFailure)]
4280
bool? FailOnMergeFailure { get; set; }
4381

44-
[JsonProperty("index.translog.flush_threshold_ops")]
82+
[JsonProperty(UpdatableSettings.TranslogFlushTreshHoldOps)]
4583
string TranslogFlushTreshHoldOps { get; set; }
4684

47-
[JsonProperty("index.translog.flush_threshold_size")]
85+
[JsonProperty(UpdatableSettings.TranslogFlushThresholdSize)]
4886
string TranslogFlushThresholdSize { get; set; }
4987

50-
[JsonProperty("index.translog.flush_threshold_period")]
88+
[JsonProperty(UpdatableSettings.TranslogFlushThresholdPeriod)]
5189
string TranslogFlushThresholdPeriod { get; set; }
5290

53-
[JsonProperty("index.translog.disable_flush")]
91+
[JsonProperty(UpdatableSettings.TranslogDisableFlush)]
5492
bool? TranslogDisableFlush { get; set; }
5593

56-
[JsonProperty("index.cache.filter.max_size")]
94+
[JsonProperty(UpdatableSettings.CacheFilterMaxSize)]
5795
string CacheFilterMaxSize { get; set; }
5896

59-
[JsonProperty("index.cache.filter.expire")]
97+
[JsonProperty(UpdatableSettings.CacheFilterExpire)]
6098
string CacheFilterExpire { get; set; }
6199

62-
[JsonProperty("index.cache.query.enable")]
100+
[JsonProperty(UpdatableSettings.CacheQueryEnable)]
63101
bool? CacheQueryEnable { get; set; }
64102

65-
[JsonProperty("index.gateway.snapshot_interval")]
103+
[JsonProperty(UpdatableSettings.GatewaySnapshotInterval)]
66104
string GatewaySnapshotInterval { get; set; }
67105

68-
[JsonProperty("index.routing.allocation.include")]
106+
[JsonProperty(UpdatableSettings.RoutingAllocationInclude)]
69107
IDictionary<string, object> RoutingAllocationInclude { get; set; }
70108

71-
[JsonProperty("index.routing.allocation.exclude")]
109+
[JsonProperty(UpdatableSettings.RoutingAllocationExclude)]
72110
IDictionary<string, object> RoutingAllocationExclude { get; set; }
73111

74-
[JsonProperty("index.routing.allocation.require")]
112+
[JsonProperty(UpdatableSettings.RoutingAllocationRequire)]
75113
IDictionary<string, object> RoutingAllocationRequire { get; set; }
76114

77-
[JsonProperty("index.routing.allocation.enable")]
115+
[JsonProperty(UpdatableSettings.RoutingAllocationEnable)]
78116
RoutingAllocationEnableOption? RoutingAllocationEnable { get; set; }
79117

80-
[JsonProperty("index.routing.allocation.disable_allocation")]
118+
[JsonProperty(UpdatableSettings.RoutingAllocationDisableAllication)]
81119
bool? RoutingAllocationDisableAllication { get; set; }
82120

83-
[JsonProperty("index.routing.allocation.disable_new_allocation")]
121+
[JsonProperty(UpdatableSettings.RoutingAllocationDisableNewAllocation)]
84122
bool? RoutingAllocationDisableNewAllocation { get; set; }
85123

86-
[JsonProperty("index.routing.allocation.disable_replica_allocation")]
124+
[JsonProperty(UpdatableSettings.RoutingAllocationDisableReplicaAllocation)]
87125
bool? RoutingAllocationDisableReplicaAllocation { get; set; }
88126

89-
[JsonProperty("index.routing.allocation.total_shards_per_node")]
127+
[JsonProperty(UpdatableSettings.RoutingAllocationTotalShardsPerNode)]
90128
int? RoutingAllocationTotalShardsPerNode { get; set; }
91129

92-
[JsonProperty("index.recovery.initial_shards")]
130+
[JsonProperty(UpdatableSettings.RecoveryInitialShards)]
93131
string RecoveryInitialShards { get; set; }
94132

95-
[JsonProperty("index.gc_deletes")]
133+
[JsonProperty(UpdatableSettings.GcDeletes)]
96134
bool? GcDeletes { get; set; }
97135

98-
[JsonProperty("index.ttl.disable_purge")]
136+
[JsonProperty(UpdatableSettings.TtlDisablePurge)]
99137
bool? TtlDisablePurge { get; set; }
100138

101-
[JsonProperty("index.translog.fs.type")]
139+
[JsonProperty(UpdatableSettings.TranslogFsType)]
102140
string TranslogFsType { get; set; }
103141

104-
[JsonProperty("index.compound_format")]
142+
[JsonProperty(UpdatableSettings.CompoundFormat)]
105143
bool? CompoundFormat { get; set; }
106144

107-
[JsonProperty("index.compound_on_flush")]
145+
[JsonProperty(UpdatableSettings.CompoundOnFlush)]
108146
bool? CompoundOnFlush { get; set; }
109147

110-
[JsonProperty("index.warmer.enabled")]
148+
[JsonProperty(UpdatableSettings.WarmersEnabled)]
111149
bool? WarmersEnabled { get; set; }
112150

113-
[JsonProperty("analysis")]
151+
[JsonProperty(UpdatableSettings.Analysis)]
114152
AnalysisSettings Analysis { get; set; }
115153
}
116154

src/Tests/Nest.Tests.Integration/Core/Repository/RestoreTests.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void SnapshotRestore()
9090

9191
var indexExistsResponse = this.Client.IndexExists(f => f.Index(_restoredIndexName));
9292
indexExistsResponse.Exists.Should().BeTrue();
93-
93+
9494
var count = this.Client.Count<ElasticsearchProject>(descriptor => descriptor.Index(_restoredIndexName)).Count;
9595

9696
var indexContent = this.Client.SourceMany<ElasticsearchProject>(_indexedElements.Select(x => (long)x.Id), _restoredIndexName);
@@ -99,6 +99,43 @@ public void SnapshotRestore()
9999
indexContent.ShouldBeEquivalentTo(_indexedElements);
100100
}
101101

102+
[Test]
103+
[SkipVersion("0 - 1.4.9", "Requires index_settings and ignore_index_settings parameters which have been added in ES 1.5.0")]
104+
public void SnapshotRestore_IndexSettings()
105+
{
106+
var updateSettingsResponse = this.Client.UpdateSettings(descriptor => descriptor.BlocksWrite());
107+
updateSettingsResponse.IsValid.Should().BeTrue();
108+
109+
var snapshotResponse = this.Client.Snapshot(_repositoryName, _snapshotName, selector: f => f
110+
.Index(_indexName)
111+
.WaitForCompletion(true)
112+
.IgnoreUnavailable()
113+
.Partial());
114+
snapshotResponse.IsValid.Should().BeTrue();
115+
116+
var d = ElasticsearchConfiguration.DefaultIndex;
117+
var restoreResponse = this.Client.Restore(_repositoryName, _snapshotName, r => r
118+
.WaitForCompletion(true)
119+
.RenamePattern(d + "_(.+)")
120+
.RenameReplacement(d + "_restored_$1")
121+
.Index(_indexName)
122+
.IgnoreUnavailable(true)
123+
.IndexSettings(descriptor => descriptor
124+
.RefreshInterval("123s"))
125+
.IgnoreIndexSettings(UpdatableSettings.BlocksWrite));
126+
127+
restoreResponse.IsValid.Should().BeTrue();
128+
_restoredIndexName = _indexName.Replace(d + "_", d + "_restored_");
129+
130+
var indexExistsResponse = this.Client.IndexExists(f => f.Index(_restoredIndexName));
131+
indexExistsResponse.Exists.Should().BeTrue();
132+
133+
var indexSettingsResponse = this.Client.GetIndexSettings(descriptor => descriptor.Index(_restoredIndexName));
134+
indexSettingsResponse.IsValid.Should().BeTrue();
135+
indexSettingsResponse.IndexSettings.Settings[UpdatableSettings.RefreshInterval].Should().Be("123s");
136+
indexSettingsResponse.IndexSettings.Settings[UpdatableSettings.BlocksWrite].Should().BeNull();
137+
}
138+
102139
[Test]
103140
[SkipVersion("0 - 1.0.9", "Requires the snapshot status api which was added in ES 1.1")]
104141
public void SnapshotRestoreObservable()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"indices": [
3+
"index"
4+
],
5+
"index_settings": {
6+
"index.number_of_replicas": 0,
7+
"index.blocks.read": true
8+
},
9+
"ignore_index_settings": [
10+
"index.refresh_interval",
11+
"index.auto_expand_replicas"
12+
]
13+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Elasticsearch.Net;
9+
using FluentAssertions;
10+
using NUnit.Framework;
11+
12+
namespace Nest.Tests.Unit.Core.Repository
13+
{
14+
public class RestoreTests : BaseJsonTests
15+
{
16+
[Test]
17+
public void Restore()
18+
{
19+
var restoreResponse = _client.Restore("repository", "snapshotName",
20+
descriptor => descriptor
21+
.Index("index")
22+
.IndexSettings(settingsDescriptor => settingsDescriptor
23+
.NumberOfReplicas(0)
24+
.BlocksRead())
25+
.IgnoreIndexSettings(UpdatableSettings.RefreshInterval, UpdatableSettings.AutoExpandReplicas));
26+
27+
restoreResponse.ConnectionStatus.RequestUrl.Should().Be("http://localhost:9200/_snapshot/repository/snapshotName/_restore");
28+
this.JsonEquals(restoreResponse.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
29+
}
30+
31+
[Test]
32+
[ExpectedException(typeof(ArgumentNullException))]
33+
public void Null_IgnoreIndexSettings_ThorwException()
34+
{
35+
List<string> ignoreIndexSettings = null;
36+
_client.Restore("repository", "snapshotName",
37+
descriptor => descriptor
38+
.Index("index")
39+
.IgnoreIndexSettings(ignoreIndexSettings));
40+
}
41+
42+
[Test]
43+
[ExpectedException(typeof(ArgumentNullException))]
44+
public void Null_IndexSettings_ThorwException()
45+
{
46+
Func<UpdateSettingsDescriptor, UpdateSettingsDescriptor> settingsSelector = null;
47+
48+
var restoreResponse = _client.Restore("repository", "snapshotName",
49+
descriptor => descriptor
50+
.Index("index")
51+
.IndexSettings(settingsSelector));
52+
}
53+
}
54+
}

src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
</None>
202202
<Compile Include="Core\MultiPercolate\MultiPercolateTests.cs" />
203203
<Compile Include="Core\Map\Transform\MappingTansformTests.cs" />
204+
<Compile Include="Core\Repository\RestoreTests.cs" />
204205
<Compile Include="Core\Script\DeleteScriptRequestTests.cs" />
205206
<Compile Include="Core\Script\GetScriptRequestTests.cs" />
206207
<Compile Include="Core\Script\PutScriptRequestTests.cs" />
@@ -684,6 +685,9 @@
684685
<None Include="Core\MultiPercolate\MultiPercolateJson.json">
685686
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
686687
</None>
688+
<None Include="Core\Repository\Restore.json">
689+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
690+
</None>
687691
<None Include="Core\Script\PutScript.json">
688692
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
689693
</None>

0 commit comments

Comments
 (0)