Debian Codegraph @

indices_segments.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 "encoding/json"
10 "fmt"
11 "net/url"
12 "strings"
13
14 "gopkg.in/olivere/elastic.v5/uritemplates"
15 )
16
17 // IndicesSegmentsService provides low level segments information that a
18 // Lucene index (shard level) is built with. Allows to be used to provide
19 // more information on the state of a shard and an index, possibly
20 // optimization information, data "wasted" on deletes, and so on.
21 //
22 // Find further documentation at
23 // https://www.elastic.co/guide/en/elasticsearch/reference/5.6/indices-segments.html.
24 type IndicesSegmentsService struct {
25 client *Client
26 pretty bool
27 index []string
28 allowNoIndices *bool
29 expandWildcards string
30 ignoreUnavailable *bool
31 human *bool
32 operationThreading interface{}
33 verbose *bool
34 }
35
36 // NewIndicesSegmentsService creates a new IndicesSegmentsService.
37 func NewIndicesSegmentsService(client *Client) *IndicesSegmentsService {
38 return &IndicesSegmentsService{
39 client: client,
40 }
41 }
42
43 // Index is a comma-separated list of index names; use `_all` or empty string
44 // to perform the operation on all indices.
45 func (s *IndicesSegmentsService) Index(indices ...string) *IndicesSegmentsService {
46 s.index = append(s.index, indices...)
47 return s
48 }
49
50 // AllowNoIndices indicates whether to ignore if a wildcard indices expression
51 // resolves into no concrete indices. (This includes `_all` string or when
52 // no indices have been specified).
53 func (s *IndicesSegmentsService) AllowNoIndices(allowNoIndices bool) *IndicesSegmentsService {
54 s.allowNoIndices = &allowNoIndices
55 return s
56 }
57
58 // ExpandWildcards indicates whether to expand wildcard expression to concrete indices
59 // that are open, closed or both.
60 func (s *IndicesSegmentsService) ExpandWildcards(expandWildcards string) *IndicesSegmentsService {
61 s.expandWildcards = expandWildcards
62 return s
63 }
64
65 // IgnoreUnavailable indicates whether specified concrete indices should be
66 // ignored when unavailable (missing or closed).
67 func (s *IndicesSegmentsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesSegmentsService {
68 s.ignoreUnavailable = &ignoreUnavailable
69 return s
70 }
71
72 // Human, when set to true, returns time and byte-values in human-readable format.
73 func (s *IndicesSegmentsService) Human(human bool) *IndicesSegmentsService {
74 s.human = &human
75 return s
76 }
77
78 // OperationThreading is undocumented in Elasticsearch as of now.
79 func (s *IndicesSegmentsService) OperationThreading(operationThreading interface{}) *IndicesSegmentsService {
80 s.operationThreading = operationThreading
81 return s
82 }
83
84 // Verbose, when set to true, includes detailed memory usage by Lucene.
85 func (s *IndicesSegmentsService) Verbose(verbose bool) *IndicesSegmentsService {
86 s.verbose = &verbose
87 return s
88 }
89
90 // Pretty indicates that the JSON response be indented and human readable.
91 func (s *IndicesSegmentsService) Pretty(pretty bool) *IndicesSegmentsService {
92 s.pretty = pretty
93 return s
94 }
95
96 // buildURL builds the URL for the operation.
97 func (s *IndicesSegmentsService) buildURL() (string, url.Values, error) {
98 var err error
99 var path string
100
101 if len(s.index) > 0 {
102 path, err = uritemplates.Expand("/{index}/_segments", map[string]string{
103 "index": strings.Join(s.index, ","),
104 })
105 } else {
106 path = "/_segments"
107 }
108 if err != nil {
109 return "", url.Values{}, err
110 }
111
112 // Add query string parameters
113 params := url.Values{}
114 if s.pretty {
115 params.Set("pretty", "true")
116 }
117 if s.allowNoIndices != nil {
118 params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
119 }
120 if s.expandWildcards != "" {
121 params.Set("expand_wildcards", s.expandWildcards)
122 }
123 if s.ignoreUnavailable != nil {
124 params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
125 }
126 if s.human != nil {
127 params.Set("human", fmt.Sprintf("%v", *s.human))
128 }
129 if s.operationThreading != nil {
130 params.Set("operation_threading", fmt.Sprintf("%v", s.operationThreading))
131 }
132 if s.verbose != nil {
133 params.Set("verbose", fmt.Sprintf("%v", *s.verbose))
134 }
135 return path, params, nil
136 }
137
138 // Validate checks if the operation is valid.
139 func (s *IndicesSegmentsService) Validate() error {
140 return nil
141 }
142
143 // Do executes the operation.
144 func (s *IndicesSegmentsService) Do(ctx context.Context) (*IndicesSegmentsResponse, error) {
145 // Check pre-conditions
146 if err := s.Validate(); err != nil {
147 return nil, err
148 }
149
150 // Get URL for request
151 path, params, err := s.buildURL()
152 if err != nil {
153 return nil, err
154 }
155
156 // Get HTTP response
157 res, err := s.client.PerformRequestWithOptions(ctx, PerformRequestOptions{
158 Method: "GET",
159 Path: path,
160 Params: params,
161 })
162 if err != nil {
163 return nil, err
164 }
165
166 // Return operation response
167 ret := new(IndicesSegmentsResponse)
168 if err := json.Unmarshal(res.Body, ret); err != nil {
169 return nil, err
170 }
171 return ret, nil
172 }
173
174 // IndicesSegmentsResponse is the response of IndicesSegmentsService.Do.
175 type IndicesSegmentsResponse struct {
176 // Shards provides information returned from shards.
177 Shards *ShardsInfo `json:"_shards"`
178
179 // Indices provides a map into the stats of an index.
180 // The key of the map is the index name.
181 Indices map[string]*IndexSegments `json:"indices,omitempty"`
182 }
183
184 type IndexSegments struct {
185 // Shards provides a map into the shard related information of an index.
186 // The key of the map is the number of a specific shard.
187 Shards map[string][]*IndexSegmentsShards `json:"shards,omitempty"`
188 }
189
190 type IndexSegmentsShards struct {
191 Routing *IndexSegmentsRouting `json:"routing,omitempty"`
192 NumCommittedSegments int64 `json:"num_committed_segments,omitempty"`
193 NumSearchSegments int64 `json:"num_search_segments"`
194
195 // Segments provides a map into the segment related information of a shard.
196 // The key of the map is the specific lucene segment id.
197 Segments map[string]*IndexSegmentsDetails `json:"segments,omitempty"`
198 }
199
200 type IndexSegmentsRouting struct {
201 State string `json:"state,omitempty"`
202 Primary bool `json:"primary,omitempty"`
203 Node string `json:"node,omitempty"`
204 RelocatingNode string `json:"relocating_node,omitempty"`
205 }
206
207 type IndexSegmentsDetails struct {
208 Generation int64 `json:"generation,omitempty"`
209 NumDocs int64 `json:"num_docs,omitempty"`
210 DeletedDocs int64 `json:"deleted_docs,omitempty"`
211 Size string `json:"size,omitempty"`
212 SizeInBytes int64 `json:"size_in_bytes,omitempty"`
213 Memory string `json:"memory,omitempty"`
214 MemoryInBytes int64 `json:"memory_in_bytes,omitempty"`
215 Committed bool `json:"committed,omitempty"`
216 Search bool `json:"search,omitempty"`
217 Version string `json:"version,omitempty"`
218 Compound bool `json:"compound,omitempty"`
219 MergeId string `json:"merge_id,omitempty"`
220 RAMTree []*IndexSegmentsRamTree `json:"ram_tree,omitempty"`
221 }
222
223 type IndexSegmentsRamTree struct {
224 Description string `json:"description,omitempty"`
225 Size string `json:"size,omitempty"`
226 SizeInBytes int64 `json:"size_in_bytes,omitempty"`
227 Children []*IndexSegmentsRamTree `json:"children,omitempty"`
228 }