delete.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "github.com/olivere/elastic/uritemplates"
  10. )
  11. type DeleteService struct {
  12. client *Client
  13. index string
  14. _type string
  15. id string
  16. routing string
  17. refresh *bool
  18. version *int
  19. pretty bool
  20. }
  21. func NewDeleteService(client *Client) *DeleteService {
  22. builder := &DeleteService{
  23. client: client,
  24. }
  25. return builder
  26. }
  27. func (s *DeleteService) Index(index string) *DeleteService {
  28. s.index = index
  29. return s
  30. }
  31. func (s *DeleteService) Type(_type string) *DeleteService {
  32. s._type = _type
  33. return s
  34. }
  35. func (s *DeleteService) Id(id string) *DeleteService {
  36. s.id = id
  37. return s
  38. }
  39. func (s *DeleteService) Parent(parent string) *DeleteService {
  40. if s.routing == "" {
  41. s.routing = parent
  42. }
  43. return s
  44. }
  45. func (s *DeleteService) Refresh(refresh bool) *DeleteService {
  46. s.refresh = &refresh
  47. return s
  48. }
  49. func (s *DeleteService) Version(version int) *DeleteService {
  50. s.version = &version
  51. return s
  52. }
  53. func (s *DeleteService) Pretty(pretty bool) *DeleteService {
  54. s.pretty = pretty
  55. return s
  56. }
  57. func (s *DeleteService) Do() (*DeleteResult, error) {
  58. // Build url
  59. path, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
  60. "index": s.index,
  61. "type": s._type,
  62. "id": s.id,
  63. })
  64. if err != nil {
  65. return nil, err
  66. }
  67. // Parameters
  68. params := make(url.Values)
  69. if s.refresh != nil {
  70. params.Set("refresh", fmt.Sprintf("%v", *s.refresh))
  71. }
  72. if s.version != nil {
  73. params.Set("version", fmt.Sprintf("%d", *s.version))
  74. }
  75. if s.routing != "" {
  76. params.Set("routing", fmt.Sprintf("%s", s.routing))
  77. }
  78. if s.pretty {
  79. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  80. }
  81. // Get response
  82. res, err := s.client.PerformRequest("DELETE", path, params, nil)
  83. if err != nil {
  84. return nil, err
  85. }
  86. // Return response
  87. ret := new(DeleteResult)
  88. if err := json.Unmarshal(res.Body, ret); err != nil {
  89. return nil, err
  90. }
  91. return ret, nil
  92. }
  93. // -- Result of a delete request.
  94. type DeleteResult struct {
  95. Found bool `json:"found"`
  96. Index string `json:"_index"`
  97. Type string `json:"_type"`
  98. Id string `json:"_id"`
  99. Version int64 `json:"_version"`
  100. }