|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +'use strict' |
| 6 | + |
| 7 | +/* eslint camelcase: 0 */ |
| 8 | +/* eslint no-unused-vars: 0 */ |
| 9 | + |
| 10 | +function buildAsyncSearchSubmit (opts) { |
| 11 | + // eslint-disable-next-line no-unused-vars |
| 12 | + const { makeRequest, ConfigurationError, handleError, snakeCaseKeys } = opts |
| 13 | + |
| 14 | + const acceptedQuerystring = [ |
| 15 | + 'wait_for_completion_timeout', |
| 16 | + 'keep_on_completion', |
| 17 | + 'keep_alive', |
| 18 | + 'batched_reduce_size', |
| 19 | + 'request_cache', |
| 20 | + 'analyzer', |
| 21 | + 'analyze_wildcard', |
| 22 | + 'default_operator', |
| 23 | + 'df', |
| 24 | + 'explain', |
| 25 | + 'stored_fields', |
| 26 | + 'docvalue_fields', |
| 27 | + 'from', |
| 28 | + 'ignore_unavailable', |
| 29 | + 'ignore_throttled', |
| 30 | + 'allow_no_indices', |
| 31 | + 'expand_wildcards', |
| 32 | + 'lenient', |
| 33 | + 'preference', |
| 34 | + 'q', |
| 35 | + 'routing', |
| 36 | + 'search_type', |
| 37 | + 'size', |
| 38 | + 'sort', |
| 39 | + '_source', |
| 40 | + '_source_excludes', |
| 41 | + '_source_exclude', |
| 42 | + '_source_includes', |
| 43 | + '_source_include', |
| 44 | + 'terminate_after', |
| 45 | + 'stats', |
| 46 | + 'suggest_field', |
| 47 | + 'suggest_mode', |
| 48 | + 'suggest_size', |
| 49 | + 'suggest_text', |
| 50 | + 'timeout', |
| 51 | + 'track_scores', |
| 52 | + 'track_total_hits', |
| 53 | + 'allow_partial_search_results', |
| 54 | + 'typed_keys', |
| 55 | + 'version', |
| 56 | + 'seq_no_primary_term', |
| 57 | + 'max_concurrent_shard_requests' |
| 58 | + ] |
| 59 | + |
| 60 | + const snakeCase = { |
| 61 | + waitForCompletionTimeout: 'wait_for_completion_timeout', |
| 62 | + keepOnCompletion: 'keep_on_completion', |
| 63 | + keepAlive: 'keep_alive', |
| 64 | + batchedReduceSize: 'batched_reduce_size', |
| 65 | + requestCache: 'request_cache', |
| 66 | + analyzeWildcard: 'analyze_wildcard', |
| 67 | + defaultOperator: 'default_operator', |
| 68 | + storedFields: 'stored_fields', |
| 69 | + docvalueFields: 'docvalue_fields', |
| 70 | + ignoreUnavailable: 'ignore_unavailable', |
| 71 | + ignoreThrottled: 'ignore_throttled', |
| 72 | + allowNoIndices: 'allow_no_indices', |
| 73 | + expandWildcards: 'expand_wildcards', |
| 74 | + searchType: 'search_type', |
| 75 | + _sourceExcludes: '_source_excludes', |
| 76 | + _sourceExclude: '_source_exclude', |
| 77 | + _sourceIncludes: '_source_includes', |
| 78 | + _sourceInclude: '_source_include', |
| 79 | + terminateAfter: 'terminate_after', |
| 80 | + suggestField: 'suggest_field', |
| 81 | + suggestMode: 'suggest_mode', |
| 82 | + suggestSize: 'suggest_size', |
| 83 | + suggestText: 'suggest_text', |
| 84 | + trackScores: 'track_scores', |
| 85 | + trackTotalHits: 'track_total_hits', |
| 86 | + allowPartialSearchResults: 'allow_partial_search_results', |
| 87 | + typedKeys: 'typed_keys', |
| 88 | + seqNoPrimaryTerm: 'seq_no_primary_term', |
| 89 | + maxConcurrentShardRequests: 'max_concurrent_shard_requests' |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Perform a async_search.submit request |
| 94 | + * Executes a search request asynchronously. |
| 95 | + * https://www.elastic.co/guide/en/elasticsearch/reference/current/async-search.html |
| 96 | + */ |
| 97 | + return function asyncSearchSubmit (params, options, callback) { |
| 98 | + options = options || {} |
| 99 | + if (typeof options === 'function') { |
| 100 | + callback = options |
| 101 | + options = {} |
| 102 | + } |
| 103 | + if (typeof params === 'function' || params == null) { |
| 104 | + callback = params |
| 105 | + params = {} |
| 106 | + options = {} |
| 107 | + } |
| 108 | + |
| 109 | + // validate headers object |
| 110 | + if (options.headers != null && typeof options.headers !== 'object') { |
| 111 | + const err = new ConfigurationError(`Headers should be an object, instead got: ${typeof options.headers}`) |
| 112 | + return handleError(err, callback) |
| 113 | + } |
| 114 | + |
| 115 | + var warnings = [] |
| 116 | + var { method, body, index, ...querystring } = params |
| 117 | + querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring, warnings) |
| 118 | + |
| 119 | + var ignore = options.ignore |
| 120 | + if (typeof ignore === 'number') { |
| 121 | + options.ignore = [ignore] |
| 122 | + } |
| 123 | + |
| 124 | + var path = '' |
| 125 | + |
| 126 | + if ((index) != null) { |
| 127 | + if (method == null) method = 'POST' |
| 128 | + path = '/' + encodeURIComponent(index) + '/' + '_async_search' |
| 129 | + } else { |
| 130 | + if (method == null) method = 'POST' |
| 131 | + path = '/' + '_async_search' |
| 132 | + } |
| 133 | + |
| 134 | + // build request object |
| 135 | + const request = { |
| 136 | + method, |
| 137 | + path, |
| 138 | + body: body || '', |
| 139 | + querystring |
| 140 | + } |
| 141 | + |
| 142 | + options.warnings = warnings.length === 0 ? null : warnings |
| 143 | + return makeRequest(request, options, callback) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +module.exports = buildAsyncSearchSubmit |
0 commit comments