Method: Elasticsearch::API::Actions#msearch
- Defined in:
- lib/elasticsearch/api/actions/msearch.rb
#msearch(arguments = {}) ⇒ Object
Run multiple searches. The format of the request is similar to the bulk API format and makes use of the newline delimited JSON (NDJSON) format. The structure is as follows:
“‘ header
body
header
body
“‘
This structure is specifically optimized to reduce parsing if a specific search ends up redirected to another node. IMPORTANT: The final line of data must end with a newline character ‘n`. Each newline character may be preceded by a carriage return `r`. When sending requests to this endpoint the `Content-Type` header should be set to `application/x-ndjson`.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/elasticsearch/api/actions/msearch.rb', line 87 def msearch(arguments = {}) request_opts = { endpoint: arguments[:endpoint] || 'msearch' } defined_params = [:index].each_with_object({}) do |variable, set_variables| set_variables[variable] = arguments[variable] if arguments.key?(variable) end request_opts[:defined_params] = defined_params unless defined_params.empty? raise ArgumentError, "Required argument 'body' missing" unless arguments[:body] arguments = arguments.clone headers = arguments.delete(:headers) || {} body = arguments.delete(:body) _index = arguments.delete(:index) method = Elasticsearch::API::HTTP_POST path = if _index "#{Utils.listify(_index)}/_msearch" else '_msearch' end params = Utils.process_params(arguments) if body.is_a?(Array) && body.any? { |d| d.key? :search } payload = body.each_with_object([]) do |item, sum| = item data = .delete(:search) sum << sum << data end.map { |item| Elasticsearch::API.serializer.dump(item) } payload << '' unless payload.empty? payload = payload.join("\n") elsif body.is_a?(Array) payload = body.map { |d| d.is_a?(String) ? d : Elasticsearch::API.serializer.dump(d) } payload << '' unless payload.empty? payload = payload.join("\n") else payload = body end Utils.update_ndjson_headers!(headers, transport..dig(:transport_options, :headers)) Elasticsearch::API::Response.new( perform_request(method, path, params, payload, headers, request_opts) ) end |