indices_stats.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. }
  188. type IndexStatsDetails struct {
  189. Docs *IndexStatsDocs `json:"docs,omitempty"`
  190. Store *IndexStatsStore `json:"store,omitempty"`
  191. Indexing *IndexStatsIndexing `json:"indexing,omitempty"`
  192. Get *IndexStatsGet `json:"get,omitempty"`
  193. Search *IndexStatsSearch `json:"search,omitempty"`
  194. Merges *IndexStatsMerges `json:"merges,omitempty"`
  195. Refresh *IndexStatsRefresh `json:"refresh,omitempty"`
  196. Flush *IndexStatsFlush `json:"flush,omitempty"`
  197. Warmer *IndexStatsWarmer `json:"warmer,omitempty"`
  198. FilterCache *IndexStatsFilterCache `json:"filter_cache,omitempty"`
  199. IdCache *IndexStatsIdCache `json:"id_cache,omitempty"`
  200. Fielddata *IndexStatsFielddata `json:"fielddata,omitempty"`
  201. Percolate *IndexStatsPercolate `json:"percolate,omitempty"`
  202. Completion *IndexStatsCompletion `json:"completion,omitempty"`
  203. Segments *IndexStatsSegments `json:"segments,omitempty"`
  204. Translog *IndexStatsTranslog `json:"translog,omitempty"`
  205. Suggest *IndexStatsSuggest `json:"suggest,omitempty"`
  206. QueryCache *IndexStatsQueryCache `json:"query_cache,omitempty"`
  207. }
  208. type IndexStatsDocs struct {
  209. Count int64 `json:"count,omitempty"`
  210. Deleted int64 `json:"deleted,omitempty"`
  211. }
  212. type IndexStatsStore struct {
  213. Size string `json:"size,omitempty"` // human size, e.g. 119.3mb
  214. SizeInBytes int64 `json:"size_in_bytes,omitempty"`
  215. ThrottleTime string `json:"throttle_time,omitempty"` // human time, e.g. 0s
  216. ThrottleTimeInMillis int64 `json:"throttle_time_in_millis,omitempty"`
  217. }
  218. type IndexStatsIndexing struct {
  219. IndexTotal int64 `json:"index_total,omitempty"`
  220. IndexTime string `json:"index_time,omitempty"`
  221. IndexTimeInMillis int64 `json:"index_time_in_millis,omitempty"`
  222. IndexCurrent int64 `json:"index_current,omitempty"`
  223. DeleteTotal int64 `json:"delete_total,omitempty"`
  224. DeleteTime string `json:"delete_time,omitempty"`
  225. DeleteTimeInMillis int64 `json:"delete_time_in_millis,omitempty"`
  226. DeleteCurrent int64 `json:"delete_current,omitempty"`
  227. NoopUpdateTotal int64 `json:"noop_update_total,omitempty"`
  228. IsThrottled bool `json:"is_throttled,omitempty"`
  229. ThrottleTime string `json:"throttle_time,omitempty"`
  230. ThrottleTimeInMillis int64 `json:"throttle_time_in_millis,omitempty"`
  231. }
  232. type IndexStatsGet struct {
  233. Total int64 `json:"total,omitempty"`
  234. GetTime string `json:"get_time,omitempty"`
  235. TimeInMillis int64 `json:"time_in_millis,omitempty"`
  236. ExistsTotal int64 `json:"exists_total,omitempty"`
  237. ExistsTime string `json:"exists_time,omitempty"`
  238. ExistsTimeInMillis int64 `json:"exists_time_in_millis,omitempty"`
  239. MissingTotal int64 `json:"missing_total,omitempty"`
  240. MissingTime string `json:"missing_time,omitempty"`
  241. MissingTimeInMillis int64 `json:"missing_time_in_millis,omitempty"`
  242. Current int64 `json:"current,omitempty"`
  243. }
  244. type IndexStatsSearch struct {
  245. OpenContexts int64 `json:"open_contexts,omitempty"`
  246. QueryTotal int64 `json:"query_total,omitempty"`
  247. QueryTime string `json:"query_time,omitempty"`
  248. QueryTimeInMillis int64 `json:"query_time_in_millis,omitempty"`
  249. QueryCurrent int64 `json:"query_current,omitempty"`
  250. FetchTotal int64 `json:"fetch_total,omitempty"`
  251. FetchTime string `json:"fetch_time,omitempty"`
  252. FetchTimeInMillis int64 `json:"fetch_time_in_millis,omitempty"`
  253. FetchCurrent int64 `json:"fetch_current,omitempty"`
  254. }
  255. type IndexStatsMerges struct {
  256. Current int64 `json:"current,omitempty"`
  257. CurrentDocs int64 `json:"current_docs,omitempty"`
  258. CurrentSize string `json:"current_size,omitempty"`
  259. CurrentSizeInBytes int64 `json:"current_size_in_bytes,omitempty"`
  260. Total int64 `json:"total,omitempty"`
  261. TotalTime string `json:"total_time,omitempty"`
  262. TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"`
  263. TotalDocs int64 `json:"total_docs,omitempty"`
  264. TotalSize string `json:"total_size,omitempty"`
  265. TotalSizeInBytes int64 `json:"total_size_in_bytes,omitempty"`
  266. }
  267. type IndexStatsRefresh struct {
  268. Total int64 `json:"total,omitempty"`
  269. TotalTime string `json:"total_time,omitempty"`
  270. TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"`
  271. }
  272. type IndexStatsFlush struct {
  273. Total int64 `json:"total,omitempty"`
  274. TotalTime string `json:"total_time,omitempty"`
  275. TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"`
  276. }
  277. type IndexStatsWarmer struct {
  278. Current int64 `json:"current,omitempty"`
  279. Total int64 `json:"total,omitempty"`
  280. TotalTime string `json:"total_time,omitempty"`
  281. TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"`
  282. }
  283. type IndexStatsFilterCache struct {
  284. MemorySize string `json:"memory_size,omitempty"`
  285. MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"`
  286. Evictions int64 `json:"evictions,omitempty"`
  287. }
  288. type IndexStatsIdCache struct {
  289. MemorySize string `json:"memory_size,omitempty"`
  290. MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"`
  291. }
  292. type IndexStatsFielddata 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 IndexStatsPercolate struct {
  298. Total int64 `json:"total,omitempty"`
  299. GetTime string `json:"get_time,omitempty"`
  300. TimeInMillis int64 `json:"time_in_millis,omitempty"`
  301. Current int64 `json:"current,omitempty"`
  302. MemorySize string `json:"memory_size,omitempty"`
  303. MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"`
  304. Queries int64 `json:"queries,omitempty"`
  305. }
  306. type IndexStatsCompletion struct {
  307. Size string `json:"size,omitempty"`
  308. SizeInBytes int64 `json:"size_in_bytes,omitempty"`
  309. }
  310. type IndexStatsSegments struct {
  311. Count int64 `json:"count,omitempty"`
  312. Memory string `json:"memory,omitempty"`
  313. MemoryInBytes int64 `json:"memory_in_bytes,omitempty"`
  314. IndexWriterMemory string `json:"index_writer_memory,omitempty"`
  315. IndexWriterMemoryInBytes int64 `json:"index_writer_memory_in_bytes,omitempty"`
  316. IndexWriterMaxMemory string `json:"index_writer_max_memory,omitempty"`
  317. IndexWriterMaxMemoryInBytes int64 `json:"index_writer_max_memory_in_bytes,omitempty"`
  318. VersionMapMemory string `json:"version_map_memory,omitempty"`
  319. VersionMapMemoryInBytes int64 `json:"version_map_memory_in_bytes,omitempty"`
  320. FixedBitSetMemory string `json:"fixed_bit_set,omitempty"`
  321. FixedBitSetMemoryInBytes int64 `json:"fixed_bit_set_memory_in_bytes,omitempty"`
  322. }
  323. type IndexStatsTranslog struct {
  324. Operations int64 `json:"operations,omitempty"`
  325. Size string `json:"size,omitempty"`
  326. SizeInBytes int64 `json:"size_in_bytes,omitempty"`
  327. }
  328. type IndexStatsSuggest struct {
  329. Total int64 `json:"total,omitempty"`
  330. Time string `json:"time,omitempty"`
  331. TimeInMillis int64 `json:"time_in_millis,omitempty"`
  332. Current int64 `json:"current,omitempty"`
  333. }
  334. type IndexStatsQueryCache struct {
  335. MemorySize string `json:"memory_size,omitempty"`
  336. MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"`
  337. Evictions int64 `json:"evictions,omitempty"`
  338. HitCount int64 `json:"hit_count,omitempty"`
  339. MissCount int64 `json:"miss_count,omitempty"`
  340. }