Debian Codegraph @

indices_stats.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 // IndicesStatsService provides stats on various metrics of one or more
17 // indices. See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-stats.html.
18 type IndicesStatsService struct {
19 client *Client
20 pretty bool
21 metric []string
22 index []string
23 level string
24 types []string
25 completionFields []string
26 fielddataFields []string
27 fields []string
28 groups []string
29 human *bool
30 }
31
32 // NewIndicesStatsService creates a new IndicesStatsService.
33 func NewIndicesStatsService(client *Client) *IndicesStatsService {
34 return &IndicesStatsService{
35 client: client,
36 index: make([]string, 0),
37 metric: make([]string, 0),
38 completionFields: make([]string, 0),
39 fielddataFields: make([]string, 0),
40 fields: make([]string, 0),
41 groups: make([]string, 0),
42 types: make([]string, 0),
43 }
44 }
45
46 // Metric limits the information returned the specific metrics. Options are:
47 // docs, store, indexing, get, search, completion, fielddata, flush, merge,
48 // query_cache, refresh, suggest, and warmer.
49 func (s *IndicesStatsService) Metric(metric ...string) *IndicesStatsService {
50 s.metric = append(s.metric, metric...)
51 return s
52 }
53
54 // Index is the list of index names; use `_all` or empty string to perform
55 // the operation on all indices.
56 func (s *IndicesStatsService) Index(indices ...string) *IndicesStatsService {
57 s.index = append(s.index, indices...)
58 return s
59 }
60
61 // Type is a list of document types for the `indexing` index metric.
62 func (s *IndicesStatsService) Type(types ...string) *IndicesStatsService {
63 s.types = append(s.types, types...)
64 return s
65 }
66
67 // Level returns stats aggregated at cluster, index or shard level.
68 func (s *IndicesStatsService) Level(level string) *IndicesStatsService {
69 s.level = level
70 return s
71 }
72
73 // CompletionFields is a list of fields for `fielddata` and `suggest`
74 // index metric (supports wildcards).
75 func (s *IndicesStatsService) CompletionFields(completionFields ...string) *IndicesStatsService {
76 s.completionFields = append(s.completionFields, completionFields...)
77 return s
78 }
79
80 // FielddataFields is a list of fields for `fielddata` index metric (supports wildcards).
81 func (s *IndicesStatsService) FielddataFields(fielddataFields ...string) *IndicesStatsService {
82 s.fielddataFields = append(s.fielddataFields, fielddataFields...)
83 return s
84 }
85
86 // Fields is a list of fields for `fielddata` and `completion` index metric
87 // (supports wildcards).
88 func (s *IndicesStatsService) Fields(fields ...string) *IndicesStatsService {
89 s.fields = append(s.fields, fields...)
90 return s
91 }
92
93 // Groups is a list of search groups for `search` index metric.
94 func (s *IndicesStatsService) Groups(groups ...string) *IndicesStatsService {
95 s.groups = append(s.groups, groups...)
96 return s
97 }
98
99 // Human indicates whether to return time and byte values in human-readable format..
100 func (s *IndicesStatsService) Human(human bool) *IndicesStatsService {
101 s.human = &human
102 return s
103 }
104
105 // Pretty indicates that the JSON response be indented and human readable.
106 func (s *IndicesStatsService) Pretty(pretty bool) *IndicesStatsService {
107 s.pretty = pretty
108 return s
109 }
110
111 // buildURL builds the URL for the operation.
112 func (s *IndicesStatsService) buildURL() (string, url.Values, error) {
113 var err error
114 var path string
115 if len(s.index) > 0 && len(s.metric) > 0 {
116 path, err = uritemplates.Expand("/{index}/_stats/{metric}", map[string]string{
117 "index": strings.Join(s.index, ","),
118 "metric": strings.Join(s.metric, ","),
119 })
120 } else if len(s.index) > 0 {
121 path, err = uritemplates.Expand("/{index}/_stats", map[string]string{
122 "index": strings.Join(s.index, ","),
123 })
124 } else if len(s.metric) > 0 {
125 path, err = uritemplates.Expand("/_stats/{metric}", map[string]string{
126 "metric": strings.Join(s.metric, ","),
127 })
128 } else {
129 path = "/_stats"
130 }
131 if err != nil {
132 return "", url.Values{}, err
133 }
134
135 // Add query string parameters
136 params := url.Values{}
137 if s.pretty {
138 params.Set("pretty", "1")
139 }
140 if len(s.groups) > 0 {
141 params.Set("groups", strings.Join(s.groups, ","))
142 }
143 if s.human != nil {
144 params.Set("human", fmt.Sprintf("%v", *s.human))
145 }
146 if s.level != "" {
147 params.Set("level", s.level)
148 }
149 if len(s.types) > 0 {
150 params.Set("types", strings.Join(s.types, ","))
151 }
152 if len(s.completionFields) > 0 {
153 params.Set("completion_fields", strings.Join(s.completionFields, ","))
154 }
155 if len(s.fielddataFields) > 0 {
156 params.Set("fielddata_fields", strings.Join(s.fielddataFields, ","))
157 }
158 if len(s.fields) > 0 {
159 params.Set("fields", strings.Join(s.fields, ","))
160 }
161 return path, params, nil
162 }
163
164 // Validate checks if the operation is valid.
165 func (s *IndicesStatsService) Validate() error {
166 return nil
167 }
168
169 // Do executes the operation.
170 func (s *IndicesStatsService) Do(ctx context.Context) (*IndicesStatsResponse, error) {
171 // Check pre-conditions
172 if err := s.Validate(); err != nil {
173 return nil, err
174 }
175
176 // Get URL for request
177 path, params, err := s.buildURL()
178 if err != nil {
179 return nil, err
180 }
181
182 // Get HTTP response
183 res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
184 if err != nil {
185 return nil, err
186 }
187
188 // Return operation response
189 ret := new(IndicesStatsResponse)
190 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
191 return nil, err
192 }
193 return ret, nil
194 }
195
196 // IndicesStatsResponse is the response of IndicesStatsService.Do.
197 type IndicesStatsResponse struct {
198 // Shards provides information returned from shards.
199 Shards *ShardsInfo `json:"_shards"`
200
201 // All provides summary stats about all indices.
202 All *IndexStats `json:"_all,omitempty"`
203
204 // Indices provides a map into the stats of an index. The key of the
205 // map is the index name.
206 Indices map[string]*IndexStats `json:"indices,omitempty"`
207 }
208
209 // IndexStats is index stats for a specific index.
210 type IndexStats struct {
211 Primaries *IndexStatsDetails `json:"primaries,omitempty"`
212 Total *IndexStatsDetails `json:"total,omitempty"`
213 Shards map[string][]*IndexStatsDetails `json:"shards,omitempty"`
214 }
215
216 type IndexStatsDetails struct {
217 Docs *IndexStatsDocs `json:"docs,omitempty"`
218 Store *IndexStatsStore `json:"store,omitempty"`
219 Indexing *IndexStatsIndexing `json:"indexing,omitempty"`
220 Get *IndexStatsGet `json:"get,omitempty"`
221 Search *IndexStatsSearch `json:"search,omitempty"`
222 Merges *IndexStatsMerges `json:"merges,omitempty"`
223 Refresh *IndexStatsRefresh `json:"refresh,omitempty"`
224 Flush *IndexStatsFlush `json:"flush,omitempty"`
225 Warmer *IndexStatsWarmer `json:"warmer,omitempty"`
226 FilterCache *IndexStatsFilterCache `json:"filter_cache,omitempty"`
227 IdCache *IndexStatsIdCache `json:"id_cache,omitempty"`
228 Fielddata *IndexStatsFielddata `json:"fielddata,omitempty"`
229 Percolate *IndexStatsPercolate `json:"percolate,omitempty"`
230 Completion *IndexStatsCompletion `json:"completion,omitempty"`
231 Segments *IndexStatsSegments `json:"segments,omitempty"`
232 Translog *IndexStatsTranslog `json:"translog,omitempty"`
233 Suggest *IndexStatsSuggest `json:"suggest,omitempty"`
234 QueryCache *IndexStatsQueryCache `json:"query_cache,omitempty"`
235 }
236
237 type IndexStatsDocs struct {
238 Count int64 `json:"count,omitempty"`
239 Deleted int64 `json:"deleted,omitempty"`
240 }
241
242 type IndexStatsStore struct {
243 Size string `json:"size,omitempty"` // human size, e.g. 119.3mb
244 SizeInBytes int64 `json:"size_in_bytes,omitempty"`
245 ThrottleTime string `json:"throttle_time,omitempty"` // human time, e.g. 0s
246 ThrottleTimeInMillis int64 `json:"throttle_time_in_millis,omitempty"`
247 }
248
249 type IndexStatsIndexing struct {
250 IndexTotal int64 `json:"index_total,omitempty"`
251 IndexTime string `json:"index_time,omitempty"`
252 IndexTimeInMillis int64 `json:"index_time_in_millis,omitempty"`
253 IndexCurrent int64 `json:"index_current,omitempty"`
254 DeleteTotal int64 `json:"delete_total,omitempty"`
255 DeleteTime string `json:"delete_time,omitempty"`
256 DeleteTimeInMillis int64 `json:"delete_time_in_millis,omitempty"`
257 DeleteCurrent int64 `json:"delete_current,omitempty"`
258 NoopUpdateTotal int64 `json:"noop_update_total,omitempty"`
259 IsThrottled bool `json:"is_throttled,omitempty"`
260 ThrottleTime string `json:"throttle_time,omitempty"`
261 ThrottleTimeInMillis int64 `json:"throttle_time_in_millis,omitempty"`
262 }
263
264 type IndexStatsGet struct {
265 Total int64 `json:"total,omitempty"`
266 GetTime string `json:"get_time,omitempty"`
267 TimeInMillis int64 `json:"time_in_millis,omitempty"`
268 ExistsTotal int64 `json:"exists_total,omitempty"`
269 ExistsTime string `json:"exists_time,omitempty"`
270 ExistsTimeInMillis int64 `json:"exists_time_in_millis,omitempty"`
271 MissingTotal int64 `json:"missing_total,omitempty"`
272 MissingTime string `json:"missing_time,omitempty"`
273 MissingTimeInMillis int64 `json:"missing_time_in_millis,omitempty"`
274 Current int64 `json:"current,omitempty"`
275 }
276
277 type IndexStatsSearch struct {
278 OpenContexts int64 `json:"open_contexts,omitempty"`
279 QueryTotal int64 `json:"query_total,omitempty"`
280 QueryTime string `json:"query_time,omitempty"`
281 QueryTimeInMillis int64 `json:"query_time_in_millis,omitempty"`
282 QueryCurrent int64 `json:"query_current,omitempty"`
283 FetchTotal int64 `json:"fetch_total,omitempty"`
284 FetchTime string `json:"fetch_time,omitempty"`
285 FetchTimeInMillis int64 `json:"fetch_time_in_millis,omitempty"`
286 FetchCurrent int64 `json:"fetch_current,omitempty"`
287 ScrollTotal int64 `json:"scroll_total,omitempty"`
288 ScrollTime string `json:"scroll_time,omitempty"`
289 ScrollTimeInMillis int64 `json:"scroll_time_in_millis,omitempty"`
290 ScrollCurrent int64 `json:"scroll_current,omitempty"`
291 SuggestTotal int64 `json:"suggest_total,omitempty"`
292 SuggestTime string `json:"suggest_time,omitempty"`
293 SuggestTimeInMillis int64 `json:"suggest_time_in_millis,omitempty"`
294 SuggestCurrent int64 `json:"suggest_current,omitempty"`
295 }
296
297 type IndexStatsMerges struct {
298 Current int64 `json:"current,omitempty"`
299 CurrentDocs int64 `json:"current_docs,omitempty"`
300 CurrentSize string `json:"current_size,omitempty"`
301 CurrentSizeInBytes int64 `json:"current_size_in_bytes,omitempty"`
302 Total int64 `json:"total,omitempty"`
303 TotalTime string `json:"total_time,omitempty"`
304 TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"`
305 TotalDocs int64 `json:"total_docs,omitempty"`
306 TotalSize string `json:"total_size,omitempty"`
307 TotalSizeInBytes int64 `json:"total_size_in_bytes,omitempty"`
308 }
309
310 type IndexStatsRefresh struct {
311 Total int64 `json:"total,omitempty"`
312 TotalTime string `json:"total_time,omitempty"`
313 TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"`
314 }
315
316 type IndexStatsFlush struct {
317 Total int64 `json:"total,omitempty"`
318 TotalTime string `json:"total_time,omitempty"`
319 TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"`
320 }
321
322 type IndexStatsWarmer struct {
323 Current int64 `json:"current,omitempty"`
324 Total int64 `json:"total,omitempty"`
325 TotalTime string `json:"total_time,omitempty"`
326 TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"`
327 }
328
329 type IndexStatsFilterCache struct {
330 MemorySize string `json:"memory_size,omitempty"`
331 MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"`
332 Evictions int64 `json:"evictions,omitempty"`
333 }
334
335 type IndexStatsIdCache struct {
336 MemorySize string `json:"memory_size,omitempty"`
337 MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"`
338 }
339
340 type IndexStatsFielddata struct {
341 MemorySize string `json:"memory_size,omitempty"`
342 MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"`
343 Evictions int64 `json:"evictions,omitempty"`
344 }
345
346 type IndexStatsPercolate struct {
347 Total int64 `json:"total,omitempty"`
348 GetTime string `json:"get_time,omitempty"`
349 TimeInMillis int64 `json:"time_in_millis,omitempty"`
350 Current int64 `json:"current,omitempty"`
351 MemorySize string `json:"memory_size,omitempty"`
352 MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"`
353 Queries int64 `json:"queries,omitempty"`
354 }
355
356 type IndexStatsCompletion struct {
357 Size string `json:"size,omitempty"`
358 SizeInBytes int64 `json:"size_in_bytes,omitempty"`
359 }
360
361 type IndexStatsSegments struct {
362 Count int64 `json:"count,omitempty"`
363 Memory string `json:"memory,omitempty"`
364 MemoryInBytes int64 `json:"memory_in_bytes,omitempty"`
365 IndexWriterMemory string `json:"index_writer_memory,omitempty"`
366 IndexWriterMemoryInBytes int64 `json:"index_writer_memory_in_bytes,omitempty"`
367 IndexWriterMaxMemory string `json:"index_writer_max_memory,omitempty"`
368 IndexWriterMaxMemoryInBytes int64 `json:"index_writer_max_memory_in_bytes,omitempty"`
369 VersionMapMemory string `json:"version_map_memory,omitempty"`
370 VersionMapMemoryInBytes int64 `json:"version_map_memory_in_bytes,omitempty"`
371 FixedBitSetMemory string `json:"fixed_bit_set,omitempty"`
372 FixedBitSetMemoryInBytes int64 `json:"fixed_bit_set_memory_in_bytes,omitempty"`
373 }
374
375 type IndexStatsTranslog struct {
376 Operations int64 `json:"operations,omitempty"`
377 Size string `json:"size,omitempty"`
378 SizeInBytes int64 `json:"size_in_bytes,omitempty"`
379 }
380
381 type IndexStatsSuggest struct {
382 Total int64 `json:"total,omitempty"`
383 Time string `json:"time,omitempty"`
384 TimeInMillis int64 `json:"time_in_millis,omitempty"`
385 Current int64 `json:"current,omitempty"`
386 }
387
388 type IndexStatsQueryCache struct {
389 MemorySize string `json:"memory_size,omitempty"`
390 MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"`
391 Evictions int64 `json:"evictions,omitempty"`
392 HitCount int64 `json:"hit_count,omitempty"`
393 MissCount int64 `json:"miss_count,omitempty"`
394 }