107 lines
2.9 KiB
JavaScript
107 lines
2.9 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 buildExists (opts) {
|
|
// eslint-disable-next-line no-unused-vars
|
|
const { makeRequest, ConfigurationError, handleError, snakeCaseKeys } = opts
|
|
|
|
const acceptedQuerystring = [
|
|
'stored_fields',
|
|
'preference',
|
|
'realtime',
|
|
'refresh',
|
|
'routing',
|
|
'_source',
|
|
'_source_excludes',
|
|
'_source_includes',
|
|
'version',
|
|
'version_type',
|
|
'pretty',
|
|
'human',
|
|
'error_trace',
|
|
'source',
|
|
'filter_path'
|
|
]
|
|
|
|
const snakeCase = {
|
|
storedFields: 'stored_fields',
|
|
_sourceExcludes: '_source_excludes',
|
|
_sourceIncludes: '_source_includes',
|
|
versionType: 'version_type',
|
|
errorTrace: 'error_trace',
|
|
filterPath: 'filter_path'
|
|
}
|
|
|
|
/**
|
|
* Perform a exists request
|
|
* Returns information about whether a document exists in an index.
|
|
* https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-get.html
|
|
*/
|
|
return function exists (params, options, callback) {
|
|
options = options || {}
|
|
if (typeof options === 'function') {
|
|
callback = options
|
|
options = {}
|
|
}
|
|
if (typeof params === 'function' || params == null) {
|
|
callback = params
|
|
params = {}
|
|
options = {}
|
|
}
|
|
|
|
// check required parameters
|
|
if (params['id'] == null) {
|
|
const err = new ConfigurationError('Missing required parameter: id')
|
|
return handleError(err, callback)
|
|
}
|
|
if (params['index'] == null) {
|
|
const err = new ConfigurationError('Missing required parameter: index')
|
|
return handleError(err, callback)
|
|
}
|
|
|
|
// 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, id, index, type, ...querystring } = params
|
|
querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring, warnings)
|
|
|
|
var ignore = options.ignore
|
|
if (typeof ignore === 'number') {
|
|
options.ignore = [ignore]
|
|
}
|
|
|
|
var path = ''
|
|
|
|
if ((index) != null && (type) != null && (id) != null) {
|
|
if (method == null) method = 'HEAD'
|
|
path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + encodeURIComponent(id)
|
|
} else {
|
|
if (method == null) method = 'HEAD'
|
|
path = '/' + encodeURIComponent(index) + '/' + '_doc' + '/' + encodeURIComponent(id)
|
|
}
|
|
|
|
// build request object
|
|
const request = {
|
|
method,
|
|
path,
|
|
body: null,
|
|
querystring
|
|
}
|
|
|
|
options.warnings = warnings.length === 0 ? null : warnings
|
|
return makeRequest(request, options, callback)
|
|
}
|
|
}
|
|
|
|
module.exports = buildExists
|