Debian Codegraph @

indices_forcemerge.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 // IndicesForcemergeService allows to force merging of one or more indices.
17 // The merge relates to the number of segments a Lucene index holds
18 // within each shard. The force merge operation allows to reduce the number
19 // of segments by merging them.
20 //
21 // See http://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-forcemerge.html
22 // for more information.
23 type IndicesForcemergeService struct {
24 client *Client
25 pretty bool
26 index []string
27 allowNoIndices *bool
28 expandWildcards string
29 flush *bool
30 ignoreUnavailable *bool
31 maxNumSegments interface{}
32 onlyExpungeDeletes *bool
33 operationThreading interface{}
34 }
35
36 // NewIndicesForcemergeService creates a new IndicesForcemergeService.
37 func NewIndicesForcemergeService(client *Client) *IndicesForcemergeService {
38 return &IndicesForcemergeService{
39 client: client,
40 index: make([]string, 0),
41 }
42 }
43
44 // Index is a list of index names; use `_all` or empty string to perform
45 // the operation on all indices.
46 func (s *IndicesForcemergeService) Index(index ...string) *IndicesForcemergeService {
47 if s.index == nil {
48 s.index = make([]string, 0)
49 }
50 s.index = append(s.index, index...)
51 return s
52 }
53
54 // AllowNoIndices indicates whether to ignore if a wildcard indices
55 // expression resolves into no concrete indices.
56 // (This includes `_all` string or when no indices have been specified).
57 func (s *IndicesForcemergeService) AllowNoIndices(allowNoIndices bool) *IndicesForcemergeService {
58 s.allowNoIndices = &allowNoIndices
59 return s
60 }
61
62 // ExpandWildcards indicates whether to expand wildcard expression to
63 // concrete indices that are open, closed or both..
64 func (s *IndicesForcemergeService) ExpandWildcards(expandWildcards string) *IndicesForcemergeService {
65 s.expandWildcards = expandWildcards
66 return s
67 }
68
69 // Flush specifies whether the index should be flushed after performing
70 // the operation (default: true).
71 func (s *IndicesForcemergeService) Flush(flush bool) *IndicesForcemergeService {
72 s.flush = &flush
73 return s
74 }
75
76 // IgnoreUnavailable indicates whether specified concrete indices should
77 // be ignored when unavailable (missing or closed).
78 func (s *IndicesForcemergeService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesForcemergeService {
79 s.ignoreUnavailable = &ignoreUnavailable
80 return s
81 }
82
83 // MaxNumSegments specifies the number of segments the index should be
84 // merged into (default: dynamic).
85 func (s *IndicesForcemergeService) MaxNumSegments(maxNumSegments interface{}) *IndicesForcemergeService {
86 s.maxNumSegments = maxNumSegments
87 return s
88 }
89
90 // OnlyExpungeDeletes specifies whether the operation should only expunge
91 // deleted documents.
92 func (s *IndicesForcemergeService) OnlyExpungeDeletes(onlyExpungeDeletes bool) *IndicesForcemergeService {
93 s.onlyExpungeDeletes = &onlyExpungeDeletes
94 return s
95 }
96
97 func (s *IndicesForcemergeService) OperationThreading(operationThreading interface{}) *IndicesForcemergeService {
98 s.operationThreading = operationThreading
99 return s
100 }
101
102 // Pretty indicates that the JSON response be indented and human readable.
103 func (s *IndicesForcemergeService) Pretty(pretty bool) *IndicesForcemergeService {
104 s.pretty = pretty
105 return s
106 }
107
108 // buildURL builds the URL for the operation.
109 func (s *IndicesForcemergeService) buildURL() (string, url.Values, error) {
110 var err error
111 var path string
112
113 // Build URL
114 if len(s.index) > 0 {
115 path, err = uritemplates.Expand("/{index}/_forcemerge", map[string]string{
116 "index": strings.Join(s.index, ","),
117 })
118 } else {
119 path = "/_forcemerge"
120 }
121 if err != nil {
122 return "", url.Values{}, err
123 }
124
125 // Add query string parameters
126 params := url.Values{}
127 if s.pretty {
128 params.Set("pretty", "1")
129 }
130 if s.allowNoIndices != nil {
131 params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
132 }
133 if s.expandWildcards != "" {
134 params.Set("expand_wildcards", s.expandWildcards)
135 }
136 if s.flush != nil {
137 params.Set("flush", fmt.Sprintf("%v", *s.flush))
138 }
139 if s.ignoreUnavailable != nil {
140 params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
141 }
142 if s.maxNumSegments != nil {
143 params.Set("max_num_segments", fmt.Sprintf("%v", s.maxNumSegments))
144 }
145 if s.onlyExpungeDeletes != nil {
146 params.Set("only_expunge_deletes", fmt.Sprintf("%v", *s.onlyExpungeDeletes))
147 }
148 if s.operationThreading != nil {
149 params.Set("operation_threading", fmt.Sprintf("%v", s.operationThreading))
150 }
151 return path, params, nil
152 }
153
154 // Validate checks if the operation is valid.
155 func (s *IndicesForcemergeService) Validate() error {
156 return nil
157 }
158
159 // Do executes the operation.
160 func (s *IndicesForcemergeService) Do(ctx context.Context) (*IndicesForcemergeResponse, error) {
161 // Check pre-conditions
162 if err := s.Validate(); err != nil {
163 return nil, err
164 }
165
166 // Get URL for request
167 path, params, err := s.buildURL()
168 if err != nil {
169 return nil, err
170 }
171
172 // Get HTTP response
173 res, err := s.client.PerformRequest(ctx, "POST", path, params, nil)
174 if err != nil {
175 return nil, err
176 }
177
178 // Return operation response
179 ret := new(IndicesForcemergeResponse)
180 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
181 return nil, err
182 }
183 return ret, nil
184 }
185
186 // IndicesForcemergeResponse is the response of IndicesForcemergeService.Do.
187 type IndicesForcemergeResponse struct {
188 Shards *ShardsInfo `json:"_shards"`
189 }