Source: api/api/create.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. 'version',
  38. 'version_type',
  39. 'pipeline',
  40. 'pretty',
  41. 'human',
  42. 'error_trace',
  43. 'source',
  44. 'filter_path',
  45. ];
  46. const snakeCase = {
  47. waitForActiveShards: 'wait_for_active_shards',
  48. versionType: 'version_type',
  49. errorTrace: 'error_trace',
  50. filterPath: 'filter_path',
  51. };
  52. /**
  53. * Adds a document with a predefined ID to an index.
  54. * <br/> See Also: {@link https://opensearch.org/docs/2.4/api-reference/document-apis/index-document/ OpenSearch - Index Document}
  55. *
  56. * @memberOf API-Document
  57. *
  58. * @param {Object} params
  59. * @param {string} params.index - Name of the index.
  60. * @param {string} params.id - A unique identifier to attach to the document.
  61. * @param {Object} params.body - The content of the document.
  62. * @param {number} [params.if_seq_no] - Only perform the index operation if the document has the specified sequence number.
  63. * @param {number} [params.if_primary_term] - Only perform the index operation if the document has the specified primary term.
  64. * @param {string} [params.pipeline] - Route the index operation to a certain pipeline.
  65. * @param {string} [params.routing] - value used to assign the index operation to a specific shard.
  66. * @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.
  67. * @param {string} [params.timeout=1m] - How long to wait for a response from the cluster.
  68. * @param {number} [params.version] - The document’s version number.
  69. * @param {number} [params.version_type] - Specific version type (options: 'external' and 'external_gte')
  70. * @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.
  71. * @param {boolean} [params.require_alias=false] - Specifies whether the target index must be an index alias.
  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/index-document/#response Index Response}
  77. */
  78. function createApi(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. if (params.body == null) {
  90. const err = new this[kConfigurationError]('Missing required parameter: body');
  91. return handleError(err, callback);
  92. }
  93. let { method, body, id, index, type, ...querystring } = params;
  94. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  95. let path = '';
  96. if (index != null && type != null && id != null) {
  97. if (method == null) method = 'PUT';
  98. path =
  99. '/' +
  100. encodeURIComponent(index) +
  101. '/' +
  102. encodeURIComponent(type) +
  103. '/' +
  104. encodeURIComponent(id) +
  105. '/' +
  106. '_create';
  107. } else {
  108. if (method == null) method = 'PUT';
  109. path = '/' + encodeURIComponent(index) + '/' + '_create' + '/' + encodeURIComponent(id);
  110. }
  111. // build request object
  112. const request = {
  113. method,
  114. path,
  115. body: body || '',
  116. querystring,
  117. };
  118. return this.transport.request(request, options, callback);
  119. }
  120. module.exports = createApi;