Source: api/api/search_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. 'ignore_unavailable',
  34. 'ignore_throttled',
  35. 'allow_no_indices',
  36. 'expand_wildcards',
  37. 'preference',
  38. 'routing',
  39. 'scroll',
  40. 'search_type',
  41. 'explain',
  42. 'profile',
  43. 'typed_keys',
  44. 'rest_total_hits_as_int',
  45. 'ccs_minimize_roundtrips',
  46. 'pretty',
  47. 'human',
  48. 'error_trace',
  49. 'source',
  50. 'filter_path',
  51. ];
  52. const snakeCase = {
  53. ignoreUnavailable: 'ignore_unavailable',
  54. ignoreThrottled: 'ignore_throttled',
  55. allowNoIndices: 'allow_no_indices',
  56. expandWildcards: 'expand_wildcards',
  57. searchType: 'search_type',
  58. typedKeys: 'typed_keys',
  59. restTotalHitsAsInt: 'rest_total_hits_as_int',
  60. ccsMinimizeRoundtrips: 'ccs_minimize_roundtrips',
  61. errorTrace: 'error_trace',
  62. filterPath: 'filter_path',
  63. };
  64. /**
  65. * Allows to use the Mustache language to pre-render a search definition.
  66. *
  67. * @memberOf API-Search
  68. *
  69. * @param {Object} params
  70. * @param {string} params.index - A comma-separated list of index names to search; use `_all` or empty string to perform the operation on all indices
  71. * @param {Object} params.body - The search definition template and its params
  72. * @param {boolean} [params.ignore_unavailable] - Whether specified concrete indices should be ignored when unavailable (missing or closed)
  73. * @param {boolean} [params.ignore_throttled] - Whether specified concrete, expanded or aliased indices should be ignored when throttled
  74. * @param {boolean} [params.allow_no_indices] - Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
  75. * @param {string} [params.expand_wildcards] - Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)
  76. * @param {string} [params.preference] - Specify the node or shard the operation should be performed on (default: random)
  77. * @param {string} [params.routing] - A comma-separated list of specific routing values
  78. * @param {string} [params.scroll] - Specify how long a consistent view of the index should be maintained for scrolled search
  79. * @param {string} [params.search_type] - Search operation type (options: query_then_fetch, dfs_query_then_fetch)
  80. * @param {boolean} [params.explain] - Specify whether to return detailed information about score computation as part of a hit
  81. * @param {boolean} [params.profile] - Specify whether to profile the query execution
  82. * @param {boolean} [params.typed_keys] - Specify whether aggregation and suggester names should be prefixed by their respective types in the response
  83. * @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
  84. * @param {boolean} [params.ccs_minimize_roundtrips] - Indicates whether network round-trips should be minimized as part of cross-cluster search requests execution
  85. *
  86. * @param {Object} options - Options for {@link Transport#request}
  87. * @param {function} callback - Callback that handles errors and response
  88. *
  89. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  90. */
  91. function searchTemplateApi(params, options, callback) {
  92. [params, options, callback] = normalizeArguments(params, options, callback);
  93. // check required parameters
  94. if (params.body == null) {
  95. const err = new this[kConfigurationError]('Missing required parameter: body');
  96. return handleError(err, callback);
  97. }
  98. // check required url components
  99. if (params.type != null && params.index == null) {
  100. const err = new this[kConfigurationError]('Missing required parameter of the url: index');
  101. return handleError(err, callback);
  102. }
  103. let { method, body, index, type, ...querystring } = params;
  104. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  105. let path = '';
  106. if (index != null && type != null) {
  107. if (method == null) method = body == null ? 'GET' : 'POST';
  108. path =
  109. '/' +
  110. encodeURIComponent(index) +
  111. '/' +
  112. encodeURIComponent(type) +
  113. '/' +
  114. '_search' +
  115. '/' +
  116. 'template';
  117. } else if (index != null) {
  118. if (method == null) method = body == null ? 'GET' : 'POST';
  119. path = '/' + encodeURIComponent(index) + '/' + '_search' + '/' + 'template';
  120. } else {
  121. if (method == null) method = body == null ? 'GET' : 'POST';
  122. path = '/' + '_search' + '/' + 'template';
  123. }
  124. // build request object
  125. const request = {
  126. method,
  127. path,
  128. body: body || '',
  129. querystring,
  130. };
  131. return this.transport.request(request, options, callback);
  132. }
  133. module.exports = searchTemplateApi;