-
Notifications
You must be signed in to change notification settings - Fork 627
/
Copy pathapi.delete_by_query.go
615 lines (517 loc) · 15.5 KB
/
api.delete_by_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// Code generated from specification version 8.0.0: DO NOT EDIT
package esapi
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
func newDeleteByQueryFunc(t Transport) DeleteByQuery {
return func(index []string, body io.Reader, o ...func(*DeleteByQueryRequest)) (*Response, error) {
var r = DeleteByQueryRequest{Index: index, Body: body}
for _, f := range o {
f(&r)
}
return r.Do(r.ctx, t)
}
}
// ----- API Definition -------------------------------------------------------
// DeleteByQuery deletes documents matching the provided query.
//
// See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-delete-by-query.html.
//
type DeleteByQuery func(index []string, body io.Reader, o ...func(*DeleteByQueryRequest)) (*Response, error)
// DeleteByQueryRequest configures the Delete By Query API request.
//
type DeleteByQueryRequest struct {
Index []string
Body io.Reader
AllowNoIndices *bool
Analyzer string
AnalyzeWildcard *bool
Conflicts string
DefaultOperator string
Df string
ExpandWildcards string
From *int
IgnoreUnavailable *bool
Lenient *bool
MaxDocs *int
Preference string
Query string
Refresh *bool
RequestCache *bool
RequestsPerSecond *int
Routing []string
Scroll time.Duration
ScrollSize *int
SearchTimeout time.Duration
SearchType string
Slices interface{}
Sort []string
Source []string
SourceExcludes []string
SourceIncludes []string
Stats []string
TerminateAfter *int
Timeout time.Duration
Version *bool
WaitForActiveShards string
WaitForCompletion *bool
Pretty bool
Human bool
ErrorTrace bool
FilterPath []string
Header http.Header
ctx context.Context
}
// Do executes the request and returns response or error.
//
func (r DeleteByQueryRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
var (
method string
path strings.Builder
params map[string]string
)
method = "POST"
path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_delete_by_query"))
path.WriteString("/")
path.WriteString(strings.Join(r.Index, ","))
path.WriteString("/")
path.WriteString("_delete_by_query")
params = make(map[string]string)
if r.AllowNoIndices != nil {
params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
}
if r.Analyzer != "" {
params["analyzer"] = r.Analyzer
}
if r.AnalyzeWildcard != nil {
params["analyze_wildcard"] = strconv.FormatBool(*r.AnalyzeWildcard)
}
if r.Conflicts != "" {
params["conflicts"] = r.Conflicts
}
if r.DefaultOperator != "" {
params["default_operator"] = r.DefaultOperator
}
if r.Df != "" {
params["df"] = r.Df
}
if r.ExpandWildcards != "" {
params["expand_wildcards"] = r.ExpandWildcards
}
if r.From != nil {
params["from"] = strconv.FormatInt(int64(*r.From), 10)
}
if r.IgnoreUnavailable != nil {
params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
}
if r.Lenient != nil {
params["lenient"] = strconv.FormatBool(*r.Lenient)
}
if r.MaxDocs != nil {
params["max_docs"] = strconv.FormatInt(int64(*r.MaxDocs), 10)
}
if r.Preference != "" {
params["preference"] = r.Preference
}
if r.Query != "" {
params["q"] = r.Query
}
if r.Refresh != nil {
params["refresh"] = strconv.FormatBool(*r.Refresh)
}
if r.RequestCache != nil {
params["request_cache"] = strconv.FormatBool(*r.RequestCache)
}
if r.RequestsPerSecond != nil {
params["requests_per_second"] = strconv.FormatInt(int64(*r.RequestsPerSecond), 10)
}
if len(r.Routing) > 0 {
params["routing"] = strings.Join(r.Routing, ",")
}
if r.Scroll != 0 {
params["scroll"] = formatDuration(r.Scroll)
}
if r.ScrollSize != nil {
params["scroll_size"] = strconv.FormatInt(int64(*r.ScrollSize), 10)
}
if r.SearchTimeout != 0 {
params["search_timeout"] = formatDuration(r.SearchTimeout)
}
if r.SearchType != "" {
params["search_type"] = r.SearchType
}
if r.Slices != nil {
params["slices"] = fmt.Sprintf("%v", r.Slices)
}
if len(r.Sort) > 0 {
params["sort"] = strings.Join(r.Sort, ",")
}
if len(r.Source) > 0 {
params["_source"] = strings.Join(r.Source, ",")
}
if len(r.SourceExcludes) > 0 {
params["_source_excludes"] = strings.Join(r.SourceExcludes, ",")
}
if len(r.SourceIncludes) > 0 {
params["_source_includes"] = strings.Join(r.SourceIncludes, ",")
}
if len(r.Stats) > 0 {
params["stats"] = strings.Join(r.Stats, ",")
}
if r.TerminateAfter != nil {
params["terminate_after"] = strconv.FormatInt(int64(*r.TerminateAfter), 10)
}
if r.Timeout != 0 {
params["timeout"] = formatDuration(r.Timeout)
}
if r.Version != nil {
params["version"] = strconv.FormatBool(*r.Version)
}
if r.WaitForActiveShards != "" {
params["wait_for_active_shards"] = r.WaitForActiveShards
}
if r.WaitForCompletion != nil {
params["wait_for_completion"] = strconv.FormatBool(*r.WaitForCompletion)
}
if r.Pretty {
params["pretty"] = "true"
}
if r.Human {
params["human"] = "true"
}
if r.ErrorTrace {
params["error_trace"] = "true"
}
if len(r.FilterPath) > 0 {
params["filter_path"] = strings.Join(r.FilterPath, ",")
}
req, err := newRequest(method, path.String(), r.Body)
if err != nil {
return nil, err
}
if len(params) > 0 {
q := req.URL.Query()
for k, v := range params {
q.Set(k, v)
}
req.URL.RawQuery = q.Encode()
}
if r.Body != nil {
req.Header[headerContentType] = headerContentTypeJSON
}
if len(r.Header) > 0 {
if len(req.Header) == 0 {
req.Header = r.Header
} else {
for k, vv := range r.Header {
for _, v := range vv {
req.Header.Add(k, v)
}
}
}
}
if ctx != nil {
req = req.WithContext(ctx)
}
res, err := transport.Perform(req)
if err != nil {
return nil, err
}
response := Response{
StatusCode: res.StatusCode,
Body: res.Body,
Header: res.Header,
}
return &response, nil
}
// WithContext sets the request context.
//
func (f DeleteByQuery) WithContext(v context.Context) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.ctx = v
}
}
// WithAllowNoIndices - whether to ignore if a wildcard indices expression resolves into no concrete indices. (this includes `_all` string or when no indices have been specified).
//
func (f DeleteByQuery) WithAllowNoIndices(v bool) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.AllowNoIndices = &v
}
}
// WithAnalyzer - the analyzer to use for the query string.
//
func (f DeleteByQuery) WithAnalyzer(v string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Analyzer = v
}
}
// WithAnalyzeWildcard - specify whether wildcard and prefix queries should be analyzed (default: false).
//
func (f DeleteByQuery) WithAnalyzeWildcard(v bool) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.AnalyzeWildcard = &v
}
}
// WithConflicts - what to do when the delete by query hits version conflicts?.
//
func (f DeleteByQuery) WithConflicts(v string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Conflicts = v
}
}
// WithDefaultOperator - the default operator for query string query (and or or).
//
func (f DeleteByQuery) WithDefaultOperator(v string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.DefaultOperator = v
}
}
// WithDf - the field to use as default where no field prefix is given in the query string.
//
func (f DeleteByQuery) WithDf(v string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Df = v
}
}
// WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
//
func (f DeleteByQuery) WithExpandWildcards(v string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.ExpandWildcards = v
}
}
// WithFrom - starting offset (default: 0).
//
func (f DeleteByQuery) WithFrom(v int) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.From = &v
}
}
// WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
//
func (f DeleteByQuery) WithIgnoreUnavailable(v bool) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.IgnoreUnavailable = &v
}
}
// WithLenient - specify whether format-based query failures (such as providing text to a numeric field) should be ignored.
//
func (f DeleteByQuery) WithLenient(v bool) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Lenient = &v
}
}
// WithMaxDocs - maximum number of documents to process (default: all documents).
//
func (f DeleteByQuery) WithMaxDocs(v int) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.MaxDocs = &v
}
}
// WithPreference - specify the node or shard the operation should be performed on (default: random).
//
func (f DeleteByQuery) WithPreference(v string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Preference = v
}
}
// WithQuery - query in the lucene query string syntax.
//
func (f DeleteByQuery) WithQuery(v string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Query = v
}
}
// WithRefresh - should the affected indexes be refreshed?.
//
func (f DeleteByQuery) WithRefresh(v bool) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Refresh = &v
}
}
// WithRequestCache - specify if request cache should be used for this request or not, defaults to index level setting.
//
func (f DeleteByQuery) WithRequestCache(v bool) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.RequestCache = &v
}
}
// WithRequestsPerSecond - the throttle for this request in sub-requests per second. -1 means no throttle..
//
func (f DeleteByQuery) WithRequestsPerSecond(v int) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.RequestsPerSecond = &v
}
}
// WithRouting - a list of specific routing values.
//
func (f DeleteByQuery) WithRouting(v ...string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Routing = v
}
}
// WithScroll - specify how long a consistent view of the index should be maintained for scrolled search.
//
func (f DeleteByQuery) WithScroll(v time.Duration) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Scroll = v
}
}
// WithScrollSize - size on the scroll request powering the delete by query.
//
func (f DeleteByQuery) WithScrollSize(v int) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.ScrollSize = &v
}
}
// WithSearchTimeout - explicit timeout for each search request. defaults to no timeout..
//
func (f DeleteByQuery) WithSearchTimeout(v time.Duration) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.SearchTimeout = v
}
}
// WithSearchType - search operation type.
//
func (f DeleteByQuery) WithSearchType(v string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.SearchType = v
}
}
// WithSlices - the number of slices this task should be divided into. defaults to 1, meaning the task isn't sliced into subtasks. can be set to `auto`..
//
func (f DeleteByQuery) WithSlices(v interface{}) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Slices = v
}
}
// WithSort - a list of <field>:<direction> pairs.
//
func (f DeleteByQuery) WithSort(v ...string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Sort = v
}
}
// WithSource - true or false to return the _source field or not, or a list of fields to return.
//
func (f DeleteByQuery) WithSource(v ...string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Source = v
}
}
// WithSourceExcludes - a list of fields to exclude from the returned _source field.
//
func (f DeleteByQuery) WithSourceExcludes(v ...string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.SourceExcludes = v
}
}
// WithSourceIncludes - a list of fields to extract and return from the _source field.
//
func (f DeleteByQuery) WithSourceIncludes(v ...string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.SourceIncludes = v
}
}
// WithStats - specific 'tag' of the request for logging and statistical purposes.
//
func (f DeleteByQuery) WithStats(v ...string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Stats = v
}
}
// WithTerminateAfter - the maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early..
//
func (f DeleteByQuery) WithTerminateAfter(v int) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.TerminateAfter = &v
}
}
// WithTimeout - time each individual bulk request should wait for shards that are unavailable..
//
func (f DeleteByQuery) WithTimeout(v time.Duration) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Timeout = v
}
}
// WithVersion - specify whether to return document version as part of a hit.
//
func (f DeleteByQuery) WithVersion(v bool) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Version = &v
}
}
// WithWaitForActiveShards - sets the number of shard copies that must be active before proceeding with the delete by query operation. defaults to 1, meaning the primary shard only. set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1).
//
func (f DeleteByQuery) WithWaitForActiveShards(v string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.WaitForActiveShards = v
}
}
// WithWaitForCompletion - should the request should block until the delete by query is complete..
//
func (f DeleteByQuery) WithWaitForCompletion(v bool) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.WaitForCompletion = &v
}
}
// WithPretty makes the response body pretty-printed.
//
func (f DeleteByQuery) WithPretty() func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Pretty = true
}
}
// WithHuman makes statistical values human-readable.
//
func (f DeleteByQuery) WithHuman() func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Human = true
}
}
// WithErrorTrace includes the stack trace for errors in the response body.
//
func (f DeleteByQuery) WithErrorTrace() func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.ErrorTrace = true
}
}
// WithFilterPath filters the properties of the response body.
//
func (f DeleteByQuery) WithFilterPath(v ...string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.FilterPath = v
}
}
// WithHeader adds the headers to the HTTP request.
//
func (f DeleteByQuery) WithHeader(h map[string]string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
if r.Header == nil {
r.Header = make(http.Header)
}
for k, v := range h {
r.Header.Add(k, v)
}
}
}
// WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
//
func (f DeleteByQuery) WithOpaqueID(s string) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
if r.Header == nil {
r.Header = make(http.Header)
}
r.Header.Set("X-Opaque-Id", s)
}
}