Source: api/api/search_shards.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. 'preference',
  34. 'routing',
  35. 'local',
  36. 'ignore_unavailable',
  37. 'allow_no_indices',
  38. 'expand_wildcards',
  39. 'pretty',
  40. 'human',
  41. 'error_trace',
  42. 'source',
  43. 'filter_path',
  44. ];
  45. const snakeCase = {
  46. ignoreUnavailable: 'ignore_unavailable',
  47. allowNoIndices: 'allow_no_indices',
  48. expandWildcards: 'expand_wildcards',
  49. errorTrace: 'error_trace',
  50. filterPath: 'filter_path',
  51. };
  52. /**
  53. * Returns information about the indices and shards that a search request would be executed against.
  54. *
  55. * @memberOf API-Search
  56. *
  57. * @param {Object} params
  58. * @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
  59. * @param {string} [params.preference] - Specify the node or shard the operation should be performed on (default: random)
  60. * @param {string} [params.routing] - Specific routing value
  61. * @param {boolean} [params.local] - Return local information, do not retrieve the state from cluster_manager node (default: false)
  62. * @param {boolean} [params.ignore_unavailable] - Whether specified concrete indices should be ignored when unavailable (missing or closed)
  63. * @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)
  64. * @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)
  65. *
  66. * @param {Object} options - Options for {@link Transport#request}
  67. * @param {function} callback - Callback that handles errors and response
  68. *
  69. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  70. */
  71. function searchShardsApi(params, options, callback) {
  72. [params, options, callback] = normalizeArguments(params, options, callback);
  73. let { method, body, index, ...querystring } = params;
  74. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  75. let path = '';
  76. if (index != null) {
  77. if (method == null) method = body == null ? 'GET' : 'POST';
  78. path = '/' + encodeURIComponent(index) + '/' + '_search_shards';
  79. } else {
  80. if (method == null) method = body == null ? 'GET' : 'POST';
  81. path = '/' + '_search_shards';
  82. }
  83. // build request object
  84. const request = {
  85. method,
  86. path,
  87. body: body || '',
  88. querystring,
  89. };
  90. return this.transport.request(request, options, callback);
  91. }
  92. module.exports = searchShardsApi;