Source: api/api/delete_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. 'preference',
  44. 'q',
  45. 'routing',
  46. 'scroll',
  47. 'search_type',
  48. 'search_timeout',
  49. 'size',
  50. 'max_docs',
  51. 'sort',
  52. '_source',
  53. '_source_excludes',
  54. '_source_exclude',
  55. '_source_includes',
  56. '_source_include',
  57. 'terminate_after',
  58. 'stats',
  59. 'version',
  60. 'request_cache',
  61. 'refresh',
  62. 'timeout',
  63. 'wait_for_active_shards',
  64. 'scroll_size',
  65. 'wait_for_completion',
  66. 'requests_per_second',
  67. 'slices',
  68. 'pretty',
  69. 'human',
  70. 'error_trace',
  71. 'source',
  72. 'filter_path',
  73. ];
  74. const snakeCase = {
  75. analyzeWildcard: 'analyze_wildcard',
  76. defaultOperator: 'default_operator',
  77. ignoreUnavailable: 'ignore_unavailable',
  78. allowNoIndices: 'allow_no_indices',
  79. expandWildcards: 'expand_wildcards',
  80. searchType: 'search_type',
  81. searchTimeout: 'search_timeout',
  82. maxDocs: 'max_docs',
  83. _sourceExcludes: '_source_excludes',
  84. _sourceExclude: '_source_exclude',
  85. _sourceIncludes: '_source_includes',
  86. _sourceInclude: '_source_include',
  87. terminateAfter: 'terminate_after',
  88. requestCache: 'request_cache',
  89. waitForActiveShards: 'wait_for_active_shards',
  90. scrollSize: 'scroll_size',
  91. waitForCompletion: 'wait_for_completion',
  92. requestsPerSecond: 'requests_per_second',
  93. errorTrace: 'error_trace',
  94. filterPath: 'filter_path',
  95. };
  96. /**
  97. * Deletes documents matching the provided query.
  98. * <br/> See Also: {@link https://opensearch.org/docs/2.4/api-reference/document-apis/delete-by-query/ OpenSearch - Delete by query}
  99. *
  100. * @memberOf API-Document
  101. *
  102. * @param {Object} params
  103. * @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
  104. * @param {Object} params.body - The search definition using the Query DSL
  105. * @param {string} [params.analyzer] - The analyzer to use for the query string
  106. * @param {boolean} [params.analyze_wildcard=false] - Specify whether wildcard and prefix queries should be analyzed
  107. * @param {string} [params.default_operator=OR] - The default operator for query string query (options: AND, OR)
  108. * @param {string} [params.df] - The field to use as default where no field prefix is given in the query string
  109. * @param {number} [params.from=0] - Starting offset
  110. * @param {boolean} [params.ignore_unavailable=false] - Whether specified concrete indices should be ignored when unavailable (missing or closed)
  111. * @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)
  112. * @param {string} [params.conflicts] - What to do when the delete-by-query hits version conflicts? (options: abort, proceed)
  113. * @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)
  114. * @param {boolean} [params.lenient=false] - Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
  115. * @param {string} [params.preference] - Specify the node or shard the operation should be performed on (default: random)
  116. * @param {string} [params.q] - Query in the Lucene query string syntax
  117. * @param {string} [params.routing] - A comma-separated list of specific routing values
  118. * @param {string} [params.scroll] - Specify how long a consistent view of the index should be maintained for scrolled search
  119. * @param {string} [params.search_type] - Search operation type (options: query_then_fetch, dfs_query_then_fetch)
  120. * @param {string} [params.search_timeout] - Explicit timeout for each search request. Defaults to no timeout.
  121. * @param {number} [params.size] - Deprecated, please use 'max_docs' instead
  122. * @param {number} [params.max_docs] - Maximum number of documents to process (default: all documents)
  123. * @param {string} [params.sort] - A comma-separated list of <field>:<direction> pairs
  124. * @param {string} [params._source] - True or false to return the _source field or not, or a list of fields to return
  125. * @param {string} [params._source_excludes] - A list of fields to exclude from the returned _source field
  126. * @param {string} [params._source_includes] - A list of fields to extract and return from the _source field
  127. * @param {number} [params.terminate_after] - The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
  128. * @param {string} [params.stats] - Specific 'tag' of the request for logging and statistical purposes
  129. * @param {boolean} [params.version] - Specify whether to return document version as part of a hit
  130. * @param {boolean} [params.request_cache] - Specify if request cache should be used for this request or not, defaults to index level setting
  131. * @param {boolean} [params.refresh=false] - Should the effected indexes be refreshed?
  132. * @param {string} [params.timeout=1m] - time each individual bulk request should wait for shards that are unavailable.
  133. * @param {string} [params.wait_for_active_shards=1] - Sets the number of shard copies that must be active before proceeding with the delete-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)
  134. * @param {number} [params.scroll_size=1000] - Size on the scroll request powering the delete-by-query
  135. * @param {boolean} [params.wait_for_completion] - Should the request should block until the delete-by-query is complete.
  136. * @param {number} [params.requests_per_second=-1] - The throttle for this request in sub-requests per second. -1 means no throttle.
  137. * @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'.
  138. *
  139. * @param {Object} options - Options for {@link Transport#request}
  140. * @param {function} callback - Callback that handles errors and response
  141. *
  142. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/2.4/api-reference/document-apis/delete-by-query/#response Delete by query Response}
  143. */
  144. function deleteByQueryApi(params, options, callback) {
  145. [params, options, callback] = normalizeArguments(params, options, callback);
  146. // check required parameters
  147. if (params.index == null) {
  148. const err = new this[kConfigurationError]('Missing required parameter: index');
  149. return handleError(err, callback);
  150. }
  151. if (params.body == null) {
  152. const err = new this[kConfigurationError]('Missing required parameter: body');
  153. return handleError(err, callback);
  154. }
  155. // check required url components
  156. if (params.type != null && params.index == null) {
  157. const err = new this[kConfigurationError]('Missing required parameter of the url: index');
  158. return handleError(err, callback);
  159. }
  160. let { method, body, index, type, ...querystring } = params;
  161. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  162. let path = '';
  163. if (index != null && type != null) {
  164. if (method == null) method = 'POST';
  165. path =
  166. '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_delete_by_query';
  167. } else {
  168. if (method == null) method = 'POST';
  169. path = '/' + encodeURIComponent(index) + '/' + '_delete_by_query';
  170. }
  171. // build request object
  172. const request = {
  173. method,
  174. path,
  175. body: body || '',
  176. querystring,
  177. };
  178. return this.transport.request(request, options, callback);
  179. }
  180. module.exports = deleteByQueryApi;