Source: api/api/get.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. 'stored_fields',
  34. 'preference',
  35. 'realtime',
  36. 'refresh',
  37. 'routing',
  38. '_source',
  39. '_source_excludes',
  40. '_source_exclude',
  41. '_source_includes',
  42. '_source_include',
  43. 'version',
  44. 'version_type',
  45. 'pretty',
  46. 'human',
  47. 'error_trace',
  48. 'source',
  49. 'filter_path',
  50. ];
  51. const snakeCase = {
  52. storedFields: 'stored_fields',
  53. _sourceExcludes: '_source_excludes',
  54. _sourceExclude: '_source_exclude',
  55. _sourceIncludes: '_source_includes',
  56. _sourceInclude: '_source_include',
  57. versionType: 'version_type',
  58. errorTrace: 'error_trace',
  59. filterPath: 'filter_path',
  60. };
  61. /**
  62. * Retrieve a document
  63. * <br/> See Also: {@link https://opensearch.org/docs/2.4/api-reference/document-apis/get-documents/ OpenSearch - Get Document}
  64. *
  65. * @memberOf API-Document
  66. *
  67. * @param {Object} params
  68. * @param {string} params.index - Name of the index.
  69. * @param {string} params.id - Document ID.
  70. * @param {string} [params.preference] - Specifies a preference of which shard to retrieve results from. Available options are '_local', which tells the operation to retrieve results from a locally allocated shard replica, and a custom string value assigned to a specific shard replica. By default, OpenSearch executes get document operations on random shards.
  71. * @param {boolean} [params.realtime=true] - Specifies whether the operation should run in realtime. If false, the operation waits for the index to refresh to analyze the source to retrieve data, which makes the operation near-realtime.
  72. * @param {boolean} [params.refresh=false] - If true, OpenSearch refreshes shards to make the get operation available to search results. Valid options are 'true', 'false', and 'wait_for', which tells OpenSearch to wait for a refresh before executing the operation.
  73. * @param {string} [params.routing] - A value used to route the operation to a specific shard.
  74. * @param {boolean} [params.stored_fields=false] - Whether the get operation should retrieve fields stored in the index.
  75. * @param {string} [params._source=true] - Whether to include the '_source' field in the response body.
  76. * @param {string} [params._source_excludes] - A comma-separated list of source fields to exclude in the query response.
  77. * @param {string} [params._source_includes] - A comma-separated list of source fields to include in the query response.
  78. * @param {number} [params.version] - The document’s version number.
  79. * @param {number} [params.version_type] - Specific version type (options: 'external' and 'external_gte')
  80. *
  81. * @param {Object} options - Options for {@link Transport#request}
  82. * @param {function} callback - Callback that handles errors and response
  83. *
  84. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/2.4/api-reference/document-apis/get-documents/#response Get Response}
  85. */
  86. function getApi(params, options, callback) {
  87. [params, options, callback] = normalizeArguments(params, options, callback);
  88. // check required parameters
  89. if (params.id == null) {
  90. const err = new this[kConfigurationError]('Missing required parameter: id');
  91. return handleError(err, callback);
  92. }
  93. if (params.index == null) {
  94. const err = new this[kConfigurationError]('Missing required parameter: index');
  95. return handleError(err, callback);
  96. }
  97. let { method, body, id, index, type, ...querystring } = params;
  98. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  99. let path = '';
  100. if (index != null && type != null && id != null) {
  101. if (method == null) method = 'GET';
  102. path =
  103. '/' +
  104. encodeURIComponent(index) +
  105. '/' +
  106. encodeURIComponent(type) +
  107. '/' +
  108. encodeURIComponent(id);
  109. } else {
  110. if (method == null) method = 'GET';
  111. path = '/' + encodeURIComponent(index) + '/' + '_doc' + '/' + encodeURIComponent(id);
  112. }
  113. // build request object
  114. const request = {
  115. method,
  116. path,
  117. body: null,
  118. querystring,
  119. };
  120. return this.transport.request(request, options, callback);
  121. }
  122. module.exports = getApi;