Source: api/api/delete.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. 'if_seq_no',
  38. 'if_primary_term',
  39. 'version',
  40. 'version_type',
  41. 'pretty',
  42. 'human',
  43. 'error_trace',
  44. 'source',
  45. 'filter_path',
  46. ];
  47. const snakeCase = {
  48. waitForActiveShards: 'wait_for_active_shards',
  49. ifSeqNo: 'if_seq_no',
  50. ifPrimaryTerm: 'if_primary_term',
  51. versionType: 'version_type',
  52. errorTrace: 'error_trace',
  53. filterPath: 'filter_path',
  54. };
  55. /**
  56. * Delete a document
  57. * <br/> See Also: {@link https://opensearch.org/docs/2.4/api-reference/document-apis/update-document/ OpenSearch - Update Document}
  58. *
  59. * @memberOf API-Document
  60. *
  61. * @param {Object} params
  62. * @param {string} params.index - Name of the index.
  63. * @param {string} params.id - A unique identifier to attach to the document.
  64. * @param {number} [params.if_seq_no] - Only perform the delete operation if the document has the specified sequence number.
  65. * @param {number} [params.if_primary_term] - Only perform the delete operation if the document has the specified primary term.
  66. * @param {string} [params.routing] - Value used to assign the index operation to a specific shard.
  67. * @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.
  68. * @param {string} [params.timeout=1m] - How long to wait for a response from the cluster.
  69. * @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.
  70. * @param {number} [params.version] - The document’s version number.
  71. * @param {number} [params.version_type] - Specific version type (options: 'external' and 'external_gte')
  72. *
  73. * @param {Object} options - Options for {@link Transport#request}
  74. * @param {function} callback - Callback that handles errors and response
  75. *
  76. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/2.4/api-reference/document-apis/update-document/#response Update Response}
  77. */
  78. function deleteApi(params, options, callback) {
  79. [params, options, callback] = normalizeArguments(params, options, callback);
  80. // check required parameters
  81. if (params.id == null) {
  82. const err = new this[kConfigurationError]('Missing required parameter: id');
  83. return handleError(err, callback);
  84. }
  85. if (params.index == null) {
  86. const err = new this[kConfigurationError]('Missing required parameter: index');
  87. return handleError(err, callback);
  88. }
  89. let { method, body, id, index, type, ...querystring } = params;
  90. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  91. let path = '';
  92. if (index != null && type != null && id != null) {
  93. if (method == null) method = 'DELETE';
  94. path =
  95. '/' +
  96. encodeURIComponent(index) +
  97. '/' +
  98. encodeURIComponent(type) +
  99. '/' +
  100. encodeURIComponent(id);
  101. } else {
  102. if (method == null) method = 'DELETE';
  103. path = '/' + encodeURIComponent(index) + '/' + '_doc' + '/' + encodeURIComponent(id);
  104. }
  105. // build request object
  106. const request = {
  107. method,
  108. path,
  109. body: body || '',
  110. querystring,
  111. };
  112. return this.transport.request(request, options, callback);
  113. }
  114. module.exports = deleteApi;