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