bulk_delete_request.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. //go:generate easyjson bulk_delete_request.go
  6. import (
  7. "encoding/json"
  8. "fmt"
  9. "strings"
  10. )
  11. // -- Bulk delete request --
  12. // BulkDeleteRequest is a request to remove a document from Elasticsearch.
  13. //
  14. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-bulk.html
  15. // for details.
  16. type BulkDeleteRequest struct {
  17. BulkableRequest
  18. index string
  19. typ string
  20. id string
  21. parent string
  22. routing string
  23. version int64 // default is MATCH_ANY
  24. versionType string // default is "internal"
  25. source []string
  26. useEasyJSON bool
  27. }
  28. //easyjson:json
  29. type bulkDeleteRequestCommand map[string]bulkDeleteRequestCommandOp
  30. //easyjson:json
  31. type bulkDeleteRequestCommandOp struct {
  32. Id string `json:"_id,omitempty"`
  33. Index string `json:"_index,omitempty"`
  34. Parent string `json:"_parent,omitempty"`
  35. Routing string `json:"_routing,omitempty"`
  36. Type string `json:"_type,omitempty"`
  37. Version int64 `json:"_version,omitempty"`
  38. VersionType string `json:"_version_type,omitempty"`
  39. }
  40. // NewBulkDeleteRequest returns a new BulkDeleteRequest.
  41. func NewBulkDeleteRequest() *BulkDeleteRequest {
  42. return &BulkDeleteRequest{}
  43. }
  44. // UseEasyJSON is an experimental setting that enables serialization
  45. // with github.com/mailru/easyjson, which should in faster serialization
  46. // time and less allocations, but removed compatibility with encoding/json,
  47. // usage of unsafe etc. See https://github.com/mailru/easyjson#issues-notes-and-limitations
  48. // for details. This setting is disabled by default.
  49. func (r *BulkDeleteRequest) UseEasyJSON(enable bool) *BulkDeleteRequest {
  50. r.useEasyJSON = enable
  51. return r
  52. }
  53. // Index specifies the Elasticsearch index to use for this delete request.
  54. // If unspecified, the index set on the BulkService will be used.
  55. func (r *BulkDeleteRequest) Index(index string) *BulkDeleteRequest {
  56. r.index = index
  57. r.source = nil
  58. return r
  59. }
  60. // Type specifies the Elasticsearch type to use for this delete request.
  61. // If unspecified, the type set on the BulkService will be used.
  62. func (r *BulkDeleteRequest) Type(typ string) *BulkDeleteRequest {
  63. r.typ = typ
  64. r.source = nil
  65. return r
  66. }
  67. // Id specifies the identifier of the document to delete.
  68. func (r *BulkDeleteRequest) Id(id string) *BulkDeleteRequest {
  69. r.id = id
  70. r.source = nil
  71. return r
  72. }
  73. // Parent specifies the parent of the request, which is used in parent/child
  74. // mappings.
  75. func (r *BulkDeleteRequest) Parent(parent string) *BulkDeleteRequest {
  76. r.parent = parent
  77. r.source = nil
  78. return r
  79. }
  80. // Routing specifies a routing value for the request.
  81. func (r *BulkDeleteRequest) Routing(routing string) *BulkDeleteRequest {
  82. r.routing = routing
  83. r.source = nil
  84. return r
  85. }
  86. // Version indicates the version to be deleted as part of an optimistic
  87. // concurrency model.
  88. func (r *BulkDeleteRequest) Version(version int64) *BulkDeleteRequest {
  89. r.version = version
  90. r.source = nil
  91. return r
  92. }
  93. // VersionType can be "internal" (default), "external", "external_gte",
  94. // "external_gt", or "force".
  95. func (r *BulkDeleteRequest) VersionType(versionType string) *BulkDeleteRequest {
  96. r.versionType = versionType
  97. r.source = nil
  98. return r
  99. }
  100. // String returns the on-wire representation of the delete request,
  101. // concatenated as a single string.
  102. func (r *BulkDeleteRequest) String() string {
  103. lines, err := r.Source()
  104. if err != nil {
  105. return fmt.Sprintf("error: %v", err)
  106. }
  107. return strings.Join(lines, "\n")
  108. }
  109. // Source returns the on-wire representation of the delete request,
  110. // split into an action-and-meta-data line and an (optional) source line.
  111. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-bulk.html
  112. // for details.
  113. func (r *BulkDeleteRequest) Source() ([]string, error) {
  114. if r.source != nil {
  115. return r.source, nil
  116. }
  117. command := bulkDeleteRequestCommand{
  118. "delete": bulkDeleteRequestCommandOp{
  119. Index: r.index,
  120. Type: r.typ,
  121. Id: r.id,
  122. Routing: r.routing,
  123. Parent: r.parent,
  124. Version: r.version,
  125. VersionType: r.versionType,
  126. },
  127. }
  128. var err error
  129. var body []byte
  130. if r.useEasyJSON {
  131. // easyjson
  132. body, err = command.MarshalJSON()
  133. } else {
  134. // encoding/json
  135. body, err = json.Marshal(command)
  136. }
  137. if err != nil {
  138. return nil, err
  139. }
  140. lines := []string{string(body)}
  141. r.source = lines
  142. return lines, nil
  143. }