Source: api/api/index.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. /** @namespace API-Document */
  32. const { handleError, snakeCaseKeys, normalizeArguments, kConfigurationError } = require('../utils');
  33. const acceptedQuerystring = [
  34. 'wait_for_active_shards',
  35. 'op_type',
  36. 'refresh',
  37. 'routing',
  38. 'timeout',
  39. 'version',
  40. 'version_type',
  41. 'if_seq_no',
  42. 'if_primary_term',
  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. opType: 'op_type',
  54. versionType: 'version_type',
  55. ifSeqNo: 'if_seq_no',
  56. ifPrimaryTerm: 'if_primary_term',
  57. requireAlias: 'require_alias',
  58. errorTrace: 'error_trace',
  59. filterPath: 'filter_path',
  60. };
  61. /**
  62. * Adds a document to an index.
  63. * <br/> See Also: {@link https://opensearch.org/docs/2.4/api-reference/document-apis/index-document/ OpenSearch - Index Document}
  64. *
  65. * @memberOf API-Document
  66. *
  67. * @param {Object} params
  68. * @param {string} params.index - Name of the index.
  69. * @param {Object} params.body - The content of the document.
  70. * @param {string} [params.id] - A unique identifier to attach to the document.
  71. * @param {number} [params.if_seq_no] - Only perform the index operation if the document has the specified sequence number.
  72. * @param {number} [params.if_primary_term] - Only perform the index operation if the document has the specified primary term.
  73. * @param {string} [params.pipeline] - Route the index operation to a certain pipeline.
  74. * @param {string} [params.routing] - value used to assign the index operation to a specific shard.
  75. * @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.
  76. * @param {string} [params.timeout=1m] - How long to wait for a response from the cluster.
  77. * @param {number} [params.version] - The document’s version number.
  78. * @param {number} [params.version_type] - Specific version type (options: 'external' and 'external_gte')
  79. * @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.
  80. * @param {boolean} [params.require_alias=false] - Specifies whether the target index must be an index alias.
  81. *
  82. * @param {Object} options - Options for {@link Transport#request}
  83. * @param {function} callback - Callback that handles errors and response
  84. *
  85. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/2.4/api-reference/document-apis/index-document/#response Index Response}
  86. */
  87. function indexApi(params, options, callback) {
  88. [params, options, callback] = normalizeArguments(params, options, callback);
  89. // check required parameters
  90. if (params.index == null) {
  91. const err = new this[kConfigurationError]('Missing required parameter: index');
  92. return handleError(err, callback);
  93. }
  94. if (params.body == null) {
  95. const err = new this[kConfigurationError]('Missing required parameter: body');
  96. return handleError(err, callback);
  97. }
  98. let { method, body, id, index, type, ...querystring } = params;
  99. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  100. let path = '';
  101. if (index != null && type != null && id != null) {
  102. if (method == null) method = 'PUT';
  103. path =
  104. '/' +
  105. encodeURIComponent(index) +
  106. '/' +
  107. encodeURIComponent(type) +
  108. '/' +
  109. encodeURIComponent(id);
  110. } else if (index != null && id != null) {
  111. if (method == null) method = 'PUT';
  112. path = '/' + encodeURIComponent(index) + '/' + '_doc' + '/' + encodeURIComponent(id);
  113. } else if (index != null && type != null) {
  114. if (method == null) method = 'POST';
  115. path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type);
  116. } else {
  117. if (method == null) method = 'POST';
  118. path = '/' + encodeURIComponent(index) + '/' + '_doc';
  119. }
  120. // build request object
  121. const request = {
  122. method,
  123. path,
  124. body: body || '',
  125. querystring,
  126. };
  127. return this.transport.request(request, options, callback);
  128. }
  129. module.exports = indexApi;