Debian Codegraph @

bulk_update_request.go

raw
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_update_request.go
8
9 import (
10 "encoding/json"
11 "fmt"
12 "strings"
13 )
14
15 // BulkUpdateRequest is a request to update a document in Elasticsearch.
16 //
17 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-bulk.html
18 // for details.
19 type BulkUpdateRequest struct {
20 BulkableRequest
21 index string
22 typ string
23 id string
24
25 routing string
26 parent string
27 script *Script
28 scriptedUpsert *bool
29 version int64 // default is MATCH_ANY
30 versionType string // default is "internal"
31 retryOnConflict *int
32 upsert interface{}
33 docAsUpsert *bool
34 detectNoop *bool
35 doc interface{}
36 returnSource *bool
37
38 source []string
39
40 useEasyJSON bool
41 }
42
43 //easyjson:json
44 type bulkUpdateRequestCommand map[string]bulkUpdateRequestCommandOp
45
46 //easyjson:json
47 type bulkUpdateRequestCommandOp struct {
48 Id string `json:"_id,omitempty"`
49 Index string `json:"_index,omitempty"`
50 Type string `json:"_type,omitempty"`
51 Parent string `json:"_parent,omitempty"`
52 RetryOnConflict *int `json:"_retry_on_conflict,omitempty"`
53 Routing string `json:"_routing,omitempty"`
54 Version int64 `json:"_version,omitempty"`
55 VersionType string `json:"_version_type,omitempty"`
56 }
57
58 //easyjson:json
59 type bulkUpdateRequestCommandData struct {
60 DetectNoop *bool `json:"detect_noop,omitempty"`
61 Doc interface{} `json:"doc,omitempty"`
62 DocAsUpsert *bool `json:"doc_as_upsert,omitempty"`
63 Upsert interface{} `json:"upsert,omitempty"`
64 Script interface{} `json:"script,omitempty"`
65 ScriptedUpsert *bool `json:"scripted_upsert,omitempty"`
66 Source *bool `json:"_source,omitempty"`
67 }
68
69 // NewBulkUpdateRequest returns a new BulkUpdateRequest.
70 func NewBulkUpdateRequest() *BulkUpdateRequest {
71 return &BulkUpdateRequest{}
72 }
73
74 // UseEasyJSON is an experimental setting that enables serialization
75 // with github.com/mailru/easyjson, which should in faster serialization
76 // time and less allocations, but removed compatibility with encoding/json,
77 // usage of unsafe etc. See https://github.com/mailru/easyjson#issues-notes-and-limitations
78 // for details. This setting is disabled by default.
79 func (r *BulkUpdateRequest) UseEasyJSON(enable bool) *BulkUpdateRequest {
80 r.useEasyJSON = enable
81 return r
82 }
83
84 // Index specifies the Elasticsearch index to use for this update request.
85 // If unspecified, the index set on the BulkService will be used.
86 func (r *BulkUpdateRequest) Index(index string) *BulkUpdateRequest {
87 r.index = index
88 r.source = nil
89 return r
90 }
91
92 // Type specifies the Elasticsearch type to use for this update request.
93 // If unspecified, the type set on the BulkService will be used.
94 func (r *BulkUpdateRequest) Type(typ string) *BulkUpdateRequest {
95 r.typ = typ
96 r.source = nil
97 return r
98 }
99
100 // Id specifies the identifier of the document to update.
101 func (r *BulkUpdateRequest) Id(id string) *BulkUpdateRequest {
102 r.id = id
103 r.source = nil
104 return r
105 }
106
107 // Routing specifies a routing value for the request.
108 func (r *BulkUpdateRequest) Routing(routing string) *BulkUpdateRequest {
109 r.routing = routing
110 r.source = nil
111 return r
112 }
113
114 // Parent specifies the identifier of the parent document (if available).
115 func (r *BulkUpdateRequest) Parent(parent string) *BulkUpdateRequest {
116 r.parent = parent
117 r.source = nil
118 return r
119 }
120
121 // Script specifies an update script.
122 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-bulk.html#bulk-update
123 // and https://www.elastic.co/guide/en/elasticsearch/reference/5.2/modules-scripting.html
124 // for details.
125 func (r *BulkUpdateRequest) Script(script *Script) *BulkUpdateRequest {
126 r.script = script
127 r.source = nil
128 return r
129 }
130
131 // ScripedUpsert specifies if your script will run regardless of
132 // whether the document exists or not.
133 //
134 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-update.html#_literal_scripted_upsert_literal
135 func (r *BulkUpdateRequest) ScriptedUpsert(upsert bool) *BulkUpdateRequest {
136 r.scriptedUpsert = &upsert
137 r.source = nil
138 return r
139 }
140
141 // RetryOnConflict specifies how often to retry in case of a version conflict.
142 func (r *BulkUpdateRequest) RetryOnConflict(retryOnConflict int) *BulkUpdateRequest {
143 r.retryOnConflict = &retryOnConflict
144 r.source = nil
145 return r
146 }
147
148 // Version indicates the version of the document as part of an optimistic
149 // concurrency model.
150 func (r *BulkUpdateRequest) Version(version int64) *BulkUpdateRequest {
151 r.version = version
152 r.source = nil
153 return r
154 }
155
156 // VersionType can be "internal" (default), "external", "external_gte",
157 // "external_gt", or "force".
158 func (r *BulkUpdateRequest) VersionType(versionType string) *BulkUpdateRequest {
159 r.versionType = versionType
160 r.source = nil
161 return r
162 }
163
164 // Doc specifies the updated document.
165 func (r *BulkUpdateRequest) Doc(doc interface{}) *BulkUpdateRequest {
166 r.doc = doc
167 r.source = nil
168 return r
169 }
170
171 // DocAsUpsert indicates whether the contents of Doc should be used as
172 // the Upsert value.
173 //
174 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-update.html#_literal_doc_as_upsert_literal
175 // for details.
176 func (r *BulkUpdateRequest) DocAsUpsert(docAsUpsert bool) *BulkUpdateRequest {
177 r.docAsUpsert = &docAsUpsert
178 r.source = nil
179 return r
180 }
181
182 // DetectNoop specifies whether changes that don't affect the document
183 // should be ignored (true) or unignored (false). This is enabled by default
184 // in Elasticsearch.
185 func (r *BulkUpdateRequest) DetectNoop(detectNoop bool) *BulkUpdateRequest {
186 r.detectNoop = &detectNoop
187 r.source = nil
188 return r
189 }
190
191 // Upsert specifies the document to use for upserts. It will be used for
192 // create if the original document does not exist.
193 func (r *BulkUpdateRequest) Upsert(doc interface{}) *BulkUpdateRequest {
194 r.upsert = doc
195 r.source = nil
196 return r
197 }
198
199 // ReturnSource specifies whether Elasticsearch should return the source
200 // after the update. In the request, this responds to the `_source` field.
201 // It is false by default.
202 func (r *BulkUpdateRequest) ReturnSource(source bool) *BulkUpdateRequest {
203 r.returnSource = &source
204 r.source = nil
205 return r
206 }
207
208 // String returns the on-wire representation of the update request,
209 // concatenated as a single string.
210 func (r *BulkUpdateRequest) String() string {
211 lines, err := r.Source()
212 if err != nil {
213 return fmt.Sprintf("error: %v", err)
214 }
215 return strings.Join(lines, "\n")
216 }
217
218 // Source returns the on-wire representation of the update request,
219 // split into an action-and-meta-data line and an (optional) source line.
220 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-bulk.html
221 // for details.
222 func (r *BulkUpdateRequest) Source() ([]string, error) {
223 // { "update" : { "_index" : "test", "_type" : "type1", "_id" : "1", ... } }
224 // { "doc" : { "field1" : "value1", ... } }
225 // or
226 // { "update" : { "_index" : "test", "_type" : "type1", "_id" : "1", ... } }
227 // { "script" : { ... } }
228
229 if r.source != nil {
230 return r.source, nil
231 }
232
233 lines := make([]string, 2)
234
235 // "update" ...
236 updateCommand := bulkUpdateRequestCommandOp{
237 Index: r.index,
238 Type: r.typ,
239 Id: r.id,
240 Routing: r.routing,
241 Parent: r.parent,
242 Version: r.version,
243 VersionType: r.versionType,
244 RetryOnConflict: r.retryOnConflict,
245 }
246 command := bulkUpdateRequestCommand{
247 "update": updateCommand,
248 }
249
250 var err error
251 var body []byte
252 if r.useEasyJSON {
253 // easyjson
254 body, err = command.MarshalJSON()
255 } else {
256 // encoding/json
257 body, err = json.Marshal(command)
258 }
259 if err != nil {
260 return nil, err
261 }
262
263 lines[0] = string(body)
264
265 // 2nd line: {"doc" : { ... }} or {"script": {...}}
266 data := bulkUpdateRequestCommandData{
267 DocAsUpsert: r.docAsUpsert,
268 DetectNoop: r.detectNoop,
269 Upsert: r.upsert,
270 ScriptedUpsert: r.scriptedUpsert,
271 Doc: r.doc,
272 Source: r.returnSource,
273 }
274 if r.script != nil {
275 script, err := r.script.Source()
276 if err != nil {
277 return nil, err
278 }
279 data.Script = script
280 }
281
282 if r.useEasyJSON {
283 // easyjson
284 body, err = data.MarshalJSON()
285 } else {
286 // encoding/json
287 body, err = json.Marshal(data)
288 }
289 if err != nil {
290 return nil, err
291 }
292
293 lines[1] = string(body)
294
295 r.source = lines
296 return lines, nil
297 }