Source: api/api/reindex.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. 'refresh',
  34. 'timeout',
  35. 'wait_for_active_shards',
  36. 'wait_for_completion',
  37. 'requests_per_second',
  38. 'scroll',
  39. 'slices',
  40. 'max_docs',
  41. 'pretty',
  42. 'human',
  43. 'error_trace',
  44. 'source',
  45. 'filter_path',
  46. ];
  47. const snakeCase = {
  48. waitForActiveShards: 'wait_for_active_shards',
  49. waitForCompletion: 'wait_for_completion',
  50. requestsPerSecond: 'requests_per_second',
  51. maxDocs: 'max_docs',
  52. errorTrace: 'error_trace',
  53. filterPath: 'filter_path',
  54. };
  55. /**
  56. * Copy all or a subset of your data from a source index into a destination index.
  57. * <br/> See Also: {@link https://opensearch.org/docs/2.4/api-reference/document-apis/reindex/ OpenSearch - Reindex Document}
  58. *
  59. * @memberOf API-Document
  60. *
  61. * @param {Object} params
  62. * @param {Object} params.body The search definition using the Query DSL and the prototype for the index request.
  63. * @param {boolean} [params.refresh=false] Should the affected indexes be refreshed?
  64. * @param {string} [params.timeout=30s] Time each individual bulk request should wait for shards that are unavailable.
  65. * @param {string} [params.wait_for_active_shards] Sets the number of shard copies that must be active before proceeding with the reindex 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)
  66. * @param {boolean} [params.wait_for_completion=false] Should the request should block until the reindex is complete.
  67. * @param {number} [params.requests_per_second=-1] The throttle to set on this request in sub-requests per second. -1 means no throttle.
  68. * @param {string} [params.scroll=5m] Control how long to keep the search context alive
  69. * @param {number|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`.
  70. * @param {number} [params.max_docs] Maximum number of documents to process (default: all documents)
  71. *
  72. * @param {Object} options - Options for {@link Transport#request}
  73. * @param {function} callback - Callback that handles errors and response
  74. *
  75. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/2.4/api-reference/document-apis/reindex/#response Reindex Document Response}
  76. */
  77. function reindexApi(params, options, callback) {
  78. [params, options, callback] = normalizeArguments(params, options, callback);
  79. // check required parameters
  80. if (params.body == null) {
  81. const err = new this[kConfigurationError]('Missing required parameter: body');
  82. return handleError(err, callback);
  83. }
  84. let { method, body, ...querystring } = params;
  85. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  86. let path = '';
  87. if (method == null) method = 'POST';
  88. path = '/' + '_reindex';
  89. // build request object
  90. const request = {
  91. method,
  92. path,
  93. body: body || '',
  94. querystring,
  95. };
  96. return this.transport.request(request, options, callback);
  97. }
  98. module.exports = reindexApi;