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