Source: api/api/update_by_query.js

  1. /*
  2. * Copyright OpenSearch Contributors
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * The OpenSearch Contributors require contributions made to
  6. * this file be licensed under the Apache-2.0 license or a
  7. * compatible open source license.
  8. *
  9. */
  10. /*
  11. * Licensed to Elasticsearch B.V. under one or more contributor
  12. * license agreements. See the NOTICE file distributed with
  13. * this work for additional information regarding copyright
  14. * ownership. Elasticsearch B.V. licenses this file to you under
  15. * the Apache License, Version 2.0 (the "License"); you may
  16. * not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing,
  22. * software distributed under the License is distributed on an
  23. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  24. * KIND, either express or implied. See the License for the
  25. * specific language governing permissions and limitations
  26. * under the License.
  27. */
  28. 'use strict';
  29. /* eslint camelcase: 0 */
  30. /* eslint no-unused-vars: 0 */
  31. const { handleError, snakeCaseKeys, normalizeArguments, kConfigurationError } = require('../utils');
  32. const acceptedQuerystring = [
  33. 'analyzer',
  34. 'analyze_wildcard',
  35. 'default_operator',
  36. 'df',
  37. 'from',
  38. 'ignore_unavailable',
  39. 'allow_no_indices',
  40. 'conflicts',
  41. 'expand_wildcards',
  42. 'lenient',
  43. 'pipeline',
  44. 'preference',
  45. 'q',
  46. 'routing',
  47. 'scroll',
  48. 'search_type',
  49. 'search_timeout',
  50. 'size',
  51. 'max_docs',
  52. 'sort',
  53. '_source',
  54. '_source_excludes',
  55. '_source_exclude',
  56. '_source_includes',
  57. '_source_include',
  58. 'terminate_after',
  59. 'stats',
  60. 'version',
  61. 'version_type',
  62. 'request_cache',
  63. 'refresh',
  64. 'timeout',
  65. 'wait_for_active_shards',
  66. 'scroll_size',
  67. 'wait_for_completion',
  68. 'requests_per_second',
  69. 'slices',
  70. 'pretty',
  71. 'human',
  72. 'error_trace',
  73. 'source',
  74. 'filter_path',
  75. ];
  76. const snakeCase = {
  77. analyzeWildcard: 'analyze_wildcard',
  78. defaultOperator: 'default_operator',
  79. ignoreUnavailable: 'ignore_unavailable',
  80. allowNoIndices: 'allow_no_indices',
  81. expandWildcards: 'expand_wildcards',
  82. searchType: 'search_type',
  83. searchTimeout: 'search_timeout',
  84. maxDocs: 'max_docs',
  85. _sourceExcludes: '_source_excludes',
  86. _sourceExclude: '_source_exclude',
  87. _sourceIncludes: '_source_includes',
  88. _sourceInclude: '_source_include',
  89. terminateAfter: 'terminate_after',
  90. versionType: 'version_type',
  91. requestCache: 'request_cache',
  92. waitForActiveShards: 'wait_for_active_shards',
  93. scrollSize: 'scroll_size',
  94. waitForCompletion: 'wait_for_completion',
  95. requestsPerSecond: 'requests_per_second',
  96. errorTrace: 'error_trace',
  97. filterPath: 'filter_path',
  98. };
  99. /**
  100. * Run a script to update all documents that match the query.
  101. * <br/> See Also: {@link https://opensearch.org/docs/2.4/api-reference/document-apis/update-by-query/ OpenSearch - Update by query}
  102. *
  103. * @memberOf API-Document
  104. *
  105. * @param {Object} params
  106. * @param {string} params.index - A comma-separated list of index names to search; use '_all' or empty string to perform the operation on all indices
  107. * @param {Object} [params.body] - The search definition using the Query DSL
  108. * @param {string} [params.analyzer] - The analyzer to use for the query string
  109. * @param {boolean} [params.analyze_wildcard=false] - Specify whether wildcard and prefix queries should be analyzed (default: false)
  110. * @param {string} [params.default_operator=OR] - The default operator for query string query (options: AND, OR)
  111. * @param {string} [params.df] - The field to use as default where no field prefix is given in the query string
  112. * @param {number} [params.from=0] - Starting offset
  113. * @param {boolean} [params.ignore_unavailable=false] - Whether specified concrete indices should be ignored when unavailable (missing or closed)
  114. * @param {boolean} [params.allow_no_indices=true] - Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes '_all' string or when no indices have been specified)
  115. * @param {string} [params.conflicts=abort] - What to do when the update by query hits version conflicts? (options: abort, proceed)
  116. * @param {string} [params.expand_wildcards=open] - Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)
  117. * @param {boolean} [params.lenient=false] - Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
  118. * @param {string} [params.pipeline] - Ingest pipeline to set on index requests made by this action. (default: none)
  119. * @param {string} [params.preference] - Specify the node or shard the operation should be performed on (default: random)
  120. * @param {string} [params.q] - Query in the Lucene query string syntax
  121. * @param {string} [params.routing] - A comma-separated list of specific routing values
  122. * @param {string} [params.scroll] - Specify how long a consistent view of the index should be maintained for scrolled search
  123. * @param {string} [params.search_type=query_then_fetch] - Search operation type (options: query_then_fetch, dfs_query_then_fetch)
  124. * @param {string} [params.search_timeout] - Explicit timeout for each search request. Defaults to no timeout.
  125. * @param {number} [params.size] - Deprecated, please use 'max_docs' instead
  126. * @param {number} [params.max_docs] - Maximum number of documents to process (default: all documents)
  127. * @param {string} [params.sort] - A comma-separated list of <field>:<direction> pairs
  128. * @param {string} [params._source] - True or false to return the _source field or not, or a list of fields to return
  129. * @param {string} [params._source_excludes] - A list of fields to exclude from the returned _source field
  130. * @param {string} [params._source_includes] - A list of fields to extract and return from the _source field
  131. * @param {number} [params.terminate_after] - The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
  132. * @param {string} [params.stats] - Specific 'tag' of the request for logging and statistical purposes
  133. * @param {boolean} [params.version] - Specify whether to return document version as part of a hit
  134. * @param {boolean} [params.version_type] - Should the document increment the version number (internal) on hit or not (reindex)
  135. * @param {boolean} [params.request_cache] - Specify if request cache should be used for this request or not, defaults to index level setting
  136. * @param {boolean} [params.refresh=false] - Should the affected indexes be refreshed?
  137. * @param {string} [params.timeout] - Time each individual bulk request should wait for shards that are unavailable.
  138. * @param {string} [params.wait_for_active_shards=1] - Sets the number of shard copies that must be active before proceeding with the update by query operation. 1 means 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)
  139. * @param {number} [params.scroll_size=1000] - Size on the scroll request powering the update by query
  140. * @param {boolean} [params.wait_for_completion=true] - Should the request should block until the update by query operation is complete.
  141. * @param {number} [params.requests_per_second=-1] - The throttle to set on this request in sub-requests per second. -1 means no throttle.
  142. * @param {string} [params.slices=1] - The number of slices this task should be divided into. 1 means the task isn't sliced into subtasks. Can be set to 'auto'.
  143. *
  144. * @param {Object} options - Options for {@link Transport#request}
  145. * @param {function} callback - Callback that handles errors and response
  146. *
  147. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/2.4/api-reference/document-apis/update-by-query/#response Update by query Response}
  148. */
  149. function updateByQueryApi(params, options, callback) {
  150. [params, options, callback] = normalizeArguments(params, options, callback);
  151. // check required parameters
  152. if (params.index == null) {
  153. const err = new this[kConfigurationError]('Missing required parameter: index');
  154. return handleError(err, callback);
  155. }
  156. // check required url components
  157. if (params.type != null && params.index == null) {
  158. const err = new this[kConfigurationError]('Missing required parameter of the url: index');
  159. return handleError(err, callback);
  160. }
  161. let { method, body, index, type, ...querystring } = params;
  162. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  163. let path = '';
  164. if (index != null && type != null) {
  165. if (method == null) method = 'POST';
  166. path =
  167. '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_update_by_query';
  168. } else {
  169. if (method == null) method = 'POST';
  170. path = '/' + encodeURIComponent(index) + '/' + '_update_by_query';
  171. }
  172. // build request object
  173. const request = {
  174. method,
  175. path,
  176. body: body || '',
  177. querystring,
  178. };
  179. return this.transport.request(request, options, callback);
  180. }
  181. module.exports = updateByQueryApi;