cat_shards.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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/http"
  9. "net/url"
  10. "strings"
  11. "gopkg.in/olivere/elastic.v5/uritemplates"
  12. )
  13. // CatShardsService returns the list of shards plus some additional
  14. // information about them.
  15. //
  16. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/cat-shards.html
  17. // for details.
  18. type CatShardsService struct {
  19. client *Client
  20. pretty *bool // pretty format the returned JSON response
  21. human *bool // return human readable values for statistics
  22. errorTrace *bool // include the stack trace of returned errors
  23. filterPath []string // list of filters used to reduce the response
  24. index []string
  25. bytes string // b, k, kb, m, mb, g, gb, t, tb, p, or pb
  26. local *bool
  27. masterTimeout string
  28. columns []string
  29. sort []string // list of columns for sort order
  30. headers http.Header
  31. }
  32. // NewCatShardsService creates a new CatShardsService.
  33. func NewCatShardsService(client *Client) *CatShardsService {
  34. return &CatShardsService{
  35. client: client,
  36. }
  37. }
  38. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  39. func (s *CatShardsService) Pretty(pretty bool) *CatShardsService {
  40. s.pretty = &pretty
  41. return s
  42. }
  43. // Human specifies whether human readable values should be returned in
  44. // the JSON response, e.g. "7.5mb".
  45. func (s *CatShardsService) Human(human bool) *CatShardsService {
  46. s.human = &human
  47. return s
  48. }
  49. // ErrorTrace specifies whether to include the stack trace of returned errors.
  50. func (s *CatShardsService) ErrorTrace(errorTrace bool) *CatShardsService {
  51. s.errorTrace = &errorTrace
  52. return s
  53. }
  54. // FilterPath specifies a list of filters used to reduce the response.
  55. func (s *CatShardsService) FilterPath(filterPath ...string) *CatShardsService {
  56. s.filterPath = filterPath
  57. return s
  58. }
  59. // Header adds a header to the request.
  60. func (s *CatShardsService) Header(name string, value string) *CatShardsService {
  61. if s.headers == nil {
  62. s.headers = http.Header{}
  63. }
  64. s.headers.Add(name, value)
  65. return s
  66. }
  67. // Headers specifies the headers of the request.
  68. func (s *CatShardsService) Headers(headers http.Header) *CatShardsService {
  69. s.headers = headers
  70. return s
  71. }
  72. // Index is the name of the index to list (by default all indices are returned).
  73. func (s *CatShardsService) Index(index ...string) *CatShardsService {
  74. s.index = index
  75. return s
  76. }
  77. // Bytes represents the unit in which to display byte values.
  78. // Valid values are: "b", "k", "kb", "m", "mb", "g", "gb", "t", "tb", "p" or "pb".
  79. func (s *CatShardsService) Bytes(bytes string) *CatShardsService {
  80. s.bytes = bytes
  81. return s
  82. }
  83. // Local indicates to return local information, i.e. do not retrieve
  84. // the state from master node (default: false).
  85. func (s *CatShardsService) Local(local bool) *CatShardsService {
  86. s.local = &local
  87. return s
  88. }
  89. // MasterTimeout is the explicit operation timeout for connection to master node.
  90. func (s *CatShardsService) MasterTimeout(masterTimeout string) *CatShardsService {
  91. s.masterTimeout = masterTimeout
  92. return s
  93. }
  94. // Columns to return in the response.
  95. //
  96. // To get a list of all possible columns to return, run the following command
  97. // in your terminal:
  98. //
  99. // Example:
  100. // curl 'http://localhost:9200/_cat/shards?help'
  101. //
  102. // You can use Columns("*") to return all possible columns. That might take
  103. // a little longer than the default set of columns.
  104. func (s *CatShardsService) Columns(columns ...string) *CatShardsService {
  105. s.columns = columns
  106. return s
  107. }
  108. // Sort is a list of fields to sort by.
  109. func (s *CatShardsService) Sort(fields ...string) *CatShardsService {
  110. s.sort = fields
  111. return s
  112. }
  113. // buildURL builds the URL for the operation.
  114. func (s *CatShardsService) buildURL() (string, url.Values, error) {
  115. // Build URL
  116. var (
  117. path string
  118. err error
  119. )
  120. if len(s.index) > 0 {
  121. path, err = uritemplates.Expand("/_cat/shards/{index}", map[string]string{
  122. "index": strings.Join(s.index, ","),
  123. })
  124. } else {
  125. path = "/_cat/shards"
  126. }
  127. if err != nil {
  128. return "", url.Values{}, err
  129. }
  130. // Add query string parameters
  131. params := url.Values{
  132. "format": []string{"json"}, // always returns as JSON
  133. }
  134. if v := s.pretty; v != nil {
  135. params.Set("pretty", fmt.Sprint(*v))
  136. }
  137. if v := s.human; v != nil {
  138. params.Set("human", fmt.Sprint(*v))
  139. }
  140. if v := s.errorTrace; v != nil {
  141. params.Set("error_trace", fmt.Sprint(*v))
  142. }
  143. if len(s.filterPath) > 0 {
  144. params.Set("filter_path", strings.Join(s.filterPath, ","))
  145. }
  146. if s.bytes != "" {
  147. params.Set("bytes", s.bytes)
  148. }
  149. if v := s.local; v != nil {
  150. params.Set("local", fmt.Sprint(*v))
  151. }
  152. if s.masterTimeout != "" {
  153. params.Set("master_timeout", s.masterTimeout)
  154. }
  155. if len(s.columns) > 0 {
  156. // loop through all columns and apply alias if needed
  157. for i, column := range s.columns {
  158. if fullValueRaw, isAliased := catShardsResponseRowAliasesMap[column]; isAliased {
  159. // alias can be translated to multiple fields,
  160. // so if translated value contains a comma, than replace the first value
  161. // and append the others
  162. if strings.Contains(fullValueRaw, ",") {
  163. fullValues := strings.Split(fullValueRaw, ",")
  164. s.columns[i] = fullValues[0]
  165. s.columns = append(s.columns, fullValues[1:]...)
  166. } else {
  167. s.columns[i] = fullValueRaw
  168. }
  169. }
  170. }
  171. params.Set("h", strings.Join(s.columns, ","))
  172. }
  173. if len(s.sort) > 0 {
  174. params.Set("s", strings.Join(s.sort, ","))
  175. }
  176. return path, params, nil
  177. }
  178. // Do executes the operation.
  179. func (s *CatShardsService) Do(ctx context.Context) (CatShardsResponse, error) {
  180. // Get URL for request
  181. path, params, err := s.buildURL()
  182. if err != nil {
  183. return nil, err
  184. }
  185. // Get HTTP response
  186. res, err := s.client.PerformRequestWithOptions(ctx, PerformRequestOptions{
  187. Method: "GET",
  188. Path: path,
  189. Params: params,
  190. Headers: s.headers,
  191. })
  192. if err != nil {
  193. return nil, err
  194. }
  195. // Return operation response
  196. var ret CatShardsResponse
  197. if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
  198. return nil, err
  199. }
  200. return ret, nil
  201. }
  202. // -- Result of a get request.
  203. // CatShardsResponse is the outcome of CatShardsService.Do.
  204. type CatShardsResponse []CatShardsResponseRow
  205. // CatShardsResponseRow specifies the data returned for one index
  206. // of a CatShardsResponse. Notice that not all of these fields might
  207. // be filled; that depends on the number of columns chose in the
  208. // request (see CatShardsService.Columns).
  209. type CatShardsResponseRow struct {
  210. Index string `json:"index"` // index name
  211. Shard int `json:"shard,string"` // shard number, e.g. 1
  212. Prirep string `json:"prirep"` // "r" for replica, "p" for primary
  213. State string `json:"state"` // STARTED, INITIALIZING, RELOCATING, or UNASSIGNED
  214. Docs int64 `json:"docs,string"` // number of documents, e.g. 142847
  215. Store string `json:"store"` // size, e.g. "40mb"
  216. IP string `json:"ip"` // IP address
  217. ID string `json:"id"`
  218. Node string `json:"node"` // Node name
  219. SyncID string `json:"sync_id"`
  220. UnassignedReason string `json:"unassigned.reason"`
  221. UnassignedAt string `json:"unassigned.at"`
  222. UnassignedFor string `json:"unassigned.for"`
  223. UnassignedDetails string `json:"unassigned.details"`
  224. RecoverysourceType string `json:"recoverysource.type"`
  225. CompletionSize string `json:"completion.size"` // size of completion on primaries & replicas
  226. FielddataMemorySize string `json:"fielddata.memory_size"` // used fielddata cache on primaries & replicas
  227. FielddataEvictions int `json:"fielddata.evictions,string"` // fielddata evictions on primaries & replicas
  228. QueryCacheMemorySize string `json:"query_cache.memory_size"` // used query cache on primaries & replicas
  229. QueryCacheEvictions int `json:"query_cache.evictions,string"` // query cache evictions on primaries & replicas
  230. FlushTotal int `json:"flush.total,string"` // number of flushes on primaries & replicas
  231. FlushTotalTime string `json:"flush.total_time"` // time spent in flush on primaries & replicas
  232. GetCurrent int `json:"get.current,string"` // number of current get ops on primaries & replicas
  233. GetTime string `json:"get.time"` // time spent in get on primaries & replicas
  234. GetTotal int `json:"get.total,string"` // number of get ops on primaries & replicas
  235. GetExistsTime string `json:"get.exists_time"` // time spent in successful gets on primaries & replicas
  236. GetExistsTotal int `json:"get.exists_total,string"` // number of successful gets on primaries & replicas
  237. GetMissingTime string `json:"get.missing_time"` // time spent in failed gets on primaries & replicas
  238. GetMissingTotal int `json:"get.missing_total,string"` // number of failed gets on primaries & replicas
  239. IndexingDeleteCurrent int `json:"indexing.delete_current,string"` // number of current deletions on primaries & replicas
  240. IndexingDeleteTime string `json:"indexing.delete_time"` // time spent in deletions on primaries & replicas
  241. IndexingDeleteTotal int `json:"indexing.delete_total,string"` // number of delete ops on primaries & replicas
  242. IndexingIndexCurrent int `json:"indexing.index_current,string"` // number of current indexing on primaries & replicas
  243. IndexingIndexTime string `json:"indexing.index_time"` // time spent in indexing on primaries & replicas
  244. IndexingIndexTotal int `json:"indexing.index_total,string"` // number of index ops on primaries & replicas
  245. IndexingIndexFailed int `json:"indexing.index_failed,string"` // number of failed indexing ops on primaries & replicas
  246. MergesCurrent int `json:"merges.current,string"` // number of current merges on primaries & replicas
  247. MergesCurrentDocs int `json:"merges.current_docs,string"` // number of current merging docs on primaries & replicas
  248. MergesCurrentSize string `json:"merges.current_size"` // size of current merges on primaries & replicas
  249. MergesTotal int `json:"merges.total,string"` // number of completed merge ops on primaries & replicas
  250. MergesTotalDocs int `json:"merges.total_docs,string"` // docs merged on primaries & replicas
  251. MergesTotalSize string `json:"merges.total_size"` // size merged on primaries & replicas
  252. MergesTotalTime string `json:"merges.total_time"` // time spent in merges on primaries & replicas
  253. RefreshTotal int `json:"refresh.total,string"` // total refreshes on primaries & replicas
  254. RefreshExternalTotal int `json:"refresh.external_total,string"` // total external refreshes on primaries & replicas
  255. RefreshTime string `json:"refresh.time"` // time spent in refreshes on primaries & replicas
  256. RefreshExternalTime string `json:"refresh.external_time"` // external time spent in refreshes on primaries & replicas
  257. RefreshListeners int `json:"refresh.listeners,string"` // number of pending refresh listeners on primaries & replicas
  258. SearchFetchCurrent int `json:"search.fetch_current,string"` // current fetch phase ops on primaries & replicas
  259. SearchFetchTime string `json:"search.fetch_time"` // time spent in fetch phase on primaries & replicas
  260. SearchFetchTotal int `json:"search.fetch_total,string"` // total fetch ops on primaries & replicas
  261. SearchOpenContexts int `json:"search.open_contexts,string"` // open search contexts on primaries & replicas
  262. SearchQueryCurrent int `json:"search.query_current,string"` // current query phase ops on primaries & replicas
  263. SearchQueryTime string `json:"search.query_time"` // time spent in query phase on primaries & replicas, e.g. "0s"
  264. SearchQueryTotal int `json:"search.query_total,string"` // total query phase ops on primaries & replicas
  265. SearchScrollCurrent int `json:"search.scroll_current,string"` // open scroll contexts on primaries & replicas
  266. SearchScrollTime string `json:"search.scroll_time"` // time scroll contexts held open on primaries & replicas, e.g. "0s"
  267. SearchScrollTotal int `json:"search.scroll_total,string"` // completed scroll contexts on primaries & replicas
  268. SearchThrottled bool `json:"search.throttled,string"` // indicates if the index is search throttled
  269. SegmentsCount int `json:"segments.count,string"` // number of segments on primaries & replicas
  270. SegmentsMemory string `json:"segments.memory"` // memory used by segments on primaries & replicas, e.g. "1.3kb"
  271. SegmentsIndexWriterMemory string `json:"segments.index_writer_memory"` // memory used by index writer on primaries & replicas, e.g. "0b"
  272. SegmentsVersionMapMemory string `json:"segments.version_map_memory"` // memory used by version map on primaries & replicas, e.g. "0b"
  273. SegmentsFixedBitsetMemory string `json:"segments.fixed_bitset_memory"` // memory used by fixed bit sets for nested object field types and type filters for types referred in _parent fields on primaries & replicas, e.g. "0b"
  274. SeqNoMax int `json:"seq_no.max,string"`
  275. SeqNoLocalCheckpoint int `json:"seq_no.local_checkpoint,string"`
  276. SeqNoGlobalCheckpoint int `json:"seq_no.global_checkpoint,string"`
  277. WarmerCurrent int `json:"warmer.current,string"` // current warmer ops on primaries & replicas
  278. WarmerTotal int `json:"warmer.total,string"` // total warmer ops on primaries & replicas
  279. WarmerTotalTime string `json:"warmer.total_time"` // time spent in warmers on primaries & replicas, e.g. "47s"
  280. }
  281. // catShardsResponseRowAliasesMap holds the global map for columns aliases
  282. // the map is used by CatShardsService.buildURL.
  283. // For backwards compatibility some fields are able to have the same aliases
  284. // that means that one alias can be translated to different columns (from different elastic versions)
  285. // example for understanding: rto -> RefreshTotal, RefreshExternalTotal
  286. var catShardsResponseRowAliasesMap = map[string]string{
  287. "sync_id": "sync_id",
  288. "ur": "unassigned.reason",
  289. "ua": "unassigned.at",
  290. "uf": "unassigned.for",
  291. "ud": "unassigned.details",
  292. "rs": "recoverysource.type",
  293. "cs": "completion.size",
  294. "fm": "fielddata.memory_size",
  295. "fe": "fielddata.evictions",
  296. "qcm": "query_cache.memory_size",
  297. "qce": "query_cache.evictions",
  298. "ft": "flush.total",
  299. "ftt": "flush.total_time",
  300. "gc": "get.current",
  301. "gti": "get.time",
  302. "gto": "get.total",
  303. "geti": "get.exists_time",
  304. "geto": "get.exists_total",
  305. "gmti": "get.missing_time",
  306. "gmto": "get.missing_total",
  307. "idc": "indexing.delete_current",
  308. "idti": "indexing.delete_time",
  309. "idto": "indexing.delete_total",
  310. "iic": "indexing.index_current",
  311. "iiti": "indexing.index_time",
  312. "iito": "indexing.index_total",
  313. "iif": "indexing.index_failed",
  314. "mc": "merges.current",
  315. "mcd": "merges.current_docs",
  316. "mcs": "merges.current_size",
  317. "mt": "merges.total",
  318. "mtd": "merges.total_docs",
  319. "mts": "merges.total_size",
  320. "mtt": "merges.total_time",
  321. "rto": "refresh.total",
  322. "rti": "refresh.time",
  323. // "rto": "refresh.external_total",
  324. // "rti": "refresh.external_time",
  325. "rli": "refresh.listeners",
  326. "sfc": "search.fetch_current",
  327. "sfti": "search.fetch_time",
  328. "sfto": "search.fetch_total",
  329. "so": "search.open_contexts",
  330. "sqc": "search.query_current",
  331. "sqti": "search.query_time",
  332. "sqto": "search.query_total",
  333. "scc": "search.scroll_current",
  334. "scti": "search.scroll_time",
  335. "scto": "search.scroll_total",
  336. "sc": "segments.count",
  337. "sm": "segments.memory",
  338. "siwm": "segments.index_writer_memory",
  339. "svmm": "segments.version_map_memory",
  340. "sfbm": "segments.fixed_bitset_memory",
  341. "sqm": "seq_no.max",
  342. "sql": "seq_no.local_checkpoint",
  343. "sqg": "seq_no.global_checkpoint",
  344. "wc": "warmer.current",
  345. "wto": "warmer.total",
  346. "wtt": "warmer.total_time",
  347. }