indices_get_aliases.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // AliasesService returns the aliases associated with one or more indices.
  13. // See http://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-aliases.html.
  14. type AliasesService struct {
  15. client *Client
  16. index []string
  17. pretty bool
  18. }
  19. // NewAliasesService instantiates a new AliasesService.
  20. func NewAliasesService(client *Client) *AliasesService {
  21. builder := &AliasesService{
  22. client: client,
  23. }
  24. return builder
  25. }
  26. // Pretty asks Elasticsearch to indent the returned JSON.
  27. func (s *AliasesService) Pretty(pretty bool) *AliasesService {
  28. s.pretty = pretty
  29. return s
  30. }
  31. // Index adds one or more indices.
  32. func (s *AliasesService) Index(index ...string) *AliasesService {
  33. s.index = append(s.index, index...)
  34. return s
  35. }
  36. // buildURL builds the URL for the operation.
  37. func (s *AliasesService) buildURL() (string, url.Values, error) {
  38. var err error
  39. var path string
  40. if len(s.index) > 0 {
  41. path, err = uritemplates.Expand("/{index}/_aliases", map[string]string{
  42. "index": strings.Join(s.index, ","),
  43. })
  44. } else {
  45. path = "/_aliases"
  46. }
  47. if err != nil {
  48. return "", url.Values{}, err
  49. }
  50. // Add query string parameters
  51. params := url.Values{}
  52. if s.pretty {
  53. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  54. }
  55. return path, params, nil
  56. }
  57. func (s *AliasesService) Do(ctx context.Context) (*AliasesResult, error) {
  58. path, params, err := s.buildURL()
  59. if err != nil {
  60. return nil, err
  61. }
  62. // Get response
  63. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  64. if err != nil {
  65. return nil, err
  66. }
  67. // {
  68. // "indexName" : {
  69. // "aliases" : {
  70. // "alias1" : { },
  71. // "alias2" : { }
  72. // }
  73. // },
  74. // "indexName2" : {
  75. // ...
  76. // },
  77. // }
  78. indexMap := make(map[string]interface{})
  79. if err := s.client.decoder.Decode(res.Body, &indexMap); err != nil {
  80. return nil, err
  81. }
  82. // Each (indexName, _)
  83. ret := &AliasesResult{
  84. Indices: make(map[string]indexResult),
  85. }
  86. for indexName, indexData := range indexMap {
  87. indexOut, found := ret.Indices[indexName]
  88. if !found {
  89. indexOut = indexResult{Aliases: make([]aliasResult, 0)}
  90. }
  91. // { "aliases" : { ... } }
  92. indexDataMap, ok := indexData.(map[string]interface{})
  93. if ok {
  94. aliasesData, ok := indexDataMap["aliases"].(map[string]interface{})
  95. if ok {
  96. for aliasName, _ := range aliasesData {
  97. aliasRes := aliasResult{AliasName: aliasName}
  98. indexOut.Aliases = append(indexOut.Aliases, aliasRes)
  99. }
  100. }
  101. }
  102. ret.Indices[indexName] = indexOut
  103. }
  104. return ret, nil
  105. }
  106. // -- Result of an alias request.
  107. type AliasesResult struct {
  108. Indices map[string]indexResult
  109. }
  110. type indexResult struct {
  111. Aliases []aliasResult
  112. }
  113. type aliasResult struct {
  114. AliasName string
  115. }
  116. func (ar AliasesResult) IndicesByAlias(aliasName string) []string {
  117. var indices []string
  118. for indexName, indexInfo := range ar.Indices {
  119. for _, aliasInfo := range indexInfo.Aliases {
  120. if aliasInfo.AliasName == aliasName {
  121. indices = append(indices, indexName)
  122. }
  123. }
  124. }
  125. return indices
  126. }
  127. func (ir indexResult) HasAlias(aliasName string) bool {
  128. for _, alias := range ir.Aliases {
  129. if alias.AliasName == aliasName {
  130. return true
  131. }
  132. }
  133. return false
  134. }