Source: api/api/msearch.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. 'search_type',
  34. 'max_concurrent_searches',
  35. 'typed_keys',
  36. 'pre_filter_shard_size',
  37. 'max_concurrent_shard_requests',
  38. 'rest_total_hits_as_int',
  39. 'ccs_minimize_roundtrips',
  40. 'pretty',
  41. 'human',
  42. 'error_trace',
  43. 'source',
  44. 'filter_path',
  45. ];
  46. const snakeCase = {
  47. searchType: 'search_type',
  48. maxConcurrentSearches: 'max_concurrent_searches',
  49. typedKeys: 'typed_keys',
  50. preFilterShardSize: 'pre_filter_shard_size',
  51. maxConcurrentShardRequests: 'max_concurrent_shard_requests',
  52. restTotalHitsAsInt: 'rest_total_hits_as_int',
  53. ccsMinimizeRoundtrips: 'ccs_minimize_roundtrips',
  54. errorTrace: 'error_trace',
  55. filterPath: 'filter_path',
  56. };
  57. /**
  58. * Allows to execute several search operations in one request.
  59. * <br/> See Also: {@link https://opensearch.org/docs/latest/api-reference/multi-search/ OpenSearch - Multi-Search}
  60. *
  61. * @memberOf API-Search
  62. *
  63. * @param {Object} params
  64. * @param {string} params.index - A comma-separated list of index names to use as default
  65. * @param {Object} params.body - The request definitions (metadata-search request definition pairs), separated by newlines
  66. * @param {string} [params.search_type] - Search operation type (options: query_then_fetch, dfs_query_then_fetch)
  67. * @param {number} [params.max_concurrent_searches] - Controls the maximum number of concurrent searches the multi search api will execute
  68. * @param {boolean} [params.typed_keys] - Specify whether aggregation and suggester names should be prefixed by their respective types in the response
  69. * @param {number} [params.pre_filter_shard_size] - A threshold that enforces a pre-filter roundtrip to prefilter search shards based on query rewriting if the number of shards the search request expands to exceeds the threshold. This filter roundtrip can limit the number of shards significantly if for instance a shard can not match any documents based on its rewrite method ie. if date filters are mandatory to match but the shard bounds and the query are disjoint.
  70. * @param {number} [params.max_concurrent_shard_requests] - The number of concurrent shard requests each sub search executes concurrently per node. This value should be used to limit the impact of the search on the cluster in order to limit the number of concurrent shard requests
  71. * @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
  72. * @param {boolean} [params.ccs_minimize_roundtrips] - Indicates whether network round-trips should be minimized as part of cross-cluster search requests execution
  73. *
  74. * @param {Object} options - Options for {@link Transport#request}
  75. * @param {function} callback - Callback that handles errors and response
  76. *
  77. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/latest/api-reference/multi-search/#response Multi-search Response}
  78. */
  79. function msearchApi(params, options, callback) {
  80. [params, options, callback] = normalizeArguments(params, options, callback);
  81. // check required parameters
  82. if (params.body == null) {
  83. const err = new this[kConfigurationError]('Missing required parameter: body');
  84. return handleError(err, callback);
  85. }
  86. // check required url components
  87. if (params.type != null && params.index == null) {
  88. const err = new this[kConfigurationError]('Missing required parameter of the url: index');
  89. return handleError(err, callback);
  90. }
  91. let { method, body, index, type, ...querystring } = params;
  92. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  93. let path = '';
  94. if (index != null && type != null) {
  95. if (method == null) method = body == null ? 'GET' : 'POST';
  96. path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_msearch';
  97. } else if (index != null) {
  98. if (method == null) method = body == null ? 'GET' : 'POST';
  99. path = '/' + encodeURIComponent(index) + '/' + '_msearch';
  100. } else {
  101. if (method == null) method = body == null ? 'GET' : 'POST';
  102. path = '/' + '_msearch';
  103. }
  104. // build request object
  105. const request = {
  106. method,
  107. path,
  108. bulkBody: body,
  109. querystring,
  110. };
  111. return this.transport.request(request, options, callback);
  112. }
  113. module.exports = msearchApi;