Source: api/api/rank_eval.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. 'allow_no_indices',
  35. 'expand_wildcards',
  36. 'search_type',
  37. 'pretty',
  38. 'human',
  39. 'error_trace',
  40. 'source',
  41. 'filter_path',
  42. ];
  43. const snakeCase = {
  44. ignoreUnavailable: 'ignore_unavailable',
  45. allowNoIndices: 'allow_no_indices',
  46. expandWildcards: 'expand_wildcards',
  47. searchType: 'search_type',
  48. errorTrace: 'error_trace',
  49. filterPath: 'filter_path',
  50. };
  51. /**
  52. * Allows to evaluate the quality of ranked search results over a set of typical search queries
  53. * <br/> See Also: {@link https://opensearch.org/docs/latest/api-reference/rank-eval/ OpenSearch - Ranking evaluation}
  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 {Object} params.body - The ranking evaluation search definition, including search requests, document ratings and ranking metric definition
  60. * @param {boolean} [params.ignore_unavailable] - Whether specified concrete indices should be ignored when unavailable (missing or closed)
  61. * @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)
  62. * @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)
  63. * @param {string} [params.search_type] - Search operation type (options: query_then_fetch, dfs_query_then_fetch)
  64. *
  65. * @param {Object} [options] - Options for {@link Transport#request}
  66. * @param {function} [callback] - Callback that handles errors and response
  67. *
  68. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*} {@link https://opensearch.org/docs/latest/api-reference/rank-eval/#sample-response Ranking Evaluation Response}
  69. */
  70. function rankEvalApi(params, options, callback) {
  71. [params, options, callback] = normalizeArguments(params, options, callback);
  72. // check required parameters
  73. if (params.body == null) {
  74. const err = new this[kConfigurationError]('Missing required parameter: body');
  75. return handleError(err, callback);
  76. }
  77. let { method, body, index, ...querystring } = params;
  78. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  79. let path = '';
  80. if (index != null) {
  81. if (method == null) method = body == null ? 'GET' : 'POST';
  82. path = '/' + encodeURIComponent(index) + '/' + '_rank_eval';
  83. } else {
  84. if (method == null) method = body == null ? 'GET' : 'POST';
  85. path = '/' + '_rank_eval';
  86. }
  87. // build request object
  88. const request = {
  89. method,
  90. path,
  91. body: body || '',
  92. querystring,
  93. };
  94. return this.transport.request(request, options, callback);
  95. }
  96. module.exports = rankEvalApi;