Skip to content

Commit 1e4b412

Browse files
authored
Add new index template APIs (#1415)
This commit implements the new Index Template APIs as described in https://www.elastic.co/guide/en/elasticsearch/reference/7.9/index-templates.html. They replace the legacy/v1 APIs and were implemented in 7.8. In general, use `client.IndexXXXIndexTemplate(...)` instead of `client.IndexXXXTemplate(...)` (with `XXX` being `Get`, `Put`, or `Delete`) to use the newer versions. Notice that this commit only implements the new Index Template APIs, not the Component Template APIs. Close #1393
1 parent 5e27e42 commit 1e4b412

9 files changed

+792
-17
lines changed

client.go

+60-8
Original file line numberDiff line numberDiff line change
@@ -1742,30 +1742,82 @@ func (c *Client) Aliases() *AliasesService {
17421742
return NewAliasesService(c)
17431743
}
17441744

1745-
// IndexGetTemplate gets an index template.
1746-
// Use XXXTemplate funcs to manage search templates.
1745+
// -- Legacy templates --
1746+
1747+
// IndexGetTemplate gets an index template (v1/legacy version before 7.8).
1748+
//
1749+
// This service implements the legacy version of index templates as described
1750+
// in https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-templates-v1.html.
1751+
//
1752+
// See e.g. IndexPutIndexTemplate and IndexPutComponentTemplate for the new version(s).
17471753
func (c *Client) IndexGetTemplate(names ...string) *IndicesGetTemplateService {
17481754
return NewIndicesGetTemplateService(c).Name(names...)
17491755
}
17501756

1751-
// IndexTemplateExists gets check if an index template exists.
1752-
// Use XXXTemplate funcs to manage search templates.
1757+
// IndexTemplateExists gets check if an index template exists (v1/legacy version before 7.8).
1758+
//
1759+
// This service implements the legacy version of index templates as described
1760+
// in https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-templates-v1.html.
1761+
//
1762+
// See e.g. IndexPutIndexTemplate and IndexPutComponentTemplate for the new version(s).
17531763
func (c *Client) IndexTemplateExists(name string) *IndicesExistsTemplateService {
17541764
return NewIndicesExistsTemplateService(c).Name(name)
17551765
}
17561766

1757-
// IndexPutTemplate creates or updates an index template.
1758-
// Use XXXTemplate funcs to manage search templates.
1767+
// IndexPutTemplate creates or updates an index template (v1/legacy version before 7.8).
1768+
//
1769+
// This service implements the legacy version of index templates as described
1770+
// in https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-templates-v1.html.
1771+
//
1772+
// See e.g. IndexPutIndexTemplate and IndexPutComponentTemplate for the new version(s).
17591773
func (c *Client) IndexPutTemplate(name string) *IndicesPutTemplateService {
17601774
return NewIndicesPutTemplateService(c).Name(name)
17611775
}
17621776

1763-
// IndexDeleteTemplate deletes an index template.
1764-
// Use XXXTemplate funcs to manage search templates.
1777+
// IndexDeleteTemplate deletes an index template (v1/legacy version before 7.8).
1778+
//
1779+
// This service implements the legacy version of index templates as described
1780+
// in https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-templates-v1.html.
1781+
//
1782+
// See e.g. IndexPutIndexTemplate and IndexPutComponentTemplate for the new version(s).
17651783
func (c *Client) IndexDeleteTemplate(name string) *IndicesDeleteTemplateService {
17661784
return NewIndicesDeleteTemplateService(c).Name(name)
17671785
}
17681786

1787+
// -- Index templates --
1788+
1789+
// IndexPutIndexTemplate creates or updates an index template (new version after 7.8).
1790+
//
1791+
// This service implements the new version of index templates as described
1792+
// on https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-put-template.html.
1793+
//
1794+
// See e.g. IndexPutTemplate for the v1/legacy version.
1795+
func (c *Client) IndexPutIndexTemplate(name string) *IndicesPutIndexTemplateService {
1796+
return NewIndicesPutIndexTemplateService(c).Name(name)
1797+
}
1798+
1799+
// IndexGetIndexTemplate returns an index template (new version after 7.8).
1800+
//
1801+
// This service implements the new version of index templates as described
1802+
// on https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-get-template.html.
1803+
//
1804+
// See e.g. IndexPutTemplate for the v1/legacy version.
1805+
func (c *Client) IndexGetIndexTemplate(name string) *IndicesGetIndexTemplateService {
1806+
return NewIndicesGetIndexTemplateService(c).Name(name)
1807+
}
1808+
1809+
// IndexDeleteIndexTemplate deletes an index template (new version after 7.8).
1810+
//
1811+
// This service implements the new version of index templates as described
1812+
// on https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-delete-template.html.
1813+
//
1814+
// See e.g. IndexPutTemplate for the v1/legacy version.
1815+
func (c *Client) IndexDeleteIndexTemplate(name string) *IndicesDeleteIndexTemplateService {
1816+
return NewIndicesDeleteIndexTemplateService(c).Name(name)
1817+
}
1818+
1819+
// -- TODO Component templates --
1820+
17691821
// GetMapping gets a mapping.
17701822
func (c *Client) GetMapping() *IndicesGetMappingService {
17711823
return NewIndicesGetMappingService(c)

indices_delete_index_template.go

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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/http"
11+
"net/url"
12+
"strings"
13+
14+
"github.com/olivere/elastic/v7/uritemplates"
15+
)
16+
17+
// IndicesDeleteIndexTemplateService deletes index templates.
18+
//
19+
// Index templates have changed during in 7.8 update of Elasticsearch.
20+
// This service implements the new version (7.8 or later). If you want
21+
// the old version, please use the IndicesDeleteTemplateService.
22+
//
23+
// See https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-delete-template.html
24+
// for more details.
25+
type IndicesDeleteIndexTemplateService struct {
26+
client *Client
27+
28+
pretty *bool // pretty format the returned JSON response
29+
human *bool // return human readable values for statistics
30+
errorTrace *bool // include the stack trace of returned errors
31+
filterPath []string // list of filters used to reduce the response
32+
headers http.Header // custom request-level HTTP headers
33+
34+
name string
35+
timeout string
36+
masterTimeout string
37+
}
38+
39+
// NewIndicesDeleteIndexTemplateService creates a new IndicesDeleteIndexTemplateService.
40+
func NewIndicesDeleteIndexTemplateService(client *Client) *IndicesDeleteIndexTemplateService {
41+
return &IndicesDeleteIndexTemplateService{
42+
client: client,
43+
}
44+
}
45+
46+
// Pretty tells Elasticsearch whether to return a formatted JSON response.
47+
func (s *IndicesDeleteIndexTemplateService) Pretty(pretty bool) *IndicesDeleteIndexTemplateService {
48+
s.pretty = &pretty
49+
return s
50+
}
51+
52+
// Human specifies whether human readable values should be returned in
53+
// the JSON response, e.g. "7.5mb".
54+
func (s *IndicesDeleteIndexTemplateService) Human(human bool) *IndicesDeleteIndexTemplateService {
55+
s.human = &human
56+
return s
57+
}
58+
59+
// ErrorTrace specifies whether to include the stack trace of returned errors.
60+
func (s *IndicesDeleteIndexTemplateService) ErrorTrace(errorTrace bool) *IndicesDeleteIndexTemplateService {
61+
s.errorTrace = &errorTrace
62+
return s
63+
}
64+
65+
// FilterPath specifies a list of filters used to reduce the response.
66+
func (s *IndicesDeleteIndexTemplateService) FilterPath(filterPath ...string) *IndicesDeleteIndexTemplateService {
67+
s.filterPath = filterPath
68+
return s
69+
}
70+
71+
// Header adds a header to the request.
72+
func (s *IndicesDeleteIndexTemplateService) Header(name string, value string) *IndicesDeleteIndexTemplateService {
73+
if s.headers == nil {
74+
s.headers = http.Header{}
75+
}
76+
s.headers.Add(name, value)
77+
return s
78+
}
79+
80+
// Headers specifies the headers of the request.
81+
func (s *IndicesDeleteIndexTemplateService) Headers(headers http.Header) *IndicesDeleteIndexTemplateService {
82+
s.headers = headers
83+
return s
84+
}
85+
86+
// Name is the name of the template.
87+
func (s *IndicesDeleteIndexTemplateService) Name(name string) *IndicesDeleteIndexTemplateService {
88+
s.name = name
89+
return s
90+
}
91+
92+
// Timeout is an explicit operation timeout.
93+
func (s *IndicesDeleteIndexTemplateService) Timeout(timeout string) *IndicesDeleteIndexTemplateService {
94+
s.timeout = timeout
95+
return s
96+
}
97+
98+
// MasterTimeout specifies the timeout for connection to master.
99+
func (s *IndicesDeleteIndexTemplateService) MasterTimeout(masterTimeout string) *IndicesDeleteIndexTemplateService {
100+
s.masterTimeout = masterTimeout
101+
return s
102+
}
103+
104+
// buildURL builds the URL for the operation.
105+
func (s *IndicesDeleteIndexTemplateService) buildURL() (string, url.Values, error) {
106+
// Build URL
107+
path, err := uritemplates.Expand("/_index_template/{name}", map[string]string{
108+
"name": s.name,
109+
})
110+
if err != nil {
111+
return "", url.Values{}, err
112+
}
113+
114+
// Add query string parameters
115+
params := url.Values{}
116+
if v := s.pretty; v != nil {
117+
params.Set("pretty", fmt.Sprint(*v))
118+
}
119+
if v := s.human; v != nil {
120+
params.Set("human", fmt.Sprint(*v))
121+
}
122+
if v := s.errorTrace; v != nil {
123+
params.Set("error_trace", fmt.Sprint(*v))
124+
}
125+
if len(s.filterPath) > 0 {
126+
params.Set("filter_path", strings.Join(s.filterPath, ","))
127+
}
128+
if s.timeout != "" {
129+
params.Set("timeout", s.timeout)
130+
}
131+
if s.masterTimeout != "" {
132+
params.Set("master_timeout", s.masterTimeout)
133+
}
134+
return path, params, nil
135+
}
136+
137+
// Validate checks if the operation is valid.
138+
func (s *IndicesDeleteIndexTemplateService) Validate() error {
139+
var invalid []string
140+
if s.name == "" {
141+
invalid = append(invalid, "Name")
142+
}
143+
if len(invalid) > 0 {
144+
return fmt.Errorf("missing required fields: %v", invalid)
145+
}
146+
return nil
147+
}
148+
149+
// Do executes the operation.
150+
func (s *IndicesDeleteIndexTemplateService) Do(ctx context.Context) (*IndicesDeleteIndexTemplateResponse, error) {
151+
// Check pre-conditions
152+
if err := s.Validate(); err != nil {
153+
return nil, err
154+
}
155+
156+
// Get URL for request
157+
path, params, err := s.buildURL()
158+
if err != nil {
159+
return nil, err
160+
}
161+
162+
// Get HTTP response
163+
res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
164+
Method: "DELETE",
165+
Path: path,
166+
Params: params,
167+
Headers: s.headers,
168+
})
169+
if err != nil {
170+
return nil, err
171+
}
172+
173+
// Return operation response
174+
ret := new(IndicesDeleteIndexTemplateResponse)
175+
if err := s.client.decoder.Decode(res.Body, ret); err != nil {
176+
return nil, err
177+
}
178+
return ret, nil
179+
}
180+
181+
// IndicesDeleteIndexTemplateResponse is the response of IndicesDeleteIndexTemplateService.Do.
182+
type IndicesDeleteIndexTemplateResponse struct {
183+
Acknowledged bool `json:"acknowledged"`
184+
ShardsAcknowledged bool `json:"shards_acknowledged"`
185+
Index string `json:"index,omitempty"`
186+
}

indices_delete_template.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ import (
1414
"github.com/olivere/elastic/v7/uritemplates"
1515
)
1616

17-
// IndicesDeleteTemplateService deletes index templates.
18-
// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-templates.html.
17+
// IndicesDeleteTemplateService deletes templates.
18+
//
19+
// Index templates have changed during in 7.8 update of Elasticsearch.
20+
// This service implements the legacy version (7.7 or lower). If you want
21+
// the new version, please use the IndicesDeleteIndexTemplateService.
22+
//
23+
// See https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-delete-template-v1.html
24+
// for more details.
1925
type IndicesDeleteTemplateService struct {
2026
client *Client
2127

indices_exists_template.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ type IndicesExistsTemplateService struct {
2626
filterPath []string // list of filters used to reduce the response
2727
headers http.Header // custom request-level HTTP headers
2828

29-
name string
30-
local *bool
29+
name string
30+
local *bool
31+
masterTimeout string
3132
}
3233

3334
// NewIndicesExistsTemplateService creates a new IndicesExistsTemplateService.
@@ -90,6 +91,12 @@ func (s *IndicesExistsTemplateService) Local(local bool) *IndicesExistsTemplateS
9091
return s
9192
}
9293

94+
// MasterTimeout specifies the timeout for connection to master.
95+
func (s *IndicesExistsTemplateService) MasterTimeout(masterTimeout string) *IndicesExistsTemplateService {
96+
s.masterTimeout = masterTimeout
97+
return s
98+
}
99+
93100
// buildURL builds the URL for the operation.
94101
func (s *IndicesExistsTemplateService) buildURL() (string, url.Values, error) {
95102
// Build URL
@@ -115,7 +122,10 @@ func (s *IndicesExistsTemplateService) buildURL() (string, url.Values, error) {
115122
params.Set("filter_path", strings.Join(s.filterPath, ","))
116123
}
117124
if s.local != nil {
118-
params.Set("local", fmt.Sprintf("%v", *s.local))
125+
params.Set("local", fmt.Sprint(*s.local))
126+
}
127+
if s.masterTimeout != "" {
128+
params.Set("master_timeout", s.masterTimeout)
119129
}
120130
return path, params, nil
121131
}

0 commit comments

Comments
 (0)