cat_count.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // CatCountService provides quick access to the document count of the entire cluster,
  14. // or individual indices.
  15. //
  16. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cat-count.html
  17. // for details.
  18. type CatCountService 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. headers http.Header // custom request-level HTTP headers
  25. index []string
  26. local *bool
  27. masterTimeout string
  28. columns []string
  29. sort []string // list of columns for sort order
  30. }
  31. // NewCatCountService creates a new CatCountService.
  32. func NewCatCountService(client *Client) *CatCountService {
  33. return &CatCountService{
  34. client: client,
  35. }
  36. }
  37. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  38. func (s *CatCountService) Pretty(pretty bool) *CatCountService {
  39. s.pretty = &pretty
  40. return s
  41. }
  42. // Human specifies whether human readable values should be returned in
  43. // the JSON response, e.g. "7.5mb".
  44. func (s *CatCountService) Human(human bool) *CatCountService {
  45. s.human = &human
  46. return s
  47. }
  48. // ErrorTrace specifies whether to include the stack trace of returned errors.
  49. func (s *CatCountService) ErrorTrace(errorTrace bool) *CatCountService {
  50. s.errorTrace = &errorTrace
  51. return s
  52. }
  53. // FilterPath specifies a list of filters used to reduce the response.
  54. func (s *CatCountService) FilterPath(filterPath ...string) *CatCountService {
  55. s.filterPath = filterPath
  56. return s
  57. }
  58. // Header adds a header to the request.
  59. func (s *CatCountService) Header(name string, value string) *CatCountService {
  60. if s.headers == nil {
  61. s.headers = http.Header{}
  62. }
  63. s.headers.Add(name, value)
  64. return s
  65. }
  66. // Headers specifies the headers of the request.
  67. func (s *CatCountService) Headers(headers http.Header) *CatCountService {
  68. s.headers = headers
  69. return s
  70. }
  71. // Index specifies zero or more indices for which to return counts
  72. // (by default counts for all indices are returned).
  73. func (s *CatCountService) Index(index ...string) *CatCountService {
  74. s.index = index
  75. return s
  76. }
  77. // Local indicates to return local information, i.e. do not retrieve
  78. // the state from master node (default: false).
  79. func (s *CatCountService) Local(local bool) *CatCountService {
  80. s.local = &local
  81. return s
  82. }
  83. // MasterTimeout is the explicit operation timeout for connection to master node.
  84. func (s *CatCountService) MasterTimeout(masterTimeout string) *CatCountService {
  85. s.masterTimeout = masterTimeout
  86. return s
  87. }
  88. // Columns to return in the response.
  89. // To get a list of all possible columns to return, run the following command
  90. // in your terminal:
  91. //
  92. // Example:
  93. // curl 'http://localhost:9200/_cat/count?help'
  94. //
  95. // You can use Columns("*") to return all possible columns. That might take
  96. // a little longer than the default set of columns.
  97. func (s *CatCountService) Columns(columns ...string) *CatCountService {
  98. s.columns = columns
  99. return s
  100. }
  101. // Sort is a list of fields to sort by.
  102. func (s *CatCountService) Sort(fields ...string) *CatCountService {
  103. s.sort = fields
  104. return s
  105. }
  106. // buildURL builds the URL for the operation.
  107. func (s *CatCountService) buildURL() (string, url.Values, error) {
  108. // Build URL
  109. var (
  110. path string
  111. err error
  112. )
  113. if len(s.index) > 0 {
  114. path, err = uritemplates.Expand("/_cat/count/{index}", map[string]string{
  115. "index": strings.Join(s.index, ","),
  116. })
  117. } else {
  118. path = "/_cat/count"
  119. }
  120. if err != nil {
  121. return "", url.Values{}, err
  122. }
  123. // Add query string parameters
  124. params := url.Values{
  125. "format": []string{"json"}, // always returns as JSON
  126. }
  127. if v := s.pretty; v != nil {
  128. params.Set("pretty", fmt.Sprint(*v))
  129. }
  130. if v := s.human; v != nil {
  131. params.Set("human", fmt.Sprint(*v))
  132. }
  133. if v := s.errorTrace; v != nil {
  134. params.Set("error_trace", fmt.Sprint(*v))
  135. }
  136. if len(s.filterPath) > 0 {
  137. params.Set("filter_path", strings.Join(s.filterPath, ","))
  138. }
  139. if v := s.local; v != nil {
  140. params.Set("local", fmt.Sprint(*v))
  141. }
  142. if s.masterTimeout != "" {
  143. params.Set("master_timeout", s.masterTimeout)
  144. }
  145. if len(s.sort) > 0 {
  146. params.Set("s", strings.Join(s.sort, ","))
  147. }
  148. if len(s.columns) > 0 {
  149. params.Set("h", strings.Join(s.columns, ","))
  150. }
  151. return path, params, nil
  152. }
  153. // Do executes the operation.
  154. func (s *CatCountService) Do(ctx context.Context) (CatCountResponse, error) {
  155. // Get URL for request
  156. path, params, err := s.buildURL()
  157. if err != nil {
  158. return nil, err
  159. }
  160. // Get HTTP response
  161. res, err := s.client.PerformRequestWithOptions(ctx, PerformRequestOptions{
  162. Method: "GET",
  163. Path: path,
  164. Params: params,
  165. Headers: s.headers,
  166. })
  167. if err != nil {
  168. return nil, err
  169. }
  170. // Return operation response
  171. var ret CatCountResponse
  172. if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
  173. return nil, err
  174. }
  175. return ret, nil
  176. }
  177. // -- Result of a get request.
  178. // CatCountResponse is the outcome of CatCountService.Do.
  179. type CatCountResponse []CatCountResponseRow
  180. // CatCountResponseRow specifies the data returned for one index
  181. // of a CatCountResponse. Notice that not all of these fields might
  182. // be filled; that depends on the number of columns chose in the
  183. // request (see CatCountService.Columns).
  184. type CatCountResponseRow struct {
  185. Epoch int64 `json:"epoch,string"` // e.g. 1527077996
  186. Timestamp string `json:"timestamp"` // e.g. "12:19:56"
  187. Count int `json:"count,string"` // number of documents
  188. }