Source: api/api/msearch_template.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. 'typed_keys',
  35. 'max_concurrent_searches',
  36. 'rest_total_hits_as_int',
  37. 'ccs_minimize_roundtrips',
  38. 'pretty',
  39. 'human',
  40. 'error_trace',
  41. 'source',
  42. 'filter_path',
  43. ];
  44. const snakeCase = {
  45. searchType: 'search_type',
  46. typedKeys: 'typed_keys',
  47. maxConcurrentSearches: 'max_concurrent_searches',
  48. restTotalHitsAsInt: 'rest_total_hits_as_int',
  49. ccsMinimizeRoundtrips: 'ccs_minimize_roundtrips',
  50. errorTrace: 'error_trace',
  51. filterPath: 'filter_path',
  52. };
  53. /**
  54. * Allows to execute several search template operations in one request.
  55. *
  56. * @memberOf API-Search
  57. *
  58. * @param {Object} params
  59. * @param {string} params.index - A comma-separated list of index names to use as default
  60. * @param {Object} params.body - The request definitions (metadata-search request definition pairs), separated by newlines
  61. * @param {string} [params.search_type] - Search operation type (options: query_then_fetch, dfs_query_then_fetch)
  62. * @param {boolean} [params.typed_keys] - Specify whether aggregation and suggester names should be prefixed by their respective types in the response
  63. * @param {number} [params.max_concurrent_searches] - Controls the maximum number of concurrent searches the multi search api will execute
  64. * @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
  65. * @param {boolean} [params.ccs_minimize_roundtrips] - Indicates whether network round-trips should be minimized as part of cross-cluster search requests execution
  66. *
  67. * @param {Object} options - Options for {@link Transport#request}
  68. * @param {function} callback - Callback that handles errors and response
  69. *
  70. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  71. */
  72. function msearchTemplateApi(params, options, callback) {
  73. [params, options, callback] = normalizeArguments(params, options, callback);
  74. // check required parameters
  75. if (params.body == null) {
  76. const err = new this[kConfigurationError]('Missing required parameter: body');
  77. return handleError(err, callback);
  78. }
  79. // check required url components
  80. if (params.type != null && params.index == null) {
  81. const err = new this[kConfigurationError]('Missing required parameter of the url: index');
  82. return handleError(err, callback);
  83. }
  84. let { method, body, index, type, ...querystring } = params;
  85. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  86. let path = '';
  87. if (index != null && type != null) {
  88. if (method == null) method = body == null ? 'GET' : 'POST';
  89. path =
  90. '/' +
  91. encodeURIComponent(index) +
  92. '/' +
  93. encodeURIComponent(type) +
  94. '/' +
  95. '_msearch' +
  96. '/' +
  97. 'template';
  98. } else if (index != null) {
  99. if (method == null) method = body == null ? 'GET' : 'POST';
  100. path = '/' + encodeURIComponent(index) + '/' + '_msearch' + '/' + 'template';
  101. } else {
  102. if (method == null) method = body == null ? 'GET' : 'POST';
  103. path = '/' + '_msearch' + '/' + 'template';
  104. }
  105. // build request object
  106. const request = {
  107. method,
  108. path,
  109. bulkBody: body,
  110. querystring,
  111. };
  112. return this.transport.request(request, options, callback);
  113. }
  114. module.exports = msearchTemplateApi;