Source: api/api/scroll.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. 'scroll',
  34. 'scroll_id',
  35. 'rest_total_hits_as_int',
  36. 'pretty',
  37. 'human',
  38. 'error_trace',
  39. 'source',
  40. 'filter_path',
  41. ];
  42. const snakeCase = {
  43. scrollId: 'scroll_id',
  44. restTotalHitsAsInt: 'rest_total_hits_as_int',
  45. errorTrace: 'error_trace',
  46. filterPath: 'filter_path',
  47. };
  48. /**
  49. * Allows to retrieve a large numbers of results from a single search request.
  50. * <br/> See Also: {@link https://opensearch.org/docs/latest/api-reference/scroll/ OpenSearch - Scroll }
  51. *
  52. * @memberOf API-Search
  53. *
  54. * @param {Object} params
  55. * @param {string} [params.scroll_id] - The scroll ID *Deprecated*
  56. * @param {string} [params.scroll] - Specify how long a consistent view of the index should be maintained for scrolled search
  57. * @param {boolean} [params.rest_total_hits_as_int] - Indicates whether hits.total should be rendered as an integer or an object in the rest search response
  58. * @param {Object} [params.body] - The scroll ID if not passed by URL or query parameter.
  59. *
  60. * @param {Object} [options] - Options for {@link Transport#request}
  61. * @param {function} [callback] - Callback that handles errors and response
  62. *
  63. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  64. */
  65. function scrollApi(params, options, callback) {
  66. [params, options, callback] = normalizeArguments(params, options, callback);
  67. let { method, body, scrollId, scroll_id, ...querystring } = params;
  68. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  69. let path = '';
  70. if ((scroll_id || scrollId) != null) {
  71. if (method == null) method = body == null ? 'GET' : 'POST';
  72. path = '/' + '_search' + '/' + 'scroll' + '/' + encodeURIComponent(scroll_id || scrollId);
  73. } else {
  74. if (method == null) method = body == null ? 'GET' : 'POST';
  75. path = '/' + '_search' + '/' + 'scroll';
  76. }
  77. // build request object
  78. const request = {
  79. method,
  80. path,
  81. body: body || '',
  82. querystring,
  83. };
  84. return this.transport.request(request, options, callback);
  85. }
  86. module.exports = scrollApi;