alias.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. )
  10. type AliasService struct {
  11. client *Client
  12. actions []aliasAction
  13. pretty bool
  14. }
  15. type aliasAction struct {
  16. // "add" or "remove"
  17. Type string
  18. // Index name
  19. Index string
  20. // Alias name
  21. Alias string
  22. // Filter
  23. Filter *Filter
  24. }
  25. func NewAliasService(client *Client) *AliasService {
  26. builder := &AliasService{
  27. client: client,
  28. actions: make([]aliasAction, 0),
  29. }
  30. return builder
  31. }
  32. func (s *AliasService) Pretty(pretty bool) *AliasService {
  33. s.pretty = pretty
  34. return s
  35. }
  36. func (s *AliasService) Add(indexName string, aliasName string) *AliasService {
  37. action := aliasAction{Type: "add", Index: indexName, Alias: aliasName}
  38. s.actions = append(s.actions, action)
  39. return s
  40. }
  41. func (s *AliasService) AddWithFilter(indexName string, aliasName string, filter *Filter) *AliasService {
  42. action := aliasAction{Type: "add", Index: indexName, Alias: aliasName, Filter: filter}
  43. s.actions = append(s.actions, action)
  44. return s
  45. }
  46. func (s *AliasService) Remove(indexName string, aliasName string) *AliasService {
  47. action := aliasAction{Type: "remove", Index: indexName, Alias: aliasName}
  48. s.actions = append(s.actions, action)
  49. return s
  50. }
  51. func (s *AliasService) Do() (*AliasResult, error) {
  52. // Build url
  53. path := "/_aliases"
  54. // Parameters
  55. params := make(url.Values)
  56. if s.pretty {
  57. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  58. }
  59. // Actions
  60. body := make(map[string]interface{})
  61. actionsJson := make([]interface{}, 0)
  62. for _, action := range s.actions {
  63. actionJson := make(map[string]interface{})
  64. detailsJson := make(map[string]interface{})
  65. detailsJson["index"] = action.Index
  66. detailsJson["alias"] = action.Alias
  67. if action.Filter != nil {
  68. detailsJson["filter"] = (*action.Filter).Source()
  69. }
  70. actionJson[action.Type] = detailsJson
  71. actionsJson = append(actionsJson, actionJson)
  72. }
  73. body["actions"] = actionsJson
  74. // Get response
  75. res, err := s.client.PerformRequest("POST", path, params, body)
  76. if err != nil {
  77. return nil, err
  78. }
  79. // Return results
  80. ret := new(AliasResult)
  81. if err := json.Unmarshal(res.Body, ret); err != nil {
  82. return nil, err
  83. }
  84. return ret, nil
  85. }
  86. // -- Result of an alias request.
  87. type AliasResult struct {
  88. Acknowledged bool `json:"acknowledged"`
  89. }