Source: api/api/termvectors.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. 'term_statistics',
  34. 'field_statistics',
  35. 'fields',
  36. 'offsets',
  37. 'positions',
  38. 'payloads',
  39. 'preference',
  40. 'routing',
  41. 'realtime',
  42. 'version',
  43. 'version_type',
  44. 'pretty',
  45. 'human',
  46. 'error_trace',
  47. 'source',
  48. 'filter_path',
  49. ];
  50. const snakeCase = {
  51. termStatistics: 'term_statistics',
  52. fieldStatistics: 'field_statistics',
  53. versionType: 'version_type',
  54. errorTrace: 'error_trace',
  55. filterPath: 'filter_path',
  56. };
  57. /**
  58. * Returns information and statistics about terms in the fields of a particular document.
  59. *
  60. * @memberOf API-Document
  61. *
  62. * @param {Object} params
  63. * @param {string} params.index - The index in which the document resides.
  64. * @param {string} [params.id] - The id of the document, when not specified a doc param should be supplied.
  65. * @param {boolean} [params.term_statistics] - Specifies if total term frequency and document frequency should be returned.
  66. * @param {boolean} [params.field_statistics] - Specifies if document count, sum of document frequencies and sum of total term frequencies should be returned.
  67. * @param {string} [params.fields] - A comma-separated list of fields to return.
  68. * @param {boolean} [params.offsets] - Specifies if term offsets should be returned.
  69. * @param {boolean} [params.positions] - Specifies if term positions should be returned.
  70. * @param {boolean} [params.payloads] - Specifies if term payloads should be returned.
  71. * @param {string} [params.preference] - Specify the node or shard the operation should be performed on (default: random).
  72. * @param {string} [params.routing] - Specific routing value.
  73. * @param {boolean} [params.realtime] - Specifies if request is real-time as opposed to near-real-time (default: true).
  74. * @param {number} [params.version] - Explicit version number for concurrency control
  75. * @param {string} [params.version_type] - Specific version type (options: internal, external, external_gte, force)
  76. * @param {Object} [params.body] - Define parameters and or supply a document to get termvectors for. See documentation.
  77. *
  78. * @param {Object} options - Options for {@link Transport#request}
  79. * @param {function} callback - Callback that handles errors and response
  80. *
  81. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  82. */
  83. function termvectorsApi(params, options, callback) {
  84. [params, options, callback] = normalizeArguments(params, options, callback);
  85. // check required parameters
  86. if (params.index == null) {
  87. const err = new this[kConfigurationError]('Missing required parameter: index');
  88. return handleError(err, callback);
  89. }
  90. let { method, body, index, id, type, ...querystring } = params;
  91. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  92. let path = '';
  93. if (index != null && type != null && id != null) {
  94. if (method == null) method = body == null ? 'GET' : 'POST';
  95. path =
  96. '/' +
  97. encodeURIComponent(index) +
  98. '/' +
  99. encodeURIComponent(type) +
  100. '/' +
  101. encodeURIComponent(id) +
  102. '/' +
  103. '_termvectors';
  104. } else if (index != null && id != null) {
  105. if (method == null) method = body == null ? 'GET' : 'POST';
  106. path = '/' + encodeURIComponent(index) + '/' + '_termvectors' + '/' + encodeURIComponent(id);
  107. } else if (index != null && type != null) {
  108. if (method == null) method = body == null ? 'GET' : 'POST';
  109. path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_termvectors';
  110. } else {
  111. if (method == null) method = body == null ? 'GET' : 'POST';
  112. path = '/' + encodeURIComponent(index) + '/' + '_termvectors';
  113. }
  114. // build request object
  115. const request = {
  116. method,
  117. path,
  118. body: body || '',
  119. querystring,
  120. };
  121. return this.transport.request(request, options, callback);
  122. }
  123. module.exports = termvectorsApi;