aliases.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2012-2015 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. "encoding/json"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "github.com/olivere/elastic/uritemplates"
  11. )
  12. type AliasesService struct {
  13. client *Client
  14. indices []string
  15. pretty bool
  16. }
  17. func NewAliasesService(client *Client) *AliasesService {
  18. builder := &AliasesService{
  19. client: client,
  20. indices: make([]string, 0),
  21. }
  22. return builder
  23. }
  24. func (s *AliasesService) Pretty(pretty bool) *AliasesService {
  25. s.pretty = pretty
  26. return s
  27. }
  28. func (s *AliasesService) Index(indexName string) *AliasesService {
  29. s.indices = append(s.indices, indexName)
  30. return s
  31. }
  32. func (s *AliasesService) Indices(indexNames ...string) *AliasesService {
  33. s.indices = append(s.indices, indexNames...)
  34. return s
  35. }
  36. func (s *AliasesService) Do() (*AliasesResult, error) {
  37. var err error
  38. // Build url
  39. path := "/"
  40. // Indices part
  41. indexPart := make([]string, 0)
  42. for _, index := range s.indices {
  43. index, err = uritemplates.Expand("{index}", map[string]string{
  44. "index": index,
  45. })
  46. if err != nil {
  47. return nil, err
  48. }
  49. indexPart = append(indexPart, index)
  50. }
  51. path += strings.Join(indexPart, ",")
  52. // TODO Add types here
  53. // Search
  54. path += "/_aliases"
  55. // Parameters
  56. params := make(url.Values)
  57. if s.pretty {
  58. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  59. }
  60. // Get response
  61. res, err := s.client.PerformRequest("GET", path, params, nil)
  62. if err != nil {
  63. return nil, err
  64. }
  65. // {
  66. // "indexName" : {
  67. // "aliases" : {
  68. // "alias1" : { },
  69. // "alias2" : { }
  70. // }
  71. // },
  72. // "indexName2" : {
  73. // ...
  74. // },
  75. // }
  76. indexMap := make(map[string]interface{})
  77. if err := json.Unmarshal(res.Body, &indexMap); err != nil {
  78. return nil, err
  79. }
  80. // Each (indexName, _)
  81. ret := &AliasesResult{
  82. Indices: make(map[string]indexResult),
  83. }
  84. for indexName, indexData := range indexMap {
  85. indexOut, found := ret.Indices[indexName]
  86. if !found {
  87. indexOut = indexResult{Aliases: make([]aliasResult, 0)}
  88. }
  89. // { "aliases" : { ... } }
  90. indexDataMap, ok := indexData.(map[string]interface{})
  91. if ok {
  92. aliasesData, ok := indexDataMap["aliases"].(map[string]interface{})
  93. if ok {
  94. for aliasName, _ := range aliasesData {
  95. aliasRes := aliasResult{AliasName: aliasName}
  96. indexOut.Aliases = append(indexOut.Aliases, aliasRes)
  97. }
  98. }
  99. }
  100. ret.Indices[indexName] = indexOut
  101. }
  102. return ret, nil
  103. }
  104. // -- Result of an alias request.
  105. type AliasesResult struct {
  106. Indices map[string]indexResult
  107. }
  108. type indexResult struct {
  109. Aliases []aliasResult
  110. }
  111. type aliasResult struct {
  112. AliasName string
  113. }
  114. func (ar AliasesResult) IndicesByAlias(aliasName string) []string {
  115. indices := make([]string, 0)
  116. for indexName, indexInfo := range ar.Indices {
  117. for _, aliasInfo := range indexInfo.Aliases {
  118. if aliasInfo.AliasName == aliasName {
  119. indices = append(indices, indexName)
  120. }
  121. }
  122. }
  123. return indices
  124. }
  125. func (ir indexResult) HasAlias(aliasName string) bool {
  126. for _, alias := range ir.Aliases {
  127. if alias.AliasName == aliasName {
  128. return true
  129. }
  130. }
  131. return false
  132. }