forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflush.go
114 lines (97 loc) · 2.18 KB
/
flush.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
// Copyright 2012 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"
"strings"
)
type FlushService struct {
client *Client
indices []string
refresh *bool
full *bool
}
func NewFlushService(client *Client) *FlushService {
builder := &FlushService{
client: client,
}
return builder
}
func (s *FlushService) Index(index string) *FlushService {
if s.indices == nil {
s.indices = make([]string, 0)
}
s.indices = append(s.indices, index)
return s
}
func (s *FlushService) Indices(indices ...string) *FlushService {
if s.indices == nil {
s.indices = make([]string, 0)
}
s.indices = append(s.indices, indices...)
return s
}
func (s *FlushService) Refresh(refresh bool) *FlushService {
s.refresh = &refresh
return s
}
func (s *FlushService) Full(full bool) *FlushService {
s.full = &full
return s
}
func (s *FlushService) Do() (*FlushResult, error) {
// Build url
urls := "/"
// Indices part
if len(s.indices) > 0 {
indexPart := make([]string, 0)
for _, index := range s.indices {
indexPart = append(indexPart, cleanPathString(index))
}
urls += strings.Join(indexPart, ",") + "/"
}
urls += "_flush"
// Parameters
params := make(url.Values)
if s.refresh != nil {
params.Set("refresh", fmt.Sprintf("%v", *s.refresh))
}
if s.full != nil {
params.Set("full", fmt.Sprintf("%v", *s.full))
}
if len(params) > 0 {
urls += "?" + params.Encode()
}
// Set up a new request
req, err := s.client.NewRequest("POST", urls)
if err != nil {
return nil, err
}
// 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()
ret := new(FlushResult)
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
// -- Result of a flush request.
type shardsInfo struct {
Total int `json:"total"`
Successful int `json:"successful"`
Failed int `json:"failed"`
}
type FlushResult struct {
Shards shardsInfo `json:"_shards"`
}