delete_mapping.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "log"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/uritemplates"
  12. )
  13. var (
  14. _ = fmt.Print
  15. _ = log.Print
  16. _ = strings.Index
  17. _ = uritemplates.Expand
  18. _ = url.Parse
  19. )
  20. // DeleteMappingService allows to delete a mapping along with its data.
  21. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-mapping.html.
  22. type DeleteMappingService struct {
  23. client *Client
  24. pretty bool
  25. index []string
  26. typ []string
  27. masterTimeout string
  28. }
  29. // NewDeleteMappingService creates a new DeleteMappingService.
  30. func NewDeleteMappingService(client *Client) *DeleteMappingService {
  31. return &DeleteMappingService{
  32. client: client,
  33. index: make([]string, 0),
  34. typ: make([]string, 0),
  35. }
  36. }
  37. // Index is a list of index names (supports wildcards). Use `_all` for all indices.
  38. func (s *DeleteMappingService) Index(index ...string) *DeleteMappingService {
  39. s.index = append(s.index, index...)
  40. return s
  41. }
  42. // Type is a list of document types to delete (supports wildcards).
  43. // Use `_all` to delete all document types in the specified indices..
  44. func (s *DeleteMappingService) Type(typ ...string) *DeleteMappingService {
  45. s.typ = append(s.typ, typ...)
  46. return s
  47. }
  48. // MasterTimeout specifies the timeout for connecting to master.
  49. func (s *DeleteMappingService) MasterTimeout(masterTimeout string) *DeleteMappingService {
  50. s.masterTimeout = masterTimeout
  51. return s
  52. }
  53. // Pretty indicates that the JSON response be indented and human readable.
  54. func (s *DeleteMappingService) Pretty(pretty bool) *DeleteMappingService {
  55. s.pretty = pretty
  56. return s
  57. }
  58. // buildURL builds the URL for the operation.
  59. func (s *DeleteMappingService) buildURL() (string, url.Values, error) {
  60. // Build URL
  61. path, err := uritemplates.Expand("/{index}/_mapping/{type}", map[string]string{
  62. "index": strings.Join(s.index, ","),
  63. "type": strings.Join(s.typ, ","),
  64. })
  65. if err != nil {
  66. return "", url.Values{}, err
  67. }
  68. // Add query string parameters
  69. params := url.Values{}
  70. if s.pretty {
  71. params.Set("pretty", "1")
  72. }
  73. if s.masterTimeout != "" {
  74. params.Set("master_timeout", s.masterTimeout)
  75. }
  76. return path, params, nil
  77. }
  78. // Validate checks if the operation is valid.
  79. func (s *DeleteMappingService) Validate() error {
  80. var invalid []string
  81. if len(s.index) == 0 {
  82. invalid = append(invalid, "Index")
  83. }
  84. if len(s.typ) == 0 {
  85. invalid = append(invalid, "Type")
  86. }
  87. if len(invalid) > 0 {
  88. return fmt.Errorf("missing required fields: %v", invalid)
  89. }
  90. return nil
  91. }
  92. // Do executes the operation.
  93. func (s *DeleteMappingService) Do() (*DeleteMappingResponse, error) {
  94. // Check pre-conditions
  95. if err := s.Validate(); err != nil {
  96. return nil, err
  97. }
  98. // Get URL for request
  99. path, params, err := s.buildURL()
  100. if err != nil {
  101. return nil, err
  102. }
  103. // Get HTTP response
  104. res, err := s.client.PerformRequest("DELETE", path, params, nil)
  105. if err != nil {
  106. return nil, err
  107. }
  108. // Return operation response
  109. ret := new(DeleteMappingResponse)
  110. if err := json.Unmarshal(res.Body, ret); err != nil {
  111. return nil, err
  112. }
  113. return ret, nil
  114. }
  115. // DeleteMappingResponse is the response of DeleteMappingService.Do.
  116. type DeleteMappingResponse struct {
  117. Acknowledged bool `json:"acknowledged"`
  118. }