forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElasticClient-Aliases.cs
209 lines (200 loc) · 7.44 KB
/
ElasticClient-Aliases.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace Nest
{
public partial class ElasticClient
{
private readonly string _aliasBody = @"{{""actions"" : [{0}] }}";
private string _createCommand(string command, AliasParams aliasParam)
{
var cmd = @"{{ ""{0}"" : {{
index: ""{1}"",
alias: ""{2}""".F(command, aliasParam.Index, aliasParam.Alias);
if (!aliasParam.Filter.IsNullOrEmpty())
cmd += @", ""filter"": {0} ".F(aliasParam.Filter);
if (!aliasParam.Routing.IsNullOrEmpty())
cmd += @", ""routing"": ""{0}"" ".F(aliasParam.Routing);
else
{
if (!aliasParam.IndexRouting.IsNullOrEmpty())
cmd += @", ""index_routing"": ""{0}"" ".F(aliasParam.IndexRouting);
if (!aliasParam.SearchRouting.IsNullOrEmpty())
cmd += @", ""search_routing"": ""{0}"" ".F(aliasParam.SearchRouting);
}
cmd += "} }";
return cmd;
}
/// <summary>
/// Get all the indices pointing to an alias
/// </summary>
public IEnumerable<string> GetIndicesPointingToAlias(string alias)
{
var path = this.PathResolver.CreateIndexPath(alias, "/_aliases");
var status = this.Connection.GetSync(path);
if (!status.Success)
{
return Enumerable.Empty<string>();
}
var r = this.Deserialize<Dictionary<string, object>>(status.Result);
return r == null ? Enumerable.Empty<string>() : r.Keys;
}
/// <summary>
/// Repoint an alias from a set of old indices to a set of new indices in one operation
/// </summary>
public IIndicesOperationResponse Swap(string alias, IEnumerable<string> oldIndices, IEnumerable<string> newIndices)
{
var commands = new List<string>();
foreach (var i in oldIndices)
commands.Add(_createCommand("remove", new AliasParams { Index = i, Alias = alias }));
foreach (var i in newIndices)
commands.Add(_createCommand("add", new AliasParams { Index = i, Alias = alias }));
return this._Alias(string.Join(", ", commands));
}
/// <summary>
/// Repoint an alias from a set of old indices to a set of new indices in one operation
/// </summary>
public IIndicesOperationResponse Swap(AliasParams aliasParam, IEnumerable<string> oldIndices, IEnumerable<string> newIndices)
{
var commands = new List<string>();
foreach (var i in oldIndices)
commands.Add(_createCommand("remove", new AliasParams { Index = i, Alias = aliasParam.Alias }));
foreach (var i in newIndices)
{
aliasParam.Index = i;
commands.Add(_createCommand("add", aliasParam));
}
return this._Alias(string.Join(", ", commands));
}
/// <summary>
/// Add an alias to the default index
/// </summary>
public IIndicesOperationResponse Alias(string alias)
{
var index = this.Settings.DefaultIndex;
var q = _createCommand("add", new AliasParams { Index = index, Alias = alias });
return this._Alias(q);
}
/// <summary>
/// Add an alias to the specified index
/// </summary>
public IIndicesOperationResponse Alias(string index, string alias)
{
var q = _createCommand("add", new AliasParams { Index = index, Alias = alias });
return this._Alias(q);
}
/// <summary>
/// Add multiple aliases to the specified index
/// </summary>
public IIndicesOperationResponse Alias(string index, IEnumerable<string> aliases)
{
aliases.Select(a => _createCommand("add", new AliasParams { Index = index, Alias = a }));
var q = string.Join(",", aliases);
return this._Alias(q);
}
/// <summary>
/// Add multiple aliases to the default index
/// </summary>
public IIndicesOperationResponse Alias(IEnumerable<string> aliases)
{
var index = this.Settings.DefaultIndex;
aliases.Select(a => _createCommand("add", new AliasParams { Index = index, Alias = a }));
var q = string.Join(",", aliases);
return this._Alias(q);
}
/// <summary>
/// Remove an alias for the default index
/// </summary>
public IIndicesOperationResponse RemoveAlias(string alias)
{
var index = this.Settings.DefaultIndex;
var q = _createCommand("remove", new AliasParams { Index = index, Alias = alias });
return this._Alias(q);
}
/// <summary>
/// Remove an alias for the specified index
/// </summary>
public IIndicesOperationResponse RemoveAlias(string index, string alias)
{
var q = _createCommand("remove", new AliasParams { Index = index, Alias = alias });
return this._Alias(q);
}
/// <summary>
/// Remove multiple alias for the default index
/// </summary>
public IIndicesOperationResponse RemoveAlias(IEnumerable<string> aliases)
{
var index = this.Settings.DefaultIndex;
aliases.Select(a => _createCommand("remove", new AliasParams { Index = index, Alias = a }));
var q = string.Join(",", aliases);
return this._Alias(q);
}
/// <summary>
/// Remove multiple alias for the specified index
/// </summary>
public IIndicesOperationResponse RemoveAlias(string index, IEnumerable<string> aliases)
{
aliases.Select(a => _createCommand("remove", new AliasParams { Index = index, Alias = a }));
var q = string.Join(",", aliases);
return this._Alias(q);
}
/// <summary>
/// Associate multiple indices with one alias
/// </summary>
public IIndicesOperationResponse Alias(IEnumerable<string> indices, string alias)
{
indices.Select(i => _createCommand("add", new AliasParams { Index = i, Alias = alias }));
var q = string.Join(",", indices);
return this._Alias(q);
}
/// <summary>
/// Rename an old alias for index to a new alias in one operation
/// </summary>
public IIndicesOperationResponse Rename(string index, string oldAlias, string newAlias)
{
var r = _createCommand("remove", new AliasParams { Index = index, Alias = oldAlias });
var a = _createCommand("add", new AliasParams { Index = index, Alias = newAlias });
return this._Alias(r + ", " + a);
}
/// <summary>
/// Freeform alias overload for complete control of all the aspects (does an add operation)
/// </summary>
public IIndicesOperationResponse Alias(AliasParams aliasParams)
{
return this._Alias(_createCommand("add", aliasParams));
}
/// <summary>
/// Freeform multi alias overload for complete control of all the aspects (does multiple add operations)
/// </summary>
public IIndicesOperationResponse Alias(IEnumerable<AliasParams> aliases)
{
var cmds = aliases.Select(a => _aliasBody.F(_createCommand("add", a)));
var q = string.Join(",", aliases);
return this._Alias(q);
}
/// <summary>
/// Freeform remove alias overload for complete control of all the aspects
/// </summary>
public IIndicesOperationResponse RemoveAlias(AliasParams aliasParams)
{
return this._Alias(_createCommand("remove", aliasParams));
}
/// <summary>
/// Freeform remove multi alias overload for complete control of all the aspects
/// </summary>
public IIndicesOperationResponse RemoveAliases(IEnumerable<AliasParams> aliases)
{
var cmds = aliases.Select(a => _aliasBody.F(_createCommand("remove", a)));
var q = string.Join(",", aliases);
return this._Alias(q);
}
private IndicesOperationResponse _Alias(string query)
{
var path = "/_aliases";
query = _aliasBody.F(query);
var status = this.Connection.PostSync(path, query);
var r = this.ToParsedResponse<IndicesOperationResponse>(status);
return r;
}
}
}