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

97 lines
2.9 KiB
JavaScript

'use strict'
function buildXpackGraphExplore (opts) {
// eslint-disable-next-line no-unused-vars
const { makeRequest, ConfigurationError, result } = opts
/**
* Perform a [xpack.graph.explore](https://www.elastic.co/guide/en/elasticsearch/reference/current/graph-explore-api.html) request
*
* @param {list} index - A comma-separated list of index names to search; use `_all` or empty string to perform the operation on all indices
* @param {list} type - A comma-separated list of document types to search; leave empty to perform the operation on all types
* @param {string} routing - Specific routing value
* @param {time} timeout - Explicit operation timeout
* @param {object} body - Graph Query DSL
*/
return function xpackGraphExplore (params, callback) {
if (typeof params === 'function' || params == null) {
callback = params
params = {}
}
// promises support
if (callback == null) {
return new Promise((resolve, reject) => {
xpackGraphExplore(params, (err, body) => {
err ? reject(err) : resolve(body)
})
})
}
// check required url components
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 = [
'routing',
'timeout'
]
const acceptedQuerystringCamelCased = [
'routing',
'timeout'
]
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 = params.body == null ? 'GET' : 'POST'
}
// 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'], '_xpack', 'graph', '_explore']
const request = {
method,
path: '/' + parts.filter(Boolean).map(encodeURIComponent).join('/'),
querystring,
body: params.body || '',
headers: params.headers || null,
ignore,
requestTimeout: params.requestTimeout || null
}
return makeRequest(request, callback)
}
}
module.exports = buildXpackGraphExplore