You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indices_get_aliases.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. "github.com/olivere/elastic/v7/uritemplates"
  12. )
  13. // AliasesService returns the aliases associated with one or more indices, or the
  14. // indices associated with one or more aliases, or a combination of those filters.
  15. // See http://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-aliases.html.
  16. type AliasesService struct {
  17. client *Client
  18. pretty *bool // pretty format the returned JSON response
  19. human *bool // return human readable values for statistics
  20. errorTrace *bool // include the stack trace of returned errors
  21. filterPath []string // list of filters used to reduce the response
  22. headers http.Header // custom request-level HTTP headers
  23. index []string
  24. alias []string
  25. }
  26. // NewAliasesService instantiates a new AliasesService.
  27. func NewAliasesService(client *Client) *AliasesService {
  28. builder := &AliasesService{
  29. client: client,
  30. }
  31. return builder
  32. }
  33. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  34. func (s *AliasesService) Pretty(pretty bool) *AliasesService {
  35. s.pretty = &pretty
  36. return s
  37. }
  38. // Human specifies whether human readable values should be returned in
  39. // the JSON response, e.g. "7.5mb".
  40. func (s *AliasesService) Human(human bool) *AliasesService {
  41. s.human = &human
  42. return s
  43. }
  44. // ErrorTrace specifies whether to include the stack trace of returned errors.
  45. func (s *AliasesService) ErrorTrace(errorTrace bool) *AliasesService {
  46. s.errorTrace = &errorTrace
  47. return s
  48. }
  49. // FilterPath specifies a list of filters used to reduce the response.
  50. func (s *AliasesService) FilterPath(filterPath ...string) *AliasesService {
  51. s.filterPath = filterPath
  52. return s
  53. }
  54. // Header adds a header to the request.
  55. func (s *AliasesService) Header(name string, value string) *AliasesService {
  56. if s.headers == nil {
  57. s.headers = http.Header{}
  58. }
  59. s.headers.Add(name, value)
  60. return s
  61. }
  62. // Headers specifies the headers of the request.
  63. func (s *AliasesService) Headers(headers http.Header) *AliasesService {
  64. s.headers = headers
  65. return s
  66. }
  67. // Index adds one or more indices.
  68. func (s *AliasesService) Index(index ...string) *AliasesService {
  69. s.index = append(s.index, index...)
  70. return s
  71. }
  72. // Alias adds one or more aliases.
  73. func (s *AliasesService) Alias(alias ...string) *AliasesService {
  74. s.alias = append(s.alias, alias...)
  75. return s
  76. }
  77. // buildURL builds the URL for the operation.
  78. func (s *AliasesService) buildURL() (string, url.Values, error) {
  79. var err error
  80. var path string
  81. if len(s.index) > 0 {
  82. path, err = uritemplates.Expand("/{index}/_alias/{alias}", map[string]string{
  83. "index": strings.Join(s.index, ","),
  84. "alias": strings.Join(s.alias, ","),
  85. })
  86. } else {
  87. path, err = uritemplates.Expand("/_alias/{alias}", map[string]string{
  88. "alias": strings.Join(s.alias, ","),
  89. })
  90. }
  91. if err != nil {
  92. return "", url.Values{}, err
  93. }
  94. path = strings.TrimSuffix(path, "/")
  95. // Add query string parameters
  96. params := url.Values{}
  97. if v := s.pretty; v != nil {
  98. params.Set("pretty", fmt.Sprint(*v))
  99. }
  100. if v := s.human; v != nil {
  101. params.Set("human", fmt.Sprint(*v))
  102. }
  103. if v := s.errorTrace; v != nil {
  104. params.Set("error_trace", fmt.Sprint(*v))
  105. }
  106. if len(s.filterPath) > 0 {
  107. params.Set("filter_path", strings.Join(s.filterPath, ","))
  108. }
  109. return path, params, nil
  110. }
  111. func (s *AliasesService) Do(ctx context.Context) (*AliasesResult, error) {
  112. path, params, err := s.buildURL()
  113. if err != nil {
  114. return nil, err
  115. }
  116. // Get response
  117. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  118. Method: "GET",
  119. Path: path,
  120. Params: params,
  121. Headers: s.headers,
  122. })
  123. if err != nil {
  124. return nil, err
  125. }
  126. // {
  127. // "indexName" : {
  128. // "aliases" : {
  129. // "alias1" : { },
  130. // "alias2" : { }
  131. // }
  132. // },
  133. // "indexName2" : {
  134. // ...
  135. // },
  136. // }
  137. indexMap := make(map[string]struct {
  138. Aliases map[string]struct {
  139. IsWriteIndex bool `json:"is_write_index"`
  140. } `json:"aliases"`
  141. })
  142. if err := s.client.decoder.Decode(res.Body, &indexMap); err != nil {
  143. return nil, err
  144. }
  145. // Each (indexName, _)
  146. ret := &AliasesResult{
  147. Indices: make(map[string]indexResult),
  148. }
  149. for indexName, indexData := range indexMap {
  150. if indexData.Aliases == nil {
  151. continue
  152. }
  153. indexOut, found := ret.Indices[indexName]
  154. if !found {
  155. indexOut = indexResult{Aliases: make([]aliasResult, 0)}
  156. }
  157. // { "aliases" : { ... } }
  158. for aliasName, aliasData := range indexData.Aliases {
  159. aliasRes := aliasResult{AliasName: aliasName, IsWriteIndex: aliasData.IsWriteIndex}
  160. indexOut.Aliases = append(indexOut.Aliases, aliasRes)
  161. }
  162. ret.Indices[indexName] = indexOut
  163. }
  164. return ret, nil
  165. }
  166. // -- Result of an alias request.
  167. // AliasesResult is the outcome of calling AliasesService.Do.
  168. type AliasesResult struct {
  169. Indices map[string]indexResult
  170. }
  171. type indexResult struct {
  172. Aliases []aliasResult
  173. }
  174. type aliasResult struct {
  175. AliasName string
  176. IsWriteIndex bool
  177. }
  178. // IndicesByAlias returns all indices given a specific alias name.
  179. func (ar AliasesResult) IndicesByAlias(aliasName string) []string {
  180. var indices []string
  181. for indexName, indexInfo := range ar.Indices {
  182. for _, aliasInfo := range indexInfo.Aliases {
  183. if aliasInfo.AliasName == aliasName {
  184. indices = append(indices, indexName)
  185. }
  186. }
  187. }
  188. return indices
  189. }
  190. // HasAlias returns true if the index has a specific alias.
  191. func (ir indexResult) HasAlias(aliasName string) bool {
  192. for _, alias := range ir.Aliases {
  193. if alias.AliasName == aliasName {
  194. return true
  195. }
  196. }
  197. return false
  198. }