Debian Codegraph @

delete_by_query.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 import (
8 "context"
9 "fmt"
10 "net/url"
11 "strings"
12
13 "gopkg.in/olivere/elastic.v5/uritemplates"
14 )
15
16 // DeleteByQueryService deletes documents that match a query.
17 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-delete-by-query.html.
18 type DeleteByQueryService struct {
19 client *Client
20 index []string
21 typ []string
22 query Query
23 body interface{}
24 xSource []string
25 xSourceExclude []string
26 xSourceInclude []string
27 analyzer string
28 analyzeWildcard *bool
29 allowNoIndices *bool
30 conflicts string
31 defaultOperator string
32 df string
33 docvalueFields []string
34 expandWildcards string
35 explain *bool
36 from *int
37 ignoreUnavailable *bool
38 lenient *bool
39 lowercaseExpandedTerms *bool
40 preference string
41 q string
42 refresh string
43 requestCache *bool
44 requestsPerSecond *int
45 routing []string
46 scroll string
47 scrollSize *int
48 searchTimeout string
49 searchType string
50 size *int
51 sort []string
52 stats []string
53 storedFields []string
54 suggestField string
55 suggestMode string
56 suggestSize *int
57 suggestText string
58 terminateAfter *int
59 timeout string
60 trackScores *bool
61 version *bool
62 waitForActiveShards string
63 waitForCompletion *bool
64 pretty bool
65 }
66
67 // NewDeleteByQueryService creates a new DeleteByQueryService.
68 // You typically use the client's DeleteByQuery to get a reference to
69 // the service.
70 func NewDeleteByQueryService(client *Client) *DeleteByQueryService {
71 builder := &DeleteByQueryService{
72 client: client,
73 }
74 return builder
75 }
76
77 // Index sets the indices on which to perform the delete operation.
78 func (s *DeleteByQueryService) Index(index ...string) *DeleteByQueryService {
79 s.index = append(s.index, index...)
80 return s
81 }
82
83 // Type limits the delete operation to the given types.
84 func (s *DeleteByQueryService) Type(typ ...string) *DeleteByQueryService {
85 s.typ = append(s.typ, typ...)
86 return s
87 }
88
89 // XSource is true or false to return the _source field or not,
90 // or a list of fields to return.
91 func (s *DeleteByQueryService) XSource(xSource ...string) *DeleteByQueryService {
92 s.xSource = append(s.xSource, xSource...)
93 return s
94 }
95
96 // XSourceExclude represents a list of fields to exclude from the returned _source field.
97 func (s *DeleteByQueryService) XSourceExclude(xSourceExclude ...string) *DeleteByQueryService {
98 s.xSourceExclude = append(s.xSourceExclude, xSourceExclude...)
99 return s
100 }
101
102 // XSourceInclude represents a list of fields to extract and return from the _source field.
103 func (s *DeleteByQueryService) XSourceInclude(xSourceInclude ...string) *DeleteByQueryService {
104 s.xSourceInclude = append(s.xSourceInclude, xSourceInclude...)
105 return s
106 }
107
108 // Analyzer to use for the query string.
109 func (s *DeleteByQueryService) Analyzer(analyzer string) *DeleteByQueryService {
110 s.analyzer = analyzer
111 return s
112 }
113
114 // AnalyzeWildcard specifies whether wildcard and prefix queries should be
115 // analyzed (default: false).
116 func (s *DeleteByQueryService) AnalyzeWildcard(analyzeWildcard bool) *DeleteByQueryService {
117 s.analyzeWildcard = &analyzeWildcard
118 return s
119 }
120
121 // AllowNoIndices indicates whether to ignore if a wildcard indices
122 // expression resolves into no concrete indices (including the _all string
123 // or when no indices have been specified).
124 func (s *DeleteByQueryService) AllowNoIndices(allow bool) *DeleteByQueryService {
125 s.allowNoIndices = &allow
126 return s
127 }
128
129 // Conflicts indicates what to do when the process detects version conflicts.
130 // Possible values are "proceed" and "abort".
131 func (s *DeleteByQueryService) Conflicts(conflicts string) *DeleteByQueryService {
132 s.conflicts = conflicts
133 return s
134 }
135
136 // AbortOnVersionConflict aborts the request on version conflicts.
137 // It is an alias to setting Conflicts("abort").
138 func (s *DeleteByQueryService) AbortOnVersionConflict() *DeleteByQueryService {
139 s.conflicts = "abort"
140 return s
141 }
142
143 // ProceedOnVersionConflict aborts the request on version conflicts.
144 // It is an alias to setting Conflicts("proceed").
145 func (s *DeleteByQueryService) ProceedOnVersionConflict() *DeleteByQueryService {
146 s.conflicts = "proceed"
147 return s
148 }
149
150 // DefaultOperator for query string query (AND or OR).
151 func (s *DeleteByQueryService) DefaultOperator(defaultOperator string) *DeleteByQueryService {
152 s.defaultOperator = defaultOperator
153 return s
154 }
155
156 // DF is the field to use as default where no field prefix is given in the query string.
157 func (s *DeleteByQueryService) DF(defaultField string) *DeleteByQueryService {
158 s.df = defaultField
159 return s
160 }
161
162 // DefaultField is the field to use as default where no field prefix is given in the query string.
163 // It is an alias to the DF func.
164 func (s *DeleteByQueryService) DefaultField(defaultField string) *DeleteByQueryService {
165 s.df = defaultField
166 return s
167 }
168
169 // DocvalueFields specifies the list of fields to return as the docvalue representation of a field for each hit.
170 func (s *DeleteByQueryService) DocvalueFields(docvalueFields ...string) *DeleteByQueryService {
171 s.docvalueFields = docvalueFields
172 return s
173 }
174
175 // ExpandWildcards indicates whether to expand wildcard expression to
176 // concrete indices that are open, closed or both. It can be "open" or "closed".
177 func (s *DeleteByQueryService) ExpandWildcards(expand string) *DeleteByQueryService {
178 s.expandWildcards = expand
179 return s
180 }
181
182 // Explain specifies whether to return detailed information about score
183 // computation as part of a hit.
184 func (s *DeleteByQueryService) Explain(explain bool) *DeleteByQueryService {
185 s.explain = &explain
186 return s
187 }
188
189 // From is the starting offset (default: 0).
190 func (s *DeleteByQueryService) From(from int) *DeleteByQueryService {
191 s.from = &from
192 return s
193 }
194
195 // IgnoreUnavailable indicates whether specified concrete indices should be
196 // ignored when unavailable (missing or closed).
197 func (s *DeleteByQueryService) IgnoreUnavailable(ignore bool) *DeleteByQueryService {
198 s.ignoreUnavailable = &ignore
199 return s
200 }
201
202 // Lenient specifies whether format-based query failures
203 // (such as providing text to a numeric field) should be ignored.
204 func (s *DeleteByQueryService) Lenient(lenient bool) *DeleteByQueryService {
205 s.lenient = &lenient
206 return s
207 }
208
209 // LowercaseExpandedTerms specifies whether query terms should be lowercased.
210 func (s *DeleteByQueryService) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *DeleteByQueryService {
211 s.lowercaseExpandedTerms = &lowercaseExpandedTerms
212 return s
213 }
214
215 // Preference specifies the node or shard the operation should be performed on
216 // (default: random).
217 func (s *DeleteByQueryService) Preference(preference string) *DeleteByQueryService {
218 s.preference = preference
219 return s
220 }
221
222 // Q specifies the query in Lucene query string syntax. You can also use
223 // Query to programmatically specify the query.
224 func (s *DeleteByQueryService) Q(query string) *DeleteByQueryService {
225 s.q = query
226 return s
227 }
228
229 // QueryString is an alias to Q. Notice that you can also use Query to
230 // programmatically set the query.
231 func (s *DeleteByQueryService) QueryString(query string) *DeleteByQueryService {
232 s.q = query
233 return s
234 }
235
236 // Query sets the query programmatically.
237 func (s *DeleteByQueryService) Query(query Query) *DeleteByQueryService {
238 s.query = query
239 return s
240 }
241
242 // Refresh indicates whether the effected indexes should be refreshed.
243 func (s *DeleteByQueryService) Refresh(refresh string) *DeleteByQueryService {
244 s.refresh = refresh
245 return s
246 }
247
248 // RequestCache specifies if request cache should be used for this request
249 // or not, defaults to index level setting.
250 func (s *DeleteByQueryService) RequestCache(requestCache bool) *DeleteByQueryService {
251 s.requestCache = &requestCache
252 return s
253 }
254
255 // RequestsPerSecond sets the throttle on this request in sub-requests per second.
256 // -1 means set no throttle as does "unlimited" which is the only non-float this accepts.
257 func (s *DeleteByQueryService) RequestsPerSecond(requestsPerSecond int) *DeleteByQueryService {
258 s.requestsPerSecond = &requestsPerSecond
259 return s
260 }
261
262 // Routing is a list of specific routing values.
263 func (s *DeleteByQueryService) Routing(routing ...string) *DeleteByQueryService {
264 s.routing = append(s.routing, routing...)
265 return s
266 }
267
268 // Scroll specifies how long a consistent view of the index should be maintained
269 // for scrolled search.
270 func (s *DeleteByQueryService) Scroll(scroll string) *DeleteByQueryService {
271 s.scroll = scroll
272 return s
273 }
274
275 // ScrollSize is the size on the scroll request powering the update_by_query.
276 func (s *DeleteByQueryService) ScrollSize(scrollSize int) *DeleteByQueryService {
277 s.scrollSize = &scrollSize
278 return s
279 }
280
281 // SearchTimeout defines an explicit timeout for each search request.
282 // Defaults to no timeout.
283 func (s *DeleteByQueryService) SearchTimeout(searchTimeout string) *DeleteByQueryService {
284 s.searchTimeout = searchTimeout
285 return s
286 }
287
288 // SearchType is the search operation type. Possible values are
289 // "query_then_fetch" and "dfs_query_then_fetch".
290 func (s *DeleteByQueryService) SearchType(searchType string) *DeleteByQueryService {
291 s.searchType = searchType
292 return s
293 }
294
295 // Size represents the number of hits to return (default: 10).
296 func (s *DeleteByQueryService) Size(size int) *DeleteByQueryService {
297 s.size = &size
298 return s
299 }
300
301 // Sort is a list of <field>:<direction> pairs.
302 func (s *DeleteByQueryService) Sort(sort ...string) *DeleteByQueryService {
303 s.sort = append(s.sort, sort...)
304 return s
305 }
306
307 // SortByField adds a sort order.
308 func (s *DeleteByQueryService) SortByField(field string, ascending bool) *DeleteByQueryService {
309 if ascending {
310 s.sort = append(s.sort, fmt.Sprintf("%s:asc", field))
311 } else {
312 s.sort = append(s.sort, fmt.Sprintf("%s:desc", field))
313 }
314 return s
315 }
316
317 // Stats specifies specific tag(s) of the request for logging and statistical purposes.
318 func (s *DeleteByQueryService) Stats(stats ...string) *DeleteByQueryService {
319 s.stats = append(s.stats, stats...)
320 return s
321 }
322
323 // StoredFields specifies the list of stored fields to return as part of a hit.
324 func (s *DeleteByQueryService) StoredFields(storedFields ...string) *DeleteByQueryService {
325 s.storedFields = storedFields
326 return s
327 }
328
329 // SuggestField specifies which field to use for suggestions.
330 func (s *DeleteByQueryService) SuggestField(suggestField string) *DeleteByQueryService {
331 s.suggestField = suggestField
332 return s
333 }
334
335 // SuggestMode specifies the suggest mode. Possible values are
336 // "missing", "popular", and "always".
337 func (s *DeleteByQueryService) SuggestMode(suggestMode string) *DeleteByQueryService {
338 s.suggestMode = suggestMode
339 return s
340 }
341
342 // SuggestSize specifies how many suggestions to return in response.
343 func (s *DeleteByQueryService) SuggestSize(suggestSize int) *DeleteByQueryService {
344 s.suggestSize = &suggestSize
345 return s
346 }
347
348 // SuggestText specifies the source text for which the suggestions should be returned.
349 func (s *DeleteByQueryService) SuggestText(suggestText string) *DeleteByQueryService {
350 s.suggestText = suggestText
351 return s
352 }
353
354 // TerminateAfter indicates the maximum number of documents to collect
355 // for each shard, upon reaching which the query execution will terminate early.
356 func (s *DeleteByQueryService) TerminateAfter(terminateAfter int) *DeleteByQueryService {
357 s.terminateAfter = &terminateAfter
358 return s
359 }
360
361 // Timeout is the time each individual bulk request should wait for shards
362 // that are unavailable.
363 func (s *DeleteByQueryService) Timeout(timeout string) *DeleteByQueryService {
364 s.timeout = timeout
365 return s
366 }
367
368 // TimeoutInMillis sets the timeout in milliseconds.
369 func (s *DeleteByQueryService) TimeoutInMillis(timeoutInMillis int) *DeleteByQueryService {
370 s.timeout = fmt.Sprintf("%dms", timeoutInMillis)
371 return s
372 }
373
374 // TrackScores indicates whether to calculate and return scores even if
375 // they are not used for sorting.
376 func (s *DeleteByQueryService) TrackScores(trackScores bool) *DeleteByQueryService {
377 s.trackScores = &trackScores
378 return s
379 }
380
381 // Version specifies whether to return document version as part of a hit.
382 func (s *DeleteByQueryService) Version(version bool) *DeleteByQueryService {
383 s.version = &version
384 return s
385 }
386
387 // WaitForActiveShards sets the number of shard copies that must be active before proceeding
388 // with the update by query operation. Defaults to 1, meaning the primary shard only.
389 // Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal
390 // to the total number of copies for the shard (number of replicas + 1).
391 func (s *DeleteByQueryService) WaitForActiveShards(waitForActiveShards string) *DeleteByQueryService {
392 s.waitForActiveShards = waitForActiveShards
393 return s
394 }
395
396 // WaitForCompletion indicates if the request should block until the reindex is complete.
397 func (s *DeleteByQueryService) WaitForCompletion(waitForCompletion bool) *DeleteByQueryService {
398 s.waitForCompletion = &waitForCompletion
399 return s
400 }
401
402 // Pretty indents the JSON output from Elasticsearch.
403 func (s *DeleteByQueryService) Pretty(pretty bool) *DeleteByQueryService {
404 s.pretty = pretty
405 return s
406 }
407
408 // Body specifies the body of the request. It overrides data being specified via SearchService.
409 func (s *DeleteByQueryService) Body(body string) *DeleteByQueryService {
410 s.body = body
411 return s
412 }
413
414 // buildURL builds the URL for the operation.
415 func (s *DeleteByQueryService) buildURL() (string, url.Values, error) {
416 // Build URL
417 var err error
418 var path string
419 if len(s.typ) > 0 {
420 path, err = uritemplates.Expand("/{index}/{type}/_delete_by_query", map[string]string{
421 "index": strings.Join(s.index, ","),
422 "type": strings.Join(s.typ, ","),
423 })
424 } else {
425 path, err = uritemplates.Expand("/{index}/_delete_by_query", map[string]string{
426 "index": strings.Join(s.index, ","),
427 })
428 }
429 if err != nil {
430 return "", url.Values{}, err
431 }
432
433 // Add query string parameters
434 params := url.Values{}
435 if len(s.xSource) > 0 {
436 params.Set("_source", strings.Join(s.xSource, ","))
437 }
438 if len(s.xSourceExclude) > 0 {
439 params.Set("_source_exclude", strings.Join(s.xSourceExclude, ","))
440 }
441 if len(s.xSourceInclude) > 0 {
442 params.Set("_source_include", strings.Join(s.xSourceInclude, ","))
443 }
444 if s.analyzer != "" {
445 params.Set("analyzer", s.analyzer)
446 }
447 if s.analyzeWildcard != nil {
448 params.Set("analyze_wildcard", fmt.Sprintf("%v", *s.analyzeWildcard))
449 }
450 if s.defaultOperator != "" {
451 params.Set("default_operator", s.defaultOperator)
452 }
453 if s.df != "" {
454 params.Set("df", s.df)
455 }
456 if s.explain != nil {
457 params.Set("explain", fmt.Sprintf("%v", *s.explain))
458 }
459 if len(s.storedFields) > 0 {
460 params.Set("stored_fields", strings.Join(s.storedFields, ","))
461 }
462 if len(s.docvalueFields) > 0 {
463 params.Set("docvalue_fields", strings.Join(s.docvalueFields, ","))
464 }
465 if s.from != nil {
466 params.Set("from", fmt.Sprintf("%d", *s.from))
467 }
468 if s.ignoreUnavailable != nil {
469 params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
470 }
471 if s.allowNoIndices != nil {
472 params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
473 }
474 if s.conflicts != "" {
475 params.Set("conflicts", s.conflicts)
476 }
477 if s.expandWildcards != "" {
478 params.Set("expand_wildcards", s.expandWildcards)
479 }
480 if s.lenient != nil {
481 params.Set("lenient", fmt.Sprintf("%v", *s.lenient))
482 }
483 if s.lowercaseExpandedTerms != nil {
484 params.Set("lowercase_expanded_terms", fmt.Sprintf("%v", *s.lowercaseExpandedTerms))
485 }
486 if s.preference != "" {
487 params.Set("preference", s.preference)
488 }
489 if s.q != "" {
490 params.Set("q", s.q)
491 }
492 if len(s.routing) > 0 {
493 params.Set("routing", strings.Join(s.routing, ","))
494 }
495 if s.scroll != "" {
496 params.Set("scroll", s.scroll)
497 }
498 if s.searchType != "" {
499 params.Set("search_type", s.searchType)
500 }
501 if s.searchTimeout != "" {
502 params.Set("search_timeout", s.searchTimeout)
503 }
504 if s.size != nil {
505 params.Set("size", fmt.Sprintf("%d", *s.size))
506 }
507 if len(s.sort) > 0 {
508 params.Set("sort", strings.Join(s.sort, ","))
509 }
510 if s.terminateAfter != nil {
511 params.Set("terminate_after", fmt.Sprintf("%v", *s.terminateAfter))
512 }
513 if len(s.stats) > 0 {
514 params.Set("stats", strings.Join(s.stats, ","))
515 }
516 if s.suggestField != "" {
517 params.Set("suggest_field", s.suggestField)
518 }
519 if s.suggestMode != "" {
520 params.Set("suggest_mode", s.suggestMode)
521 }
522 if s.suggestSize != nil {
523 params.Set("suggest_size", fmt.Sprintf("%v", *s.suggestSize))
524 }
525 if s.suggestText != "" {
526 params.Set("suggest_text", s.suggestText)
527 }
528 if s.timeout != "" {
529 params.Set("timeout", s.timeout)
530 }
531 if s.trackScores != nil {
532 params.Set("track_scores", fmt.Sprintf("%v", *s.trackScores))
533 }
534 if s.version != nil {
535 params.Set("version", fmt.Sprintf("%v", *s.version))
536 }
537 if s.requestCache != nil {
538 params.Set("request_cache", fmt.Sprintf("%v", *s.requestCache))
539 }
540 if s.refresh != "" {
541 params.Set("refresh", s.refresh)
542 }
543 if s.waitForActiveShards != "" {
544 params.Set("wait_for_active_shards", s.waitForActiveShards)
545 }
546 if s.scrollSize != nil {
547 params.Set("scroll_size", fmt.Sprintf("%d", *s.scrollSize))
548 }
549 if s.waitForCompletion != nil {
550 params.Set("wait_for_completion", fmt.Sprintf("%v", *s.waitForCompletion))
551 }
552 if s.requestsPerSecond != nil {
553 params.Set("requests_per_second", fmt.Sprintf("%v", *s.requestsPerSecond))
554 }
555 if s.pretty {
556 params.Set("pretty", fmt.Sprintf("%v", s.pretty))
557 }
558 return path, params, nil
559 }
560
561 // Validate checks if the operation is valid.
562 func (s *DeleteByQueryService) Validate() error {
563 var invalid []string
564 if len(s.index) == 0 {
565 invalid = append(invalid, "Index")
566 }
567 if len(invalid) > 0 {
568 return fmt.Errorf("missing required fields: %v", invalid)
569 }
570 return nil
571 }
572
573 // Do executes the delete-by-query operation.
574 func (s *DeleteByQueryService) Do(ctx context.Context) (*BulkIndexByScrollResponse, error) {
575 // Check pre-conditions
576 if err := s.Validate(); err != nil {
577 return nil, err
578 }
579
580 // Get URL for request
581 path, params, err := s.buildURL()
582 if err != nil {
583 return nil, err
584 }
585
586 // Set body if there is a query set
587 var body interface{}
588 if s.body != nil {
589 body = s.body
590 } else if s.query != nil {
591 src, err := s.query.Source()
592 if err != nil {
593 return nil, err
594 }
595 body = map[string]interface{}{
596 "query": src,
597 }
598 }
599
600 // Get response
601 res, err := s.client.PerformRequest(ctx, "POST", path, params, body)
602 if err != nil {
603 return nil, err
604 }
605
606 // Return result
607 ret := new(BulkIndexByScrollResponse)
608 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
609 return nil, err
610 }
611 return ret, nil
612 }
613
614 // BulkIndexByScrollResponse is the outcome of executing Do with
615 // DeleteByQueryService and UpdateByQueryService.
616 type BulkIndexByScrollResponse struct {
617 Took int64 `json:"took"`
618 SliceId *int64 `json:"slice_id,omitempty"`
619 TimedOut bool `json:"timed_out"`
620 Total int64 `json:"total"`
621 Updated int64 `json:"updated,omitempty"`
622 Created int64 `json:"created,omitempty"`
623 Deleted int64 `json:"deleted"`
624 Batches int64 `json:"batches"`
625 VersionConflicts int64 `json:"version_conflicts"`
626 Noops int64 `json:"noops"`
627 Retries struct {
628 Bulk int64 `json:"bulk"`
629 Search int64 `json:"search"`
630 } `json:"retries,omitempty"`
631 Throttled string `json:"throttled"`
632 ThrottledMillis int64 `json:"throttled_millis"`
633 RequestsPerSecond float64 `json:"requests_per_second"`
634 Canceled string `json:"canceled,omitempty"`
635 ThrottledUntil string `json:"throttled_until"`
636 ThrottledUntilMillis int64 `json:"throttled_until_millis"`
637 Failures []bulkIndexByScrollResponseFailure `json:"failures"`
638 }
639
640 type bulkIndexByScrollResponseFailure struct {
641 Index string `json:"index,omitempty"`
642 Type string `json:"type,omitempty"`
643 Id string `json:"id,omitempty"`
644 Status int `json:"status,omitempty"`
645 Shard int `json:"shard,omitempty"`
646 Node int `json:"node,omitempty"`
647 // TOOD "cause" contains exception details
648 // TOOD "reason" contains exception details
649 }