128 lines
4.7 KiB
JavaScript
128 lines
4.7 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 buildBulk (opts) {
|
|
// eslint-disable-next-line no-unused-vars
|
|
const { makeRequest, ConfigurationError, handleError, snakeCaseKeys } = opts
|
|
/**
|
|
* Perform a [bulk](http://www.elastic.co/guide/en/elasticsearch/reference/master/docs-bulk.html) request
|
|
*
|
|
* @param {string} index - Default index for items which don't provide one
|
|
* @param {string} type - Default document type for items which don't provide one
|
|
* @param {string} wait_for_active_shards - Sets the number of shard copies that must be active before proceeding with the bulk operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1)
|
|
* @param {enum} refresh - If `true` then refresh the effected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes.
|
|
* @param {string} routing - Specific routing value
|
|
* @param {time} timeout - Explicit operation timeout
|
|
* @param {string} type - Default document type for items which don't provide one
|
|
* @param {list} fields - Default comma-separated list of fields to return in the response for updates, can be overridden on each sub-request
|
|
* @param {list} _source - True or false to return the _source field or not, or default list of fields to return, can be overridden on each sub-request
|
|
* @param {list} _source_excludes - Default list of fields to exclude from the returned _source field, can be overridden on each sub-request
|
|
* @param {list} _source_includes - Default list of fields to extract and return from the _source field, can be overridden on each sub-request
|
|
* @param {string} pipeline - The pipeline id to preprocess incoming documents with
|
|
* @param {object} body - The operation definition and data (action-data pairs), separated by newlines
|
|
*/
|
|
|
|
const acceptedQuerystring = [
|
|
'wait_for_active_shards',
|
|
'refresh',
|
|
'routing',
|
|
'timeout',
|
|
'type',
|
|
'fields',
|
|
'_source',
|
|
'_source_excludes',
|
|
'_source_exclude',
|
|
'_source_includes',
|
|
'_source_include',
|
|
'pipeline',
|
|
'pretty',
|
|
'human',
|
|
'error_trace',
|
|
'source',
|
|
'filter_path'
|
|
]
|
|
|
|
const snakeCase = {
|
|
waitForActiveShards: 'wait_for_active_shards',
|
|
_sourceExcludes: '_source_excludes',
|
|
_sourceExclude: '_source_exclude',
|
|
_sourceIncludes: '_source_includes',
|
|
_sourceInclude: '_source_include',
|
|
errorTrace: 'error_trace',
|
|
filterPath: 'filter_path'
|
|
}
|
|
|
|
return function bulk (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['body'] == null) {
|
|
const err = new ConfigurationError('Missing required parameter: body')
|
|
return handleError(err, callback)
|
|
}
|
|
|
|
// check required url components
|
|
if (params['type'] != null && (params['index'] == null)) {
|
|
const err = new ConfigurationError('Missing required parameter of the url: 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, index, type, ...querystring } = params
|
|
querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring, warnings)
|
|
|
|
if (method == null) {
|
|
method = 'POST'
|
|
}
|
|
|
|
var ignore = options.ignore
|
|
if (typeof ignore === 'number') {
|
|
options.ignore = [ignore]
|
|
}
|
|
|
|
var path = ''
|
|
|
|
if ((index) != null && (type) != null) {
|
|
path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_bulk'
|
|
} else if ((index) != null) {
|
|
path = '/' + encodeURIComponent(index) + '/' + '_bulk'
|
|
} else {
|
|
path = '/' + '_bulk'
|
|
}
|
|
|
|
// build request object
|
|
const request = {
|
|
method,
|
|
path,
|
|
bulkBody: body,
|
|
querystring
|
|
}
|
|
|
|
options.warnings = warnings.length === 0 ? null : warnings
|
|
return makeRequest(request, options, callback)
|
|
}
|
|
}
|
|
|
|
module.exports = buildBulk
|