| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"net/url" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
// -- Actions -- |
| 15 |
|
| 16 |
// AliasAction is an action to apply to an alias, e.g. "add" or "remove". |
| 17 |
type AliasAction interface { |
| 18 |
Source() (interface{}, error) |
| 19 |
} |
| 20 |
|
| 21 |
// AliasAddAction is an action to add to an alias. |
| 22 |
type AliasAddAction struct { |
| 23 |
index []string // index name(s) |
| 24 |
alias string // alias name |
| 25 |
filter Query |
| 26 |
routing string |
| 27 |
searchRouting string |
| 28 |
indexRouting string |
| 29 |
} |
| 30 |
|
| 31 |
// NewAliasAddAction returns an action to add an alias. |
| 32 |
func NewAliasAddAction(alias string) *AliasAddAction { |
| 33 |
return &AliasAddAction{ |
| 34 |
alias: alias, |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// Index associates one or more indices to the alias. |
| 39 |
func (a *AliasAddAction) Index(index ...string) *AliasAddAction { |
| 40 |
a.index = append(a.index, index...) |
| 41 |
return a |
| 42 |
} |
| 43 |
|
| 44 |
func (a *AliasAddAction) removeBlankIndexNames() { |
| 45 |
var indices []string |
| 46 |
for _, index := range a.index { |
| 47 |
if len(index) > 0 { |
| 48 |
indices = append(indices, index) |
| 49 |
} |
| 50 |
} |
| 51 |
a.index = indices |
| 52 |
} |
| 53 |
|
| 54 |
// Filter associates a filter to the alias. |
| 55 |
func (a *AliasAddAction) Filter(filter Query) *AliasAddAction { |
| 56 |
a.filter = filter |
| 57 |
return a |
| 58 |
} |
| 59 |
|
| 60 |
// Routing associates a routing value to the alias. |
| 61 |
// This basically sets index and search routing to the same value. |
| 62 |
func (a *AliasAddAction) Routing(routing string) *AliasAddAction { |
| 63 |
a.routing = routing |
| 64 |
return a |
| 65 |
} |
| 66 |
|
| 67 |
// IndexRouting associates an index routing value to the alias. |
| 68 |
func (a *AliasAddAction) IndexRouting(routing string) *AliasAddAction { |
| 69 |
a.indexRouting = routing |
| 70 |
return a |
| 71 |
} |
| 72 |
|
| 73 |
// SearchRouting associates a search routing value to the alias. |
| 74 |
func (a *AliasAddAction) SearchRouting(routing ...string) *AliasAddAction { |
| 75 |
a.searchRouting = strings.Join(routing, ",") |
| 76 |
return a |
| 77 |
} |
| 78 |
|
| 79 |
// Validate checks if the operation is valid. |
| 80 |
func (a *AliasAddAction) Validate() error { |
| 81 |
var invalid []string |
| 82 |
if len(a.alias) == 0 { |
| 83 |
invalid = append(invalid, "Alias") |
| 84 |
} |
| 85 |
if len(a.index) == 0 { |
| 86 |
invalid = append(invalid, "Index") |
| 87 |
} |
| 88 |
if len(invalid) > 0 { |
| 89 |
return fmt.Errorf("missing required fields: %v", invalid) |
| 90 |
} |
| 91 |
return nil |
| 92 |
} |
| 93 |
|
| 94 |
// Source returns the JSON-serializable data. |
| 95 |
func (a *AliasAddAction) Source() (interface{}, error) { |
| 96 |
a.removeBlankIndexNames() |
| 97 |
if err := a.Validate(); err != nil { |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
src := make(map[string]interface{}) |
| 101 |
act := make(map[string]interface{}) |
| 102 |
src["add"] = act |
| 103 |
act["alias"] = a.alias |
| 104 |
switch len(a.index) { |
| 105 |
case 1: |
| 106 |
act["index"] = a.index[0] |
| 107 |
default: |
| 108 |
act["indices"] = a.index |
| 109 |
} |
| 110 |
if a.filter != nil { |
| 111 |
f, err := a.filter.Source() |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
act["filter"] = f |
| 116 |
} |
| 117 |
if len(a.routing) > 0 { |
| 118 |
act["routing"] = a.routing |
| 119 |
} |
| 120 |
if len(a.indexRouting) > 0 { |
| 121 |
act["index_routing"] = a.indexRouting |
| 122 |
} |
| 123 |
if len(a.searchRouting) > 0 { |
| 124 |
act["search_routing"] = a.searchRouting |
| 125 |
} |
| 126 |
return src, nil |
| 127 |
} |
| 128 |
|
| 129 |
// AliasRemoveAction is an action to remove an alias. |
| 130 |
type AliasRemoveAction struct { |
| 131 |
index []string // index name(s) |
| 132 |
alias string // alias name |
| 133 |
} |
| 134 |
|
| 135 |
// NewAliasRemoveAction returns an action to remove an alias. |
| 136 |
func NewAliasRemoveAction(alias string) *AliasRemoveAction { |
| 137 |
return &AliasRemoveAction{ |
| 138 |
alias: alias, |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// Index associates one or more indices to the alias. |
| 143 |
func (a *AliasRemoveAction) Index(index ...string) *AliasRemoveAction { |
| 144 |
a.index = append(a.index, index...) |
| 145 |
return a |
| 146 |
} |
| 147 |
|
| 148 |
func (a *AliasRemoveAction) removeBlankIndexNames() { |
| 149 |
var indices []string |
| 150 |
for _, index := range a.index { |
| 151 |
if len(index) > 0 { |
| 152 |
indices = append(indices, index) |
| 153 |
} |
| 154 |
} |
| 155 |
a.index = indices |
| 156 |
} |
| 157 |
|
| 158 |
// Validate checks if the operation is valid. |
| 159 |
func (a *AliasRemoveAction) Validate() error { |
| 160 |
var invalid []string |
| 161 |
if len(a.alias) == 0 { |
| 162 |
invalid = append(invalid, "Alias") |
| 163 |
} |
| 164 |
if len(a.index) == 0 { |
| 165 |
invalid = append(invalid, "Index") |
| 166 |
} |
| 167 |
if len(invalid) > 0 { |
| 168 |
return fmt.Errorf("missing required fields: %v", invalid) |
| 169 |
} |
| 170 |
return nil |
| 171 |
} |
| 172 |
|
| 173 |
// Source returns the JSON-serializable data. |
| 174 |
func (a *AliasRemoveAction) Source() (interface{}, error) { |
| 175 |
a.removeBlankIndexNames() |
| 176 |
if err := a.Validate(); err != nil { |
| 177 |
return nil, err |
| 178 |
} |
| 179 |
src := make(map[string]interface{}) |
| 180 |
act := make(map[string]interface{}) |
| 181 |
src["remove"] = act |
| 182 |
act["alias"] = a.alias |
| 183 |
switch len(a.index) { |
| 184 |
case 1: |
| 185 |
act["index"] = a.index[0] |
| 186 |
default: |
| 187 |
act["indices"] = a.index |
| 188 |
} |
| 189 |
return src, nil |
| 190 |
} |
| 191 |
|
| 192 |
// -- Service -- |
| 193 |
|
| 194 |
// AliasService enables users to add or remove an alias. |
| 195 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-aliases.html |
| 196 |
// for details. |
| 197 |
type AliasService struct { |
| 198 |
client *Client |
| 199 |
actions []AliasAction |
| 200 |
pretty bool |
| 201 |
} |
| 202 |
|
| 203 |
// NewAliasService implements a service to manage aliases. |
| 204 |
func NewAliasService(client *Client) *AliasService { |
| 205 |
builder := &AliasService{ |
| 206 |
client: client, |
| 207 |
} |
| 208 |
return builder |
| 209 |
} |
| 210 |
|
| 211 |
// Pretty asks Elasticsearch to indent the HTTP response. |
| 212 |
func (s *AliasService) Pretty(pretty bool) *AliasService { |
| 213 |
s.pretty = pretty |
| 214 |
return s |
| 215 |
} |
| 216 |
|
| 217 |
// Add adds an alias to an index. |
| 218 |
func (s *AliasService) Add(indexName string, aliasName string) *AliasService { |
| 219 |
action := NewAliasAddAction(aliasName).Index(indexName) |
| 220 |
s.actions = append(s.actions, action) |
| 221 |
return s |
| 222 |
} |
| 223 |
|
| 224 |
// Add adds an alias to an index and associates a filter to the alias. |
| 225 |
func (s *AliasService) AddWithFilter(indexName string, aliasName string, filter Query) *AliasService { |
| 226 |
action := NewAliasAddAction(aliasName).Index(indexName).Filter(filter) |
| 227 |
s.actions = append(s.actions, action) |
| 228 |
return s |
| 229 |
} |
| 230 |
|
| 231 |
// Remove removes an alias. |
| 232 |
func (s *AliasService) Remove(indexName string, aliasName string) *AliasService { |
| 233 |
action := NewAliasRemoveAction(aliasName).Index(indexName) |
| 234 |
s.actions = append(s.actions, action) |
| 235 |
return s |
| 236 |
} |
| 237 |
|
| 238 |
// Action accepts one or more AliasAction instances which can be |
| 239 |
// of type AliasAddAction or AliasRemoveAction. |
| 240 |
func (s *AliasService) Action(action ...AliasAction) *AliasService { |
| 241 |
s.actions = append(s.actions, action...) |
| 242 |
return s |
| 243 |
} |
| 244 |
|
| 245 |
// buildURL builds the URL for the operation. |
| 246 |
func (s *AliasService) buildURL() (string, url.Values, error) { |
| 247 |
path := "/_aliases" |
| 248 |
|
| 249 |
// Add query string parameters |
| 250 |
params := url.Values{} |
| 251 |
if s.pretty { |
| 252 |
params.Set("pretty", fmt.Sprintf("%v", s.pretty)) |
| 253 |
} |
| 254 |
return path, params, nil |
| 255 |
} |
| 256 |
|
| 257 |
// Do executes the command. |
| 258 |
func (s *AliasService) Do(ctx context.Context) (*AliasResult, error) { |
| 259 |
path, params, err := s.buildURL() |
| 260 |
if err != nil { |
| 261 |
return nil, err |
| 262 |
} |
| 263 |
|
| 264 |
// Body with actions |
| 265 |
body := make(map[string]interface{}) |
| 266 |
var actions []interface{} |
| 267 |
for _, action := range s.actions { |
| 268 |
src, err := action.Source() |
| 269 |
if err != nil { |
| 270 |
return nil, err |
| 271 |
} |
| 272 |
actions = append(actions, src) |
| 273 |
} |
| 274 |
body["actions"] = actions |
| 275 |
|
| 276 |
// Get response |
| 277 |
res, err := s.client.PerformRequest(ctx, "POST", path, params, body) |
| 278 |
if err != nil { |
| 279 |
return nil, err |
| 280 |
} |
| 281 |
|
| 282 |
// Return results |
| 283 |
ret := new(AliasResult) |
| 284 |
if err := s.client.decoder.Decode(res.Body, ret); err != nil { |
| 285 |
return nil, err |
| 286 |
} |
| 287 |
return ret, nil |
| 288 |
} |
| 289 |
|
| 290 |
// -- Result of an alias request. |
| 291 |
|
| 292 |
// AliasResult is the outcome of calling Do on AliasService. |
| 293 |
type AliasResult struct { |
| 294 |
Acknowledged bool `json:"acknowledged"` |
| 295 |
} |