-
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
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
)
// -- Actions --
// AliasAction is an action to apply to an alias, e.g. "add" or "remove".
type AliasAction interface {
Source() (interface{}, error)
}
// AliasAddAction is an action to add to an alias.
type AliasAddAction struct {
index []string // index name(s)
alias string // alias name
filter Query
routing string
searchRouting string
indexRouting string
isWriteIndex *bool
}
// NewAliasAddAction returns an action to add an alias.
func NewAliasAddAction(alias string) *AliasAddAction {
return &AliasAddAction{
alias: alias,
}
}
// Index associates one or more indices to the alias.
func (a *AliasAddAction) Index(index ...string) *AliasAddAction {
a.index = append(a.index, index...)
return a
}
func (a *AliasAddAction) removeBlankIndexNames() {
var indices []string
for _, index := range a.index {
if len(index) > 0 {
indices = append(indices, index)
}
}
a.index = indices
}
// Filter associates a filter to the alias.
func (a *AliasAddAction) Filter(filter Query) *AliasAddAction {
a.filter = filter
return a
}
// Routing associates a routing value to the alias.
// This basically sets index and search routing to the same value.
func (a *AliasAddAction) Routing(routing string) *AliasAddAction {
a.routing = routing
return a
}
// IndexRouting associates an index routing value to the alias.
func (a *AliasAddAction) IndexRouting(routing string) *AliasAddAction {
a.indexRouting = routing
return a
}
// SearchRouting associates a search routing value to the alias.
func (a *AliasAddAction) SearchRouting(routing ...string) *AliasAddAction {
a.searchRouting = strings.Join(routing, ",")
return a
}
// IsWriteIndex associates an is_write_index flag to the alias.
func (a *AliasAddAction) IsWriteIndex(flag bool) *AliasAddAction {
a.isWriteIndex = &flag
return a
}
// Validate checks if the operation is valid.
func (a *AliasAddAction) Validate() error {
var invalid []string
if len(a.alias) == 0 {
invalid = append(invalid, "Alias")
}
if len(a.index) == 0 {
invalid = append(invalid, "Index")
}
if len(invalid) > 0 {
return fmt.Errorf("missing required fields: %v", invalid)
}
if a.isWriteIndex != nil && len(a.index) > 1 {
return fmt.Errorf("more than 1 target index specified in operation with 'is_write_index' flag present")
}
return nil
}
// Source returns the JSON-serializable data.
func (a *AliasAddAction) Source() (interface{}, error) {
a.removeBlankIndexNames()
if err := a.Validate(); err != nil {
return nil, err
}
src := make(map[string]interface{})
act := make(map[string]interface{})
src["add"] = act
act["alias"] = a.alias
switch len(a.index) {
case 1:
act["index"] = a.index[0]
default:
act["indices"] = a.index
}
if a.filter != nil {
f, err := a.filter.Source()
if err != nil {
return nil, err
}
act["filter"] = f
}
if len(a.routing) > 0 {
act["routing"] = a.routing
}
if len(a.indexRouting) > 0 {
act["index_routing"] = a.indexRouting
}
if len(a.searchRouting) > 0 {
act["search_routing"] = a.searchRouting
}
if a.isWriteIndex != nil {
act["is_write_index"] = *a.isWriteIndex
}
return src, nil
}
// AliasRemoveAction is an action to remove an alias.
type AliasRemoveAction struct {
index []string // index name(s)
alias string // alias name
}
// NewAliasRemoveAction returns an action to remove an alias.
func NewAliasRemoveAction(alias string) *AliasRemoveAction {
return &AliasRemoveAction{
alias: alias,
}
}
// Index associates one or more indices to the alias.
func (a *AliasRemoveAction) Index(index ...string) *AliasRemoveAction {
a.index = append(a.index, index...)
return a
}
func (a *AliasRemoveAction) removeBlankIndexNames() {
var indices []string
for _, index := range a.index {
if len(index) > 0 {
indices = append(indices, index)
}
}
a.index = indices
}
// Validate checks if the operation is valid.
func (a *AliasRemoveAction) Validate() error {
var invalid []string
if len(a.alias) == 0 {
invalid = append(invalid, "Alias")
}
if len(a.index) == 0 {
invalid = append(invalid, "Index")
}
if len(invalid) > 0 {
return fmt.Errorf("missing required fields: %v", invalid)
}
return nil
}
// Source returns the JSON-serializable data.
func (a *AliasRemoveAction) Source() (interface{}, error) {
a.removeBlankIndexNames()
if err := a.Validate(); err != nil {
return nil, err
}
src := make(map[string]interface{})
act := make(map[string]interface{})
src["remove"] = act
act["alias"] = a.alias
switch len(a.index) {
case 1:
act["index"] = a.index[0]
default:
act["indices"] = a.index
}
return src, nil
}
// AliasRemoveIndexAction is an action to remove an index during an alias
// operation.
type AliasRemoveIndexAction struct {
index string // index name
}
// NewAliasRemoveIndexAction returns an action to remove an index.
func NewAliasRemoveIndexAction(index string) *AliasRemoveIndexAction {
return &AliasRemoveIndexAction{
index: index,
}
}
// Validate checks if the operation is valid.
func (a *AliasRemoveIndexAction) Validate() error {
if a.index == "" {
return fmt.Errorf("missing required field: index")
}
return nil
}
// Source returns the JSON-serializable data.
func (a *AliasRemoveIndexAction) Source() (interface{}, error) {
if err := a.Validate(); err != nil {
return nil, err
}
src := make(map[string]interface{})
act := make(map[string]interface{})
src["remove_index"] = act
act["index"] = a.index
return src, nil
}
// -- Service --
// AliasService enables users to add or remove an alias.
// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-aliases.html
// for details.
type AliasService struct {
client *Client
pretty *bool // pretty format the returned JSON response
human *bool // return human readable values for statistics
errorTrace *bool // include the stack trace of returned errors
filterPath []string // list of filters used to reduce the response
headers http.Header // custom request-level HTTP headers
actions []AliasAction
}
// NewAliasService implements a service to manage aliases.
func NewAliasService(client *Client) *AliasService {
builder := &AliasService{
client: client,
}
return builder
}
// Pretty tells Elasticsearch whether to return a formatted JSON response.
func (s *AliasService) Pretty(pretty bool) *AliasService {
s.pretty = &pretty
return s
}
// Human specifies whether human readable values should be returned in
// the JSON response, e.g. "7.5mb".
func (s *AliasService) Human(human bool) *AliasService {
s.human = &human
return s
}
// ErrorTrace specifies whether to include the stack trace of returned errors.
func (s *AliasService) ErrorTrace(errorTrace bool) *AliasService {
s.errorTrace = &errorTrace
return s
}
// FilterPath specifies a list of filters used to reduce the response.
func (s *AliasService) FilterPath(filterPath ...string) *AliasService {
s.filterPath = filterPath
return s
}
// Header adds a header to the request.
func (s *AliasService) Header(name string, value string) *AliasService {
if s.headers == nil {
s.headers = http.Header{}
}
s.headers.Add(name, value)
return s
}
// Headers specifies the headers of the request.
func (s *AliasService) Headers(headers http.Header) *AliasService {
s.headers = headers
return s
}
// Add adds an alias to an index.
func (s *AliasService) Add(indexName string, aliasName string) *AliasService {
action := NewAliasAddAction(aliasName).Index(indexName)
s.actions = append(s.actions, action)
return s
}
// Add adds an alias to an index and associates a filter to the alias.
func (s *AliasService) AddWithFilter(indexName string, aliasName string, filter Query) *AliasService {
action := NewAliasAddAction(aliasName).Index(indexName).Filter(filter)
s.actions = append(s.actions, action)
return s
}
// Remove removes an alias.
func (s *AliasService) Remove(indexName string, aliasName string) *AliasService {
action := NewAliasRemoveAction(aliasName).Index(indexName)
s.actions = append(s.actions, action)
return s
}
// Action accepts one or more AliasAction instances which can be
// of type AliasAddAction or AliasRemoveAction.
func (s *AliasService) Action(action ...AliasAction) *AliasService {
s.actions = append(s.actions, action...)
return s
}
// buildURL builds the URL for the operation.
func (s *AliasService) buildURL() (string, url.Values, error) {
path := "/_aliases"
// Add query string parameters
params := url.Values{}
if v := s.pretty; v != nil {
params.Set("pretty", fmt.Sprint(*v))
}
if v := s.human; v != nil {
params.Set("human", fmt.Sprint(*v))
}
if v := s.errorTrace; v != nil {
params.Set("error_trace", fmt.Sprint(*v))
}
if len(s.filterPath) > 0 {
params.Set("filter_path", strings.Join(s.filterPath, ","))
}
return path, params, nil
}
// Do executes the command.
func (s *AliasService) Do(ctx context.Context) (*AliasResult, error) {
path, params, err := s.buildURL()
if err != nil {
return nil, err
}
// Body with actions
body := make(map[string]interface{})
var actions []interface{}
for _, action := range s.actions {
src, err := action.Source()
if err != nil {
return nil, err
}
actions = append(actions, src)
}
body["actions"] = actions
// Get response
res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
Method: "POST",
Path: path,
Params: params,
Body: body,
Headers: s.headers,
})
if err != nil {
return nil, err
}
// Return results
ret := new(AliasResult)
if err := s.client.decoder.Decode(res.Body, ret); err != nil {
return nil, err
}
return ret, nil
}
// -- Result of an alias request.
// AliasResult is the outcome of calling Do on AliasService.
type AliasResult struct {
Acknowledged bool `json:"acknowledged"`
ShardsAcknowledged bool `json:"shards_acknowledged"`
Index string `json:"index,omitempty"`
}