indices_stats.go 14 KB

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