133 lines
4.5 KiB
JavaScript
133 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 buildExplain (opts) {
|
|
// eslint-disable-next-line no-unused-vars
|
|
const { makeRequest, ConfigurationError, handleError, snakeCaseKeys } = opts
|
|
/**
|
|
* Perform a [explain](http://www.elastic.co/guide/en/elasticsearch/reference/master/search-explain.html) request
|
|
*
|
|
* @param {string} id - The document ID
|
|
* @param {string} index - The name of the index
|
|
* @param {string} type - The type of the document
|
|
* @param {boolean} analyze_wildcard - Specify whether wildcards and prefix queries in the query string query should be analyzed (default: false)
|
|
* @param {string} analyzer - The analyzer for the query string query
|
|
* @param {enum} default_operator - The default operator for query string query (AND or OR)
|
|
* @param {string} df - The default field for query string query (default: _all)
|
|
* @param {list} stored_fields - A comma-separated list of stored fields to return in the response
|
|
* @param {boolean} lenient - Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
|
|
* @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 {string} q - Query in the Lucene query string syntax
|
|
* @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_excludes - A list of fields to exclude from the returned _source field
|
|
* @param {list} _source_includes - A list of fields to extract and return from the _source field
|
|
* @param {object} body - The query definition using the Query DSL
|
|
*/
|
|
|
|
const acceptedQuerystring = [
|
|
'analyze_wildcard',
|
|
'analyzer',
|
|
'default_operator',
|
|
'df',
|
|
'stored_fields',
|
|
'lenient',
|
|
'parent',
|
|
'preference',
|
|
'q',
|
|
'routing',
|
|
'_source',
|
|
'_source_excludes',
|
|
'_source_exclude',
|
|
'_source_includes',
|
|
'_source_include',
|
|
'pretty',
|
|
'human',
|
|
'error_trace',
|
|
'source',
|
|
'filter_path'
|
|
]
|
|
|
|
const snakeCase = {
|
|
analyzeWildcard: 'analyze_wildcard',
|
|
defaultOperator: 'default_operator',
|
|
storedFields: 'stored_fields',
|
|
_sourceExcludes: '_source_excludes',
|
|
_sourceExclude: '_source_exclude',
|
|
_sourceIncludes: '_source_includes',
|
|
_sourceInclude: '_source_include',
|
|
errorTrace: 'error_trace',
|
|
filterPath: 'filter_path'
|
|
}
|
|
|
|
return function explain (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)
|
|
}
|
|
if (params['type'] == null) {
|
|
const err = new ConfigurationError('Missing required parameter: type')
|
|
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)
|
|
|
|
if (method == null) {
|
|
method = body == null ? 'GET' : 'POST'
|
|
}
|
|
|
|
var ignore = options.ignore
|
|
if (typeof ignore === 'number') {
|
|
options.ignore = [ignore]
|
|
}
|
|
|
|
var path = ''
|
|
|
|
path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + encodeURIComponent(id) + '/' + '_explain'
|
|
|
|
// build request object
|
|
const request = {
|
|
method,
|
|
path,
|
|
body: body || '',
|
|
querystring
|
|
}
|
|
|
|
options.warnings = warnings.length === 0 ? null : warnings
|
|
return makeRequest(request, options, callback)
|
|
}
|
|
}
|
|
|
|
module.exports = buildExplain
|