Skip to content

Commit c10e083

Browse files
committed
1 parent 50b1ada commit c10e083

8 files changed

+795
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ See the [wiki](https://github.com/olivere/elastic/wiki) for more details.
275275
- [x] Get Settings
276276
- [ ] Analyze
277277
- [x] Index Templates
278-
- [ ] Warmers
278+
- [x] Warmers
279279
- [x] Indices Stats
280280
- [ ] Indices Segments
281281
- [ ] Indices Recovery

client.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
const (
2424
// Version is the current version of Elastic.
25-
Version = "3.0.10"
25+
Version = "3.0.11"
2626

2727
// DefaultUrl is the default endpoint of Elasticsearch on the local machine.
2828
// It is used e.g. when initializing a new Client without a specific URL.
@@ -1294,6 +1294,21 @@ func (c *Client) PutMapping() *PutMappingService {
12941294
return NewPutMappingService(c)
12951295
}
12961296

1297+
// GetWarmer gets one or more warmers by name.
1298+
func (c *Client) GetWarmer() *IndicesGetWarmerService {
1299+
return NewIndicesGetWarmerService(c)
1300+
}
1301+
1302+
// PutWarmer registers a warmer.
1303+
func (c *Client) PutWarmer() *IndicesPutWarmerService {
1304+
return NewIndicesPutWarmerService(c)
1305+
}
1306+
1307+
// DeleteWarmer deletes one or more warmers.
1308+
func (c *Client) DeleteWarmer() *IndicesDeleteWarmerService {
1309+
return NewIndicesDeleteWarmerService(c)
1310+
}
1311+
12971312
// -- cat APIs --
12981313

12991314
// TODO cat aliases

indices_delete_warmer.go

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2012-2015 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+
"encoding/json"
9+
"fmt"
10+
"net/url"
11+
"strings"
12+
13+
"gopkg.in/olivere/elastic.v3/uritemplates"
14+
)
15+
16+
// IndicesDeleteWarmerService allows to delete a warmer.
17+
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-warmers.html.
18+
type IndicesDeleteWarmerService struct {
19+
client *Client
20+
pretty bool
21+
index []string
22+
name []string
23+
masterTimeout string
24+
}
25+
26+
// NewIndicesDeleteWarmerService creates a new IndicesDeleteWarmerService.
27+
func NewIndicesDeleteWarmerService(client *Client) *IndicesDeleteWarmerService {
28+
return &IndicesDeleteWarmerService{
29+
client: client,
30+
index: make([]string, 0),
31+
name: make([]string, 0),
32+
}
33+
}
34+
35+
// Index is a list of index names the mapping should be added to
36+
// (supports wildcards); use `_all` or omit to add the mapping on all indices.
37+
func (s *IndicesDeleteWarmerService) Index(indices ...string) *IndicesDeleteWarmerService {
38+
s.index = append(s.index, indices...)
39+
return s
40+
}
41+
42+
// Name is a list of warmer names to delete (supports wildcards);
43+
// use `_all` to delete all warmers in the specified indices.
44+
func (s *IndicesDeleteWarmerService) Name(name ...string) *IndicesDeleteWarmerService {
45+
s.name = append(s.name, name...)
46+
return s
47+
}
48+
49+
// MasterTimeout specifies the timeout for connection to master.
50+
func (s *IndicesDeleteWarmerService) MasterTimeout(masterTimeout string) *IndicesDeleteWarmerService {
51+
s.masterTimeout = masterTimeout
52+
return s
53+
}
54+
55+
// Pretty indicates that the JSON response be indented and human readable.
56+
func (s *IndicesDeleteWarmerService) Pretty(pretty bool) *IndicesDeleteWarmerService {
57+
s.pretty = pretty
58+
return s
59+
}
60+
61+
// buildURL builds the URL for the operation.
62+
func (s *IndicesDeleteWarmerService) buildURL() (string, url.Values, error) {
63+
// Build URL
64+
path, err := uritemplates.Expand("/{index}/_warmer/{name}", map[string]string{
65+
"index": strings.Join(s.index, ","),
66+
"name": strings.Join(s.name, ","),
67+
})
68+
if err != nil {
69+
return "", url.Values{}, err
70+
}
71+
72+
// Add query string parameters
73+
params := url.Values{}
74+
if s.pretty {
75+
params.Set("pretty", "1")
76+
}
77+
if s.masterTimeout != "" {
78+
params.Set("master_timeout", s.masterTimeout)
79+
}
80+
if len(s.name) > 0 {
81+
params.Set("name", strings.Join(s.name, ","))
82+
}
83+
return path, params, nil
84+
}
85+
86+
// Validate checks if the operation is valid.
87+
func (s *IndicesDeleteWarmerService) Validate() error {
88+
var invalid []string
89+
if len(s.index) == 0 {
90+
invalid = append(invalid, "Index")
91+
}
92+
if len(s.name) == 0 {
93+
invalid = append(invalid, "Name")
94+
}
95+
if len(invalid) > 0 {
96+
return fmt.Errorf("missing required fields: %v", invalid)
97+
}
98+
return nil
99+
}
100+
101+
// Do executes the operation.
102+
func (s *IndicesDeleteWarmerService) Do() (*DeleteWarmerResponse, error) {
103+
// Check pre-conditions
104+
if err := s.Validate(); err != nil {
105+
return nil, err
106+
}
107+
108+
// Get URL for request
109+
path, params, err := s.buildURL()
110+
if err != nil {
111+
return nil, err
112+
}
113+
114+
// Get HTTP response
115+
res, err := s.client.PerformRequest("DELETE", path, params, nil)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
// Return operation response
121+
ret := new(DeleteWarmerResponse)
122+
if err := json.Unmarshal(res.Body, ret); err != nil {
123+
return nil, err
124+
}
125+
return ret, nil
126+
}
127+
128+
// DeleteWarmerResponse is the response of IndicesDeleteWarmerService.Do.
129+
type DeleteWarmerResponse struct {
130+
Acknowledged bool `json:"acknowledged"`
131+
}

indices_delete_warmer_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2012-2015 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 "testing"
8+
9+
func TestDeleteWarmerBuildURL(t *testing.T) {
10+
client := setupTestClientAndCreateIndex(t)
11+
12+
tests := []struct {
13+
Indices []string
14+
Names []string
15+
Expected string
16+
}{
17+
{
18+
[]string{"test"},
19+
[]string{"warmer_1"},
20+
"/test/_warmer/warmer_1",
21+
},
22+
{
23+
[]string{"*"},
24+
[]string{"warmer_1"},
25+
"/%2A/_warmer/warmer_1",
26+
},
27+
{
28+
[]string{"_all"},
29+
[]string{"warmer_1"},
30+
"/_all/_warmer/warmer_1",
31+
},
32+
{
33+
[]string{"index-1", "index-2"},
34+
[]string{"warmer_1", "warmer_2"},
35+
"/index-1%2Cindex-2/_warmer/warmer_1%2Cwarmer_2",
36+
},
37+
}
38+
39+
for _, test := range tests {
40+
path, _, err := client.DeleteWarmer().Index(test.Indices...).Name(test.Names...).buildURL()
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
if path != test.Expected {
45+
t.Errorf("expected %q; got: %q", test.Expected, path)
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)