forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias.go
137 lines (114 loc) · 2.87 KB
/
alias.go
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
// Copyright 2012-2014 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 (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
type AliasService struct {
client *Client
actions []aliasAction
pretty bool
debug bool
}
type aliasAction struct {
// "add" or "remove"
Type string
// Index name
Index string
// Alias name
Alias string
// Filter
Filter *Filter
}
func NewAliasService(client *Client) *AliasService {
builder := &AliasService{
client: client,
actions: make([]aliasAction, 0),
}
return builder
}
func (s *AliasService) Pretty(pretty bool) *AliasService {
s.pretty = pretty
return s
}
func (s *AliasService) Debug(debug bool) *AliasService {
s.debug = debug
return s
}
func (s *AliasService) Add(indexName string, aliasName string) *AliasService {
action := aliasAction{Type: "add", Index: indexName, Alias: aliasName}
s.actions = append(s.actions, action)
return s
}
func (s *AliasService) AddWithFilter(indexName string, aliasName string, filter *Filter) *AliasService {
action := aliasAction{Type: "add", Index: indexName, Alias: aliasName, Filter: filter}
s.actions = append(s.actions, action)
return s
}
func (s *AliasService) Remove(indexName string, aliasName string) *AliasService {
action := aliasAction{Type: "remove", Index: indexName, Alias: aliasName}
s.actions = append(s.actions, action)
return s
}
func (s *AliasService) Do() (*AliasResult, error) {
// Build url
urls := "/_aliases"
// Set up a new request
req, err := s.client.NewRequest("POST", urls)
if err != nil {
return nil, err
}
// Parameters
params := make(url.Values)
if s.pretty {
params.Set("pretty", fmt.Sprintf("%v", s.pretty))
}
if len(params) > 0 {
urls += "?" + params.Encode()
}
// Actions
body := make(map[string]interface{})
actionsJson := make([]interface{}, 0)
for _, action := range s.actions {
actionJson := make(map[string]interface{})
detailsJson := make(map[string]interface{})
detailsJson["index"] = action.Index
detailsJson["alias"] = action.Alias
if action.Filter != nil {
detailsJson["filter"] = (*action.Filter).Source()
}
actionJson[action.Type] = detailsJson
actionsJson = append(actionsJson, actionJson)
}
body["actions"] = actionsJson
// Set body
req.SetBodyJson(body)
if s.debug {
s.client.dumpRequest((*http.Request)(req))
}
// Get response
res, err := s.client.c.Do((*http.Request)(req))
if err != nil {
return nil, err
}
if err := checkResponse(res); err != nil {
return nil, err
}
defer res.Body.Close()
if s.debug {
s.client.dumpResponse(res)
}
ret := new(AliasResult)
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
// -- Result of an alias request.
type AliasResult struct {
Acknowledged bool `json:"acknowledged"`
}