Debian Codegraph @

indices_flush.go

raw
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/url"
11 "strings"
12
13 "gopkg.in/olivere/elastic.v5/uritemplates"
14 )
15
16 // Flush allows to flush one or more indices. The flush process of an index
17 // basically frees memory from the index by flushing data to the index
18 // storage and clearing the internal transaction log.
19 //
20 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-flush.html
21 // for details.
22 type IndicesFlushService struct {
23 client *Client
24 pretty bool
25 index []string
26 force *bool
27 waitIfOngoing *bool
28 ignoreUnavailable *bool
29 allowNoIndices *bool
30 expandWildcards string
31 }
32
33 // NewIndicesFlushService creates a new IndicesFlushService.
34 func NewIndicesFlushService(client *Client) *IndicesFlushService {
35 return &IndicesFlushService{
36 client: client,
37 index: make([]string, 0),
38 }
39 }
40
41 // Index is a list of index names; use `_all` or empty string for all indices.
42 func (s *IndicesFlushService) Index(indices ...string) *IndicesFlushService {
43 s.index = append(s.index, indices...)
44 return s
45 }
46
47 // Force indicates whether a flush should be forced even if it is not
48 // necessarily needed ie. if no changes will be committed to the index.
49 // This is useful if transaction log IDs should be incremented even if
50 // no uncommitted changes are present. (This setting can be considered as internal).
51 func (s *IndicesFlushService) Force(force bool) *IndicesFlushService {
52 s.force = &force
53 return s
54 }
55
56 // WaitIfOngoing, if set to true, indicates that the flush operation will
57 // block until the flush can be executed if another flush operation is
58 // already executing. The default is false and will cause an exception
59 // to be thrown on the shard level if another flush operation is already running..
60 func (s *IndicesFlushService) WaitIfOngoing(waitIfOngoing bool) *IndicesFlushService {
61 s.waitIfOngoing = &waitIfOngoing
62 return s
63 }
64
65 // IgnoreUnavailable indicates whether specified concrete indices should be
66 // ignored when unavailable (missing or closed).
67 func (s *IndicesFlushService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesFlushService {
68 s.ignoreUnavailable = &ignoreUnavailable
69 return s
70 }
71
72 // AllowNoIndices indicates whether to ignore if a wildcard indices expression
73 // resolves into no concrete indices. (This includes `_all` string or when
74 // no indices have been specified).
75 func (s *IndicesFlushService) AllowNoIndices(allowNoIndices bool) *IndicesFlushService {
76 s.allowNoIndices = &allowNoIndices
77 return s
78 }
79
80 // ExpandWildcards specifies whether to expand wildcard expression to
81 // concrete indices that are open, closed or both..
82 func (s *IndicesFlushService) ExpandWildcards(expandWildcards string) *IndicesFlushService {
83 s.expandWildcards = expandWildcards
84 return s
85 }
86
87 // Pretty indicates that the JSON response be indented and human readable.
88 func (s *IndicesFlushService) Pretty(pretty bool) *IndicesFlushService {
89 s.pretty = pretty
90 return s
91 }
92
93 // buildURL builds the URL for the operation.
94 func (s *IndicesFlushService) buildURL() (string, url.Values, error) {
95 // Build URL
96 var err error
97 var path string
98
99 if len(s.index) > 0 {
100 path, err = uritemplates.Expand("/{index}/_flush", map[string]string{
101 "index": strings.Join(s.index, ","),
102 })
103 } else {
104 path = "/_flush"
105 }
106 if err != nil {
107 return "", url.Values{}, err
108 }
109
110 // Add query string parameters
111 params := url.Values{}
112 if s.pretty {
113 params.Set("pretty", "1")
114 }
115 if s.force != nil {
116 params.Set("force", fmt.Sprintf("%v", *s.force))
117 }
118 if s.waitIfOngoing != nil {
119 params.Set("wait_if_ongoing", fmt.Sprintf("%v", *s.waitIfOngoing))
120 }
121 if s.ignoreUnavailable != nil {
122 params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
123 }
124 if s.allowNoIndices != nil {
125 params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
126 }
127 if s.expandWildcards != "" {
128 params.Set("expand_wildcards", s.expandWildcards)
129 }
130 return path, params, nil
131 }
132
133 // Validate checks if the operation is valid.
134 func (s *IndicesFlushService) Validate() error {
135 return nil
136 }
137
138 // Do executes the service.
139 func (s *IndicesFlushService) Do(ctx context.Context) (*IndicesFlushResponse, error) {
140 // Check pre-conditions
141 if err := s.Validate(); err != nil {
142 return nil, err
143 }
144
145 // Get URL for request
146 path, params, err := s.buildURL()
147 if err != nil {
148 return nil, err
149 }
150
151 // Get HTTP response
152 res, err := s.client.PerformRequest(ctx, "POST", path, params, nil)
153 if err != nil {
154 return nil, err
155 }
156
157 // Return operation response
158 ret := new(IndicesFlushResponse)
159 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
160 return nil, err
161 }
162 return ret, nil
163 }
164
165 // -- Result of a flush request.
166
167 type IndicesFlushResponse struct {
168 Shards *ShardsInfo `json:"_shards"`
169 }