cat_aliases.go 6.1 KB

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