Source: api/api/explain.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. 'analyze_wildcard',
  34. 'analyzer',
  35. 'default_operator',
  36. 'df',
  37. 'stored_fields',
  38. 'lenient',
  39. 'preference',
  40. 'q',
  41. 'routing',
  42. '_source',
  43. '_source_excludes',
  44. '_source_exclude',
  45. '_source_includes',
  46. '_source_include',
  47. 'pretty',
  48. 'human',
  49. 'error_trace',
  50. 'source',
  51. 'filter_path',
  52. ];
  53. const snakeCase = {
  54. analyzeWildcard: 'analyze_wildcard',
  55. defaultOperator: 'default_operator',
  56. storedFields: 'stored_fields',
  57. _sourceExcludes: '_source_excludes',
  58. _sourceExclude: '_source_exclude',
  59. _sourceIncludes: '_source_includes',
  60. _sourceInclude: '_source_include',
  61. errorTrace: 'error_trace',
  62. filterPath: 'filter_path',
  63. };
  64. /**
  65. * Returns information about why a specific matches (or doesn't match) a query.
  66. * <br/> See also: {@link https://opensearch.org/docs/latest/api-reference/explain/ OpenSearch - Explain}
  67. * @memberOf API-Search
  68. *
  69. * @param {Object} params
  70. * @param {string} [params.id] - The document ID
  71. * @param {string} [params.index] - The name of the index
  72. * @param {Object} [params.body] - The query definition using the Query DSL
  73. * @param {string} [params.analyzer] - The analyzer to use for the query string
  74. * @param {boolean} [params.analyze_wildcard=false] - Specify whether wildcard and prefix queries should be analyzed
  75. * @param {string} [params.default_operator=OR] - The default operator for query string query (options: AND, OR)
  76. * @param {string} [params.df] - The default field for query string query (default: _all)
  77. * @param {string} [params.stored_fields] - A comma-separated list of stored fields to return in the response
  78. * @param {boolean} [params.lenient] - Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
  79. * @param {string} [params.preference] - Specify the node or shard the operation should be performed on (default: random)
  80. * @param {string} [params.q] - Query in the Lucene query string syntax
  81. * @param {string} [params.routing] - Specific routing value
  82. * @param {string} [params._source=true] - Whether to include the '_source' field in the response body.
  83. * @param {string} [params._source_excludes] - A comma-separated list of source fields to exclude in the query response.
  84. * @param {string} [params._source_includes] - A comma-separated list of source fields to include in the query response.
  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 explainApi(params, options, callback) {
  92. [params, options, callback] = normalizeArguments(params, options, callback);
  93. // check required parameters
  94. if (params.id == null) {
  95. const err = new this[kConfigurationError]('Missing required parameter: id');
  96. return handleError(err, callback);
  97. }
  98. if (params.index == null) {
  99. const err = new this[kConfigurationError]('Missing required parameter: index');
  100. return handleError(err, callback);
  101. }
  102. let { method, body, id, index, type, ...querystring } = params;
  103. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  104. let path = '';
  105. if (index != null && type != null && id != null) {
  106. if (method == null) method = body == null ? 'GET' : 'POST';
  107. path =
  108. '/' +
  109. encodeURIComponent(index) +
  110. '/' +
  111. encodeURIComponent(type) +
  112. '/' +
  113. encodeURIComponent(id) +
  114. '/' +
  115. '_explain';
  116. } else {
  117. if (method == null) method = body == null ? 'GET' : 'POST';
  118. path = '/' + encodeURIComponent(index) + '/' + '_explain' + '/' + encodeURIComponent(id);
  119. }
  120. // build request object
  121. const request = {
  122. method,
  123. path,
  124. body: body || '',
  125. querystring,
  126. };
  127. return this.transport.request(request, options, callback);
  128. }
  129. module.exports = explainApi;