Files
elasticsearch-js/api/api/get_source.js
2018-11-15 17:46:33 +01:00

162 lines
4.8 KiB
JavaScript

'use strict'
function buildGetSource (opts) {
// eslint-disable-next-line no-unused-vars
const { makeRequest, ConfigurationError, result } = opts
/**
* Perform a [get_source](http://www.elastic.co/guide/en/elasticsearch/reference/master/docs-get.html) request
*
* @param {string} id - The document ID
* @param {string} index - The name of the index
* @param {string} type - The type of the document; use `_all` to fetch the first document matching the ID across all types
* @param {string} parent - The ID of the parent document
* @param {string} preference - Specify the node or shard the operation should be performed on (default: random)
* @param {boolean} realtime - Specify whether to perform the operation in realtime or search mode
* @param {boolean} refresh - Refresh the shard containing the document before performing the operation
* @param {string} routing - Specific routing value
* @param {list} _source - True or false to return the _source field or not, or a list of fields to return
* @param {list} _source_exclude - A list of fields to exclude from the returned _source field
* @param {list} _source_include - A list of fields to extract and return from the _source field
* @param {number} version - Explicit version number for concurrency control
* @param {enum} version_type - Specific version type
*/
return function getSource (params, callback) {
if (typeof params === 'function' || params == null) {
callback = params
params = {}
}
// promises support
if (callback == null) {
return new Promise((resolve, reject) => {
getSource(params, (err, body) => {
err ? reject(err) : resolve(body)
})
})
}
// check required parameters
if (params['id'] == null) {
return callback(
new ConfigurationError('Missing required parameter: id'),
result
)
}
if (params['index'] == null) {
return callback(
new ConfigurationError('Missing required parameter: index'),
result
)
}
if (params['type'] == null) {
return callback(
new ConfigurationError('Missing required parameter: type'),
result
)
}
if (params.body != null) {
return callback(
new ConfigurationError('This API does not require a body'),
result
)
}
// check required url components
if (params['id'] != null && (params['type'] == null || params['index'] == null)) {
return callback(
new ConfigurationError('Missing required parameter of the url: type, index'),
result
)
} else if (params['type'] != null && (params['index'] == null)) {
return callback(
new ConfigurationError('Missing required parameter of the url: index'),
result
)
}
// build querystring object
const querystring = {}
const keys = Object.keys(params)
const acceptedQuerystring = [
'parent',
'preference',
'realtime',
'refresh',
'routing',
'_source',
'_source_exclude',
'_source_include',
'version',
'version_type',
'pretty',
'human',
'error_trace',
'source',
'filter_path'
]
const acceptedQuerystringCamelCased = [
'parent',
'preference',
'realtime',
'refresh',
'routing',
'_source',
'_sourceExclude',
'_sourceInclude',
'version',
'versionType',
'pretty',
'human',
'errorTrace',
'source',
'filterPath'
]
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i]
if (acceptedQuerystring.indexOf(key) !== -1) {
querystring[key] = params[key]
} else {
var camelIndex = acceptedQuerystringCamelCased.indexOf(key)
if (camelIndex !== -1) {
querystring[acceptedQuerystring[camelIndex]] = params[key]
}
}
}
// configure http method
var method = params.method
if (method == null) {
method = 'GET'
}
// validate headers object
if (params.headers != null && typeof params.headers !== 'object') {
return callback(
new ConfigurationError(`Headers should be an object, instead got: ${typeof params.headers}`),
result
)
}
var ignore = params.ignore || null
if (typeof ignore === 'number') {
ignore = [ignore]
}
// build request object
const parts = [params['index'], params['type'], params['id'], '_source']
const request = {
method,
path: '/' + parts.filter(Boolean).map(encodeURIComponent).join('/'),
querystring,
body: null,
headers: params.headers || null,
ignore,
requestTimeout: params.requestTimeout || null
}
return makeRequest(request, callback)
}
}
module.exports = buildGetSource