Source: api/api/bulk.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. 'wait_for_active_shards',
  34. 'refresh',
  35. 'routing',
  36. 'timeout',
  37. 'type',
  38. '_source',
  39. '_source_excludes',
  40. '_source_exclude',
  41. '_source_includes',
  42. '_source_include',
  43. 'pipeline',
  44. 'require_alias',
  45. 'pretty',
  46. 'human',
  47. 'error_trace',
  48. 'source',
  49. 'filter_path',
  50. ];
  51. const snakeCase = {
  52. waitForActiveShards: 'wait_for_active_shards',
  53. _sourceExcludes: '_source_excludes',
  54. _sourceExclude: '_source_exclude',
  55. _sourceIncludes: '_source_includes',
  56. _sourceInclude: '_source_include',
  57. requireAlias: 'require_alias',
  58. errorTrace: 'error_trace',
  59. filterPath: 'filter_path',
  60. };
  61. /**
  62. * The bulk operation lets you add, update, or delete many documents in a single request.
  63. * Compared to individual OpenSearch indexing requests, the bulk operation has significant performance benefits.
  64. * Whenever practical, we recommend batching indexing operations into bulk requests.
  65. * <br/> See Also: {@link https://opensearch.org/docs/latest/api-reference/document-apis/bulk/|OpenSearch - Bulk}
  66. *
  67. * @memberOf API-Document
  68. *
  69. * @param {Object} params
  70. * @param {Object[]} params.body - {@link https://opensearch.org/docs/latest/api-reference/document-apis/bulk/#request-body|Request Body}
  71. * @param {string} [params.index] - Specifying the index means you don’t need to include it in the request body.
  72. * @param {string} [params.pipeline] - The pipeline ID for preprocessing documents.
  73. * @param {string} [params.routing] - Routes the request to the specified shard.
  74. * @param {string} [params.refresh=false] - If true, OpenSearch refreshes shards to make the operation visible to searching. Valid options are 'true', 'false', and 'wait_for', which tells OpenSearch to wait for a refresh before executing the operation.
  75. * @param {string} [params.timeout=1m] - How long to wait for a response from the cluster.
  76. * @param {string} [params.wait_for_active_shards] - The number of active shards that must be available before OpenSearch processes the request. Default is 1 (only the primary shard). Set to all or a positive integer. Values greater than 1 require replicas. For example, if you specify a value of 3, the index must have two replicas distributed across two additional nodes for the operation to succeed.
  77. * @param {boolean} [params.require_alias=false] - Specifies whether the target index must be an index alias.
  78. *
  79. * @param {Object} options - Options for {@link Transport#request}
  80. * @param {function} callback - Callback that handles errors and response
  81. *
  82. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/latest/api-reference/document-apis/bulk/#response|Bulk Response}
  83. */
  84. function bulkApi(params, options, callback) {
  85. [params, options, callback] = normalizeArguments(params, options, callback);
  86. // check required parameters
  87. if (params.body == null) {
  88. const err = new this[kConfigurationError]('Missing required parameter: body');
  89. return handleError(err, callback);
  90. }
  91. // check required url components
  92. if (params.type != null && params.index == null) {
  93. const err = new this[kConfigurationError]('Missing required parameter of the url: index');
  94. return handleError(err, callback);
  95. }
  96. let { method, body, index, type, ...querystring } = params;
  97. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  98. let path = '';
  99. if (index != null && type != null) {
  100. if (method == null) method = 'POST';
  101. path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_bulk';
  102. } else if (index != null) {
  103. if (method == null) method = 'POST';
  104. path = '/' + encodeURIComponent(index) + '/' + '_bulk';
  105. } else {
  106. if (method == null) method = 'POST';
  107. path = '/' + '_bulk';
  108. }
  109. // build request object
  110. const request = {
  111. method,
  112. path,
  113. bulkBody: body,
  114. querystring,
  115. };
  116. return this.transport.request(request, options, callback);
  117. }
  118. module.exports = bulkApi;