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