Skip to content

Commit 2365c21

Browse files
authored
Add Point In Time API (#1480)
This commit adds the Point in Time API as described here: https://www.elastic.co/guide/en/elasticsearch/reference/7.x/point-in-time-api.html Close #1433
1 parent 0697e23 commit 2365c21

8 files changed

+594
-0
lines changed

client.go

+10
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,16 @@ func (c *Client) ClearScroll(scrollIds ...string) *ClearScrollService {
16361636
return NewClearScrollService(c).ScrollId(scrollIds...)
16371637
}
16381638

1639+
// OpenPointInTime opens a new Point in Time.
1640+
func (c *Client) OpenPointInTime(indices ...string) *OpenPointInTimeService {
1641+
return NewOpenPointInTimeService(c).Index(indices...)
1642+
}
1643+
1644+
// ClosePointInTime closes an existing Point in Time.
1645+
func (c *Client) ClosePointInTime(id string) *ClosePointInTimeService {
1646+
return NewClosePointInTimeService(c).ID(id)
1647+
}
1648+
16391649
// -- Indices APIs --
16401650

16411651
// CreateIndex returns a service to create a new index.

pit.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// PointInTime is a lightweight view into the state of the data that existed
8+
// when initiated. It can be created with OpenPointInTime API and be used
9+
// when searching, e.g. in Search API or with SearchSource.
10+
type PointInTime struct {
11+
// Id that uniquely identifies the point in time, as created with the
12+
// OpenPointInTime API.
13+
Id string `json:"id,omitempty"`
14+
// KeepAlive is the time for which this specific PointInTime will be
15+
// kept alive by Elasticsearch.
16+
KeepAlive string `json:"keep_alive,omitempty"`
17+
}
18+
19+
// NewPointInTime creates a new PointInTime.
20+
func NewPointInTime(id, keepAlive string) *PointInTime {
21+
return &PointInTime{
22+
Id: id,
23+
KeepAlive: keepAlive,
24+
}
25+
}
26+
27+
// Source generates the JSON serializable fragment for the PointInTime.
28+
func (pit *PointInTime) Source() (interface{}, error) {
29+
if pit == nil {
30+
return nil, nil
31+
}
32+
return map[string]interface{}{
33+
"id": pit.Id,
34+
"keep_alive": pit.KeepAlive,
35+
}, nil
36+
}

pit_close.go

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
15+
// ClosePointInTimeService removes a point in time.
16+
//
17+
// See https://www.elastic.co/guide/en/elasticsearch/reference/7.x/point-in-time-api.html
18+
// for details.
19+
type ClosePointInTimeService struct {
20+
client *Client
21+
22+
pretty *bool // pretty format the returned JSON response
23+
human *bool // return human readable values for statistics
24+
errorTrace *bool // include the stack trace of returned errors
25+
filterPath []string // list of filters used to reduce the response
26+
headers http.Header // custom request-level HTTP headers
27+
28+
id string
29+
bodyJson interface{}
30+
bodyString string
31+
}
32+
33+
// NewClosePointInTimeService creates a new ClosePointInTimeService.
34+
func NewClosePointInTimeService(client *Client) *ClosePointInTimeService {
35+
return &ClosePointInTimeService{
36+
client: client,
37+
}
38+
}
39+
40+
// Pretty tells Elasticsearch whether to return a formatted JSON response.
41+
func (s *ClosePointInTimeService) Pretty(pretty bool) *ClosePointInTimeService {
42+
s.pretty = &pretty
43+
return s
44+
}
45+
46+
// Human specifies whether human readable values should be returned in
47+
// the JSON response, e.g. "7.5mb".
48+
func (s *ClosePointInTimeService) Human(human bool) *ClosePointInTimeService {
49+
s.human = &human
50+
return s
51+
}
52+
53+
// ErrorTrace specifies whether to include the stack trace of returned errors.
54+
func (s *ClosePointInTimeService) ErrorTrace(errorTrace bool) *ClosePointInTimeService {
55+
s.errorTrace = &errorTrace
56+
return s
57+
}
58+
59+
// FilterPath specifies a list of filters used to reduce the response.
60+
func (s *ClosePointInTimeService) FilterPath(filterPath ...string) *ClosePointInTimeService {
61+
s.filterPath = filterPath
62+
return s
63+
}
64+
65+
// Header adds a header to the request.
66+
func (s *ClosePointInTimeService) Header(name string, value string) *ClosePointInTimeService {
67+
if s.headers == nil {
68+
s.headers = http.Header{}
69+
}
70+
s.headers.Add(name, value)
71+
return s
72+
}
73+
74+
// Headers specifies the headers of the request.
75+
func (s *ClosePointInTimeService) Headers(headers http.Header) *ClosePointInTimeService {
76+
s.headers = headers
77+
return s
78+
}
79+
80+
// ID to close.
81+
func (s *ClosePointInTimeService) ID(id string) *ClosePointInTimeService {
82+
s.id = id
83+
return s
84+
}
85+
86+
// BodyJson is the document as a serializable JSON interface.
87+
func (s *ClosePointInTimeService) BodyJson(body interface{}) *ClosePointInTimeService {
88+
s.bodyJson = body
89+
return s
90+
}
91+
92+
// BodyString is the document encoded as a string.
93+
func (s *ClosePointInTimeService) BodyString(body string) *ClosePointInTimeService {
94+
s.bodyString = body
95+
return s
96+
}
97+
98+
// buildURL builds the URL for the operation.
99+
func (s *ClosePointInTimeService) buildURL() (string, string, url.Values, error) {
100+
var (
101+
method = "DELETE"
102+
path = "/_pit"
103+
)
104+
105+
// Add query string parameters
106+
params := url.Values{}
107+
if v := s.pretty; v != nil {
108+
params.Set("pretty", fmt.Sprint(*v))
109+
}
110+
if v := s.human; v != nil {
111+
params.Set("human", fmt.Sprint(*v))
112+
}
113+
if v := s.errorTrace; v != nil {
114+
params.Set("error_trace", fmt.Sprint(*v))
115+
}
116+
if len(s.filterPath) > 0 {
117+
params.Set("filter_path", strings.Join(s.filterPath, ","))
118+
}
119+
return method, path, params, nil
120+
}
121+
122+
// Validate checks if the operation is valid.
123+
func (s *ClosePointInTimeService) Validate() error {
124+
return nil
125+
}
126+
127+
// Do executes the operation.
128+
func (s *ClosePointInTimeService) Do(ctx context.Context) (*ClosePointInTimeResponse, error) {
129+
// Check pre-conditions
130+
if err := s.Validate(); err != nil {
131+
return nil, err
132+
}
133+
134+
// Get URL for request
135+
method, path, params, err := s.buildURL()
136+
if err != nil {
137+
return nil, err
138+
}
139+
140+
// Setup HTTP request body
141+
var body interface{}
142+
if s.id != "" {
143+
body = map[string]interface{}{
144+
"id": s.id,
145+
}
146+
} else if s.bodyJson != nil {
147+
body = s.bodyJson
148+
} else {
149+
body = s.bodyString
150+
}
151+
152+
// Get HTTP response
153+
res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
154+
Method: method,
155+
Path: path,
156+
Params: params,
157+
Body: body,
158+
Headers: s.headers,
159+
})
160+
if err != nil {
161+
return nil, err
162+
}
163+
164+
// Return operation response
165+
ret := new(ClosePointInTimeResponse)
166+
if err := s.client.decoder.Decode(res.Body, ret); err != nil {
167+
return nil, err
168+
}
169+
return ret, nil
170+
}
171+
172+
// ClosePointInTimeResponse is the result of closing a point in time.
173+
type ClosePointInTimeResponse struct {
174+
Succeeded bool `json:"succeeded,omitempty"`
175+
NumFreed int `json:"num_freed,omitempty"`
176+
}

0 commit comments

Comments
 (0)