Source: api/api/count.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. /** @namespace API-Count */
  32. const { handleError, snakeCaseKeys, normalizeArguments, kConfigurationError } = require('../utils');
  33. const acceptedQuerystring = [
  34. 'ignore_unavailable',
  35. 'ignore_throttled',
  36. 'allow_no_indices',
  37. 'expand_wildcards',
  38. 'min_score',
  39. 'preference',
  40. 'routing',
  41. 'q',
  42. 'analyzer',
  43. 'analyze_wildcard',
  44. 'default_operator',
  45. 'df',
  46. 'lenient',
  47. 'terminate_after',
  48. 'pretty',
  49. 'human',
  50. 'error_trace',
  51. 'source',
  52. 'filter_path',
  53. ];
  54. const snakeCase = {
  55. ignoreUnavailable: 'ignore_unavailable',
  56. ignoreThrottled: 'ignore_throttled',
  57. allowNoIndices: 'allow_no_indices',
  58. expandWildcards: 'expand_wildcards',
  59. minScore: 'min_score',
  60. analyzeWildcard: 'analyze_wildcard',
  61. defaultOperator: 'default_operator',
  62. terminateAfter: 'terminate_after',
  63. errorTrace: 'error_trace',
  64. filterPath: 'filter_path',
  65. };
  66. /**
  67. * The count API gives you quick access to the number of documents that match a query. You can also use it to check the document count of an index, data stream, or cluster.
  68. * <br/> See Also: {@link https://opensearch.org/docs/latest/api-reference/count/ OpenSearch - Bulk}
  69. *
  70. * @memberOf API-Count
  71. *
  72. * @param {Object} params
  73. * @param {boolean} [params.allow_no_indices=false] - If false, the request returns an error if any wildcard expression or index alias targets any closed or missing indices.
  74. * @param {string} [params.analyzer] - The analyzer to use in the query string.
  75. * @param {boolean} [params.analyze_wildcard=false] - Specifies whether to analyze wildcard and prefix queries.
  76. * @param {string} [params.default_operator='OR'] - Indicates whether the default operator for a string query should be 'AND' or 'OR'.
  77. * @param {string} [params.df] - The default field in case a field prefix is not provided in the query string.
  78. * @param {string} [params.expand_wildcards=open] - Expands wildcard expressions to concrete indices. Combine multiple values with commas. Supported values are 'all', 'open', 'closed', 'hidden', and 'none'.
  79. * @param {boolean} [params.ignore_unavailable=false] - Specifies whether to include missing or closed indices in the response.
  80. * @param {boolean} [params.lenient=false] - Specifies whether OpenSearch should accept requests if queries have format errors (for example, querying a text field for an integer).
  81. * @param {number} [params.min_score] - Include only documents with a minimum '_score' value in the result.
  82. * @param {string} [params.routing] - Value used to route the operation to a specific shard.
  83. * @param {string} [params.preference] - Specifies which shard or node OpenSearch should perform the count operation on.
  84. * @param {number} [params.terminate_after] - The maximum number of documents OpenSearch should process before terminating the request.
  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>|*} {@link https://opensearch.org/docs/latest/api-reference/count/#response Count Response}
  90. */
  91. function countApi(params, options, callback) {
  92. [params, options, callback] = normalizeArguments(params, options, callback);
  93. // check required url components
  94. if (params.type != null && params.index == null) {
  95. const err = new this[kConfigurationError]('Missing required parameter of the url: index');
  96. return handleError(err, callback);
  97. }
  98. let { method, body, index, type, ...querystring } = params;
  99. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  100. let path = '';
  101. if (index != null && type != null) {
  102. if (method == null) method = body == null ? 'GET' : 'POST';
  103. path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_count';
  104. } else if (index != null) {
  105. if (method == null) method = body == null ? 'GET' : 'POST';
  106. path = '/' + encodeURIComponent(index) + '/' + '_count';
  107. } else {
  108. if (method == null) method = body == null ? 'GET' : 'POST';
  109. path = '/' + '_count';
  110. }
  111. // build request object
  112. const request = {
  113. method,
  114. path,
  115. body: body || '',
  116. querystring,
  117. };
  118. return this.transport.request(request, options, callback);
  119. }
  120. module.exports = countApi;