Source: api/api/mget.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. 'pretty',
  44. 'human',
  45. 'error_trace',
  46. 'source',
  47. 'filter_path',
  48. ];
  49. const snakeCase = {
  50. storedFields: 'stored_fields',
  51. _sourceExcludes: '_source_excludes',
  52. _sourceExclude: '_source_exclude',
  53. _sourceIncludes: '_source_includes',
  54. _sourceInclude: '_source_include',
  55. errorTrace: 'error_trace',
  56. filterPath: 'filter_path',
  57. };
  58. /**
  59. * Retrieve multiple documents
  60. * <br/> See Also: {@link https://opensearch.org/docs/2.4/api-reference/document-apis/multi-get/ OpenSearch - Multi-get Document}
  61. *
  62. * @memberOf API-Document
  63. *
  64. * @param {Object} params
  65. * @param {string} params.index - Name of the index.
  66. * @param {string} params.id - Document ID.
  67. * @param {string} params.body - {@link https://opensearch.org/docs/2.4/api-reference/document-apis/multi-get/#request-body Multi-get Request Body}
  68. * @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.
  69. * @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.
  70. * @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.
  71. * @param {string} [params.routing] - A value used to route the operation to a specific shard.
  72. * @param {boolean} [params.stored_fields=false] - Whether the get operation should retrieve fields stored in the index.
  73. * @param {string} [params._source=true] - Whether to include the '_source' field in the response body.
  74. * @param {string} [params._source_excludes] - A comma-separated list of source fields to exclude in the query response.
  75. * @param {string} [params._source_includes] - A comma-separated list of source fields to include in the query response.
  76. *
  77. * @param {Object} options - Options for {@link Transport#request}
  78. * @param {function} callback - Callback that handles errors and response
  79. *
  80. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/2.4/api-reference/document-apis/multi-get/#response Multi-get Response}
  81. */
  82. function mgetApi(params, options, callback) {
  83. [params, options, callback] = normalizeArguments(params, options, callback);
  84. // check required parameters
  85. if (params.body == null) {
  86. const err = new this[kConfigurationError]('Missing required parameter: body');
  87. return handleError(err, callback);
  88. }
  89. // check required url components
  90. if (params.type != null && params.index == null) {
  91. const err = new this[kConfigurationError]('Missing required parameter of the url: index');
  92. return handleError(err, callback);
  93. }
  94. let { method, body, index, type, ...querystring } = params;
  95. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  96. let path = '';
  97. if (index != null && type != null) {
  98. if (method == null) method = body == null ? 'GET' : 'POST';
  99. path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_mget';
  100. } else if (index != null) {
  101. if (method == null) method = body == null ? 'GET' : 'POST';
  102. path = '/' + encodeURIComponent(index) + '/' + '_mget';
  103. } else {
  104. if (method == null) method = body == null ? 'GET' : 'POST';
  105. path = '/' + '_mget';
  106. }
  107. // build request object
  108. const request = {
  109. method,
  110. path,
  111. body: body || '',
  112. querystring,
  113. };
  114. return this.transport.request(request, options, callback);
  115. }
  116. module.exports = mgetApi;