123 lines
4.5 KiB
JavaScript
123 lines
4.5 KiB
JavaScript
// Licensed to Elasticsearch B.V under one or more agreements.
|
|
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
|
|
// See the LICENSE file in the project root for more information
|
|
|
|
'use strict'
|
|
|
|
/* eslint camelcase: 0 */
|
|
/* eslint no-unused-vars: 0 */
|
|
|
|
function buildCount (opts) {
|
|
// eslint-disable-next-line no-unused-vars
|
|
const { makeRequest, ConfigurationError, handleError, snakeCaseKeys } = opts
|
|
/**
|
|
* Perform a [count](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-count.html) request
|
|
*
|
|
* @param {list} index - A comma-separated list of indices to restrict the results
|
|
* @param {list} type - A comma-separated list of types to restrict the results
|
|
* @param {boolean} ignore_unavailable - Whether specified concrete indices should be ignored when unavailable (missing or closed)
|
|
* @param {boolean} ignore_throttled - Whether specified concrete, expanded or aliased indices should be ignored when throttled
|
|
* @param {boolean} 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)
|
|
* @param {enum} expand_wildcards - Whether to expand wildcard expression to concrete indices that are open, closed or both.
|
|
* @param {number} min_score - Include only documents with a specific `_score` value in the result
|
|
* @param {string} preference - Specify the node or shard the operation should be performed on (default: random)
|
|
* @param {list} routing - A comma-separated list of specific routing values
|
|
* @param {string} q - Query in the Lucene query string syntax
|
|
* @param {string} analyzer - The analyzer to use for the query string
|
|
* @param {boolean} analyze_wildcard - Specify whether wildcard and prefix queries should be analyzed (default: false)
|
|
* @param {enum} default_operator - The default operator for query string query (AND or OR)
|
|
* @param {string} df - The field to use as default where no field prefix is given in the query string
|
|
* @param {boolean} lenient - Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
|
|
* @param {number} terminate_after - The maximum count for each shard, upon reaching which the query execution will terminate early
|
|
* @param {object} body - A query to restrict the results specified with the Query DSL (optional)
|
|
*/
|
|
|
|
const acceptedQuerystring = [
|
|
'ignore_unavailable',
|
|
'ignore_throttled',
|
|
'allow_no_indices',
|
|
'expand_wildcards',
|
|
'min_score',
|
|
'preference',
|
|
'routing',
|
|
'q',
|
|
'analyzer',
|
|
'analyze_wildcard',
|
|
'default_operator',
|
|
'df',
|
|
'lenient',
|
|
'terminate_after',
|
|
'pretty',
|
|
'human',
|
|
'error_trace',
|
|
'source',
|
|
'filter_path'
|
|
]
|
|
|
|
const snakeCase = {
|
|
ignoreUnavailable: 'ignore_unavailable',
|
|
ignoreThrottled: 'ignore_throttled',
|
|
allowNoIndices: 'allow_no_indices',
|
|
expandWildcards: 'expand_wildcards',
|
|
minScore: 'min_score',
|
|
analyzeWildcard: 'analyze_wildcard',
|
|
defaultOperator: 'default_operator',
|
|
terminateAfter: 'terminate_after',
|
|
errorTrace: 'error_trace',
|
|
filterPath: 'filter_path'
|
|
}
|
|
|
|
return function count (params, options, callback) {
|
|
options = options || {}
|
|
if (typeof options === 'function') {
|
|
callback = options
|
|
options = {}
|
|
}
|
|
if (typeof params === 'function' || params == null) {
|
|
callback = params
|
|
params = {}
|
|
options = {}
|
|
}
|
|
|
|
// validate headers object
|
|
if (options.headers != null && typeof options.headers !== 'object') {
|
|
const err = new ConfigurationError(`Headers should be an object, instead got: ${typeof options.headers}`)
|
|
return handleError(err, callback)
|
|
}
|
|
|
|
var warnings = []
|
|
var { method, body, index, type, ...querystring } = params
|
|
querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring, warnings)
|
|
|
|
if (method == null) {
|
|
method = body == null ? 'GET' : 'POST'
|
|
}
|
|
|
|
var ignore = options.ignore
|
|
if (typeof ignore === 'number') {
|
|
options.ignore = [ignore]
|
|
}
|
|
|
|
var path = ''
|
|
|
|
if ((index) != null) {
|
|
path = '/' + encodeURIComponent(index) + '/' + '_count'
|
|
} else {
|
|
path = '/' + '_count'
|
|
}
|
|
|
|
// build request object
|
|
const request = {
|
|
method,
|
|
path,
|
|
body: body || '',
|
|
querystring
|
|
}
|
|
|
|
options.warnings = warnings.length === 0 ? null : warnings
|
|
return makeRequest(request, options, callback)
|
|
}
|
|
}
|
|
|
|
module.exports = buildCount
|