Skip to content

Commit b59ac12

Browse files
committed
[XPACK] Adds fleet.msearch
1 parent 53ce5d3 commit b59ac12

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
module Elasticsearch
19+
module XPack
20+
module API
21+
module Fleet
22+
module Actions
23+
# Multi Search API where the search will only be executed after specified checkpoints are available due to a refresh. This API is designed for internal use by the fleet server project.
24+
# This functionality is Experimental and may be changed or removed
25+
# completely in a future release. Elastic will take a best effort approach
26+
# to fix any issues, but experimental features are not subject to the
27+
# support SLA of official GA features.
28+
#
29+
# @option arguments [String] :index The index name to use as the default
30+
# @option arguments [Hash] :headers Custom HTTP headers
31+
# @option arguments [Hash] :body The request definitions (metadata-fleet search request definition pairs), separated by newlines (*Required*)
32+
#
33+
# @see [TODO]
34+
#
35+
def msearch(arguments = {})
36+
raise ArgumentError, "Required argument 'body' missing" unless arguments[:body]
37+
38+
headers = arguments.delete(:headers) || {}
39+
40+
arguments = arguments.clone
41+
42+
_index = arguments.delete(:index)
43+
44+
method = Elasticsearch::API::HTTP_POST
45+
path = if _index
46+
"#{Elasticsearch::API::Utils.__listify(_index)}/_fleet/_fleet_msearch"
47+
else
48+
"_fleet/_fleet_msearch"
49+
end
50+
params = {}
51+
52+
body = arguments[:body]
53+
case
54+
when body.is_a?(Array) && body.any? { |d| d.has_key? :search }
55+
payload = body
56+
.inject([]) do |sum, item|
57+
meta = item
58+
data = meta.delete(:search)
59+
60+
sum << meta
61+
sum << data
62+
sum
63+
end
64+
.map { |item| Elasticsearch::API.serializer.dump(item) }
65+
payload << "" unless payload.empty?
66+
payload = payload.join("\n")
67+
when body.is_a?(Array)
68+
payload = body.map { |d| d.is_a?(String) ? d : Elasticsearch::API.serializer.dump(d) }
69+
payload << "" unless payload.empty?
70+
payload = payload.join("\n")
71+
else
72+
payload = body
73+
end
74+
75+
headers.merge!("Content-Type" => "application/x-ndjson")
76+
perform_request(method, path, params, payload, headers).body
77+
end
78+
end
79+
end
80+
end
81+
end
82+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
require 'spec_helper'
19+
20+
describe 'client#fleet.msearch' do
21+
let(:expected_args) do
22+
[
23+
'POST',
24+
'_fleet/_fleet_msearch',
25+
{},
26+
{},
27+
{ 'Content-Type' => 'application/x-ndjson' }
28+
]
29+
end
30+
31+
it 'performs the request' do
32+
expect(client_double.fleet.msearch(body: {})).to eq({})
33+
end
34+
35+
let(:client) do
36+
Class.new { include Elasticsearch::XPack::API }.new
37+
end
38+
39+
it 'requires the :body argument' do
40+
expect {
41+
client.fleet.msearch
42+
}.to raise_exception(ArgumentError)
43+
end
44+
end

0 commit comments

Comments
 (0)