Source: api/api/update.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. '_source',
  35. '_source_excludes',
  36. '_source_exclude',
  37. '_source_includes',
  38. '_source_include',
  39. 'lang',
  40. 'refresh',
  41. 'retry_on_conflict',
  42. 'routing',
  43. 'timeout',
  44. 'if_seq_no',
  45. 'if_primary_term',
  46. 'require_alias',
  47. 'pretty',
  48. 'human',
  49. 'error_trace',
  50. 'source',
  51. 'filter_path',
  52. ];
  53. const snakeCase = {
  54. waitForActiveShards: 'wait_for_active_shards',
  55. _sourceExcludes: '_source_excludes',
  56. _sourceExclude: '_source_exclude',
  57. _sourceIncludes: '_source_includes',
  58. _sourceInclude: '_source_include',
  59. retryOnConflict: 'retry_on_conflict',
  60. ifSeqNo: 'if_seq_no',
  61. ifPrimaryTerm: 'if_primary_term',
  62. requireAlias: 'require_alias',
  63. errorTrace: 'error_trace',
  64. filterPath: 'filter_path',
  65. };
  66. /**
  67. * Update an existing document
  68. * <br/> See Also: {@link https://opensearch.org/docs/2.4/api-reference/document-apis/update-document/ OpenSearch - Update Document}
  69. *
  70. * @memberOf API-Document
  71. *
  72. * @param {Object} params
  73. * @param {string} params.index - Name of the index.
  74. * @param {string} params.id - A unique identifier to attach to the document.
  75. * @param {Object} params.body - The request definition requires either `script` or partial `doc`.
  76. * @param {number} [params.if_seq_no] - Only perform the update operation if the document has the specified sequence number.
  77. * @param {number} [params.if_primary_term] - Only perform the update operation if the document has the specified primary term.
  78. * @param {string} [params.lang=painless] - Language of the script.
  79. * @param {string} [params.routing] - Value used to assign the index operation to a specific shard.
  80. * @param {string} [params._source=true] - Whether to include the '_source' field in the response body.
  81. * @param {string} [params._source_excludes] - A comma-separated list of source fields to exclude in the query response.
  82. * @param {string} [params._source_includes] - A comma-separated list of source fields to include in the query response.
  83. * @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.
  84. * @param {number} [params.retry_on_conflict=0] - The amount of times OpenSearch should retry the operation if there’s a document conflict.
  85. * @param {string} [params.timeout=1m] - How long to wait for a response from the cluster.
  86. * @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.
  87. * @param {boolean} [params.require_alias=false] - Specifies whether the target index must be an index alias.
  88. *
  89. * @param {Object} options - Options for {@link Transport#request}
  90. * @param {function} callback - Callback that handles errors and response
  91. *
  92. * @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}
  93. */
  94. function updateApi(params, options, callback) {
  95. [params, options, callback] = normalizeArguments(params, options, callback);
  96. // check required parameters
  97. if (params.id == null) {
  98. const err = new this[kConfigurationError]('Missing required parameter: id');
  99. return handleError(err, callback);
  100. }
  101. if (params.index == null) {
  102. const err = new this[kConfigurationError]('Missing required parameter: index');
  103. return handleError(err, callback);
  104. }
  105. if (params.body == null) {
  106. const err = new this[kConfigurationError]('Missing required parameter: body');
  107. return handleError(err, callback);
  108. }
  109. let { method, body, id, index, type, ...querystring } = params;
  110. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  111. let path = '';
  112. if (index != null && type != null && id != null) {
  113. if (method == null) method = 'POST';
  114. path =
  115. '/' +
  116. encodeURIComponent(index) +
  117. '/' +
  118. encodeURIComponent(type) +
  119. '/' +
  120. encodeURIComponent(id) +
  121. '/' +
  122. '_update';
  123. } else {
  124. if (method == null) method = 'POST';
  125. path = '/' + encodeURIComponent(index) + '/' + '_update' + '/' + encodeURIComponent(id);
  126. }
  127. // build request object
  128. const request = {
  129. method,
  130. path,
  131. body: body || '',
  132. querystring,
  133. };
  134. return this.transport.request(request, options, callback);
  135. }
  136. module.exports = updateApi;