indices_delete.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // IndicesDeleteService allows to delete existing indices.
  13. //
  14. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-delete-index.html
  15. // for details.
  16. type IndicesDeleteService struct {
  17. client *Client
  18. pretty bool
  19. index []string
  20. timeout string
  21. masterTimeout string
  22. }
  23. // NewIndicesDeleteService creates and initializes a new IndicesDeleteService.
  24. func NewIndicesDeleteService(client *Client) *IndicesDeleteService {
  25. return &IndicesDeleteService{
  26. client: client,
  27. index: make([]string, 0),
  28. }
  29. }
  30. // Index adds the list of indices to delete.
  31. // Use `_all` or `*` string to delete all indices.
  32. func (s *IndicesDeleteService) Index(index []string) *IndicesDeleteService {
  33. s.index = index
  34. return s
  35. }
  36. // Timeout is an explicit operation timeout.
  37. func (s *IndicesDeleteService) Timeout(timeout string) *IndicesDeleteService {
  38. s.timeout = timeout
  39. return s
  40. }
  41. // MasterTimeout specifies the timeout for connection to master.
  42. func (s *IndicesDeleteService) MasterTimeout(masterTimeout string) *IndicesDeleteService {
  43. s.masterTimeout = masterTimeout
  44. return s
  45. }
  46. // Pretty indicates that the JSON response be indented and human readable.
  47. func (s *IndicesDeleteService) Pretty(pretty bool) *IndicesDeleteService {
  48. s.pretty = pretty
  49. return s
  50. }
  51. // buildURL builds the URL for the operation.
  52. func (s *IndicesDeleteService) buildURL() (string, url.Values, error) {
  53. // Build URL
  54. path, err := uritemplates.Expand("/{index}", map[string]string{
  55. "index": strings.Join(s.index, ","),
  56. })
  57. if err != nil {
  58. return "", url.Values{}, err
  59. }
  60. // Add query string parameters
  61. params := url.Values{}
  62. if s.pretty {
  63. params.Set("pretty", "1")
  64. }
  65. if s.timeout != "" {
  66. params.Set("timeout", s.timeout)
  67. }
  68. if s.masterTimeout != "" {
  69. params.Set("master_timeout", s.masterTimeout)
  70. }
  71. return path, params, nil
  72. }
  73. // Validate checks if the operation is valid.
  74. func (s *IndicesDeleteService) Validate() error {
  75. var invalid []string
  76. if len(s.index) == 0 {
  77. invalid = append(invalid, "Index")
  78. }
  79. if len(invalid) > 0 {
  80. return fmt.Errorf("missing required fields: %v", invalid)
  81. }
  82. return nil
  83. }
  84. // Do executes the operation.
  85. func (s *IndicesDeleteService) Do(ctx context.Context) (*IndicesDeleteResponse, error) {
  86. // Check pre-conditions
  87. if err := s.Validate(); err != nil {
  88. return nil, err
  89. }
  90. // Get URL for request
  91. path, params, err := s.buildURL()
  92. if err != nil {
  93. return nil, err
  94. }
  95. // Get HTTP response
  96. res, err := s.client.PerformRequest(ctx, "DELETE", path, params, nil)
  97. if err != nil {
  98. return nil, err
  99. }
  100. // Return operation response
  101. ret := new(IndicesDeleteResponse)
  102. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  103. return nil, err
  104. }
  105. return ret, nil
  106. }
  107. // -- Result of a delete index request.
  108. // IndicesDeleteResponse is the response of IndicesDeleteService.Do.
  109. type IndicesDeleteResponse struct {
  110. Acknowledged bool `json:"acknowledged"`
  111. }