Skip to content

Commit 32500dd

Browse files
committed
[API] Add mpercolate back
1 parent c3a0c4d commit 32500dd

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module Elasticsearch
2+
module API
3+
module Actions
4+
5+
# Perform multiple percolate operations in a single request, similar to the {#msearch} API
6+
#
7+
# Pass the percolate definitions as header-body pairs in the `:body` argument, as an Array of Hashes.
8+
#
9+
# @example Perform two different percolations in a single request
10+
#
11+
# client.mpercolate \
12+
# body: [
13+
# { percolate: { index: "my-index", type: "my-type" } },
14+
# { doc: { message: "foo bar" } },
15+
# { percolate: { index: "my-other-index", type: "my-other-type", id: "1" } },
16+
# { }
17+
# ]
18+
#
19+
# @option arguments [String] :index The index of the document being count percolated to use as default
20+
# @option arguments [String] :type The type of the document being percolated to use as default.
21+
# @option arguments [Array<Hash>] The percolate request definitions (header & body pairs) (*Required*)
22+
# @option arguments [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
23+
# unavailable (missing or closed)
24+
# @option arguments [Boolean] :allow_no_indices Whether to ignore if a wildcard indices expression resolves into
25+
# no concrete indices. (This includes `_all` string or when no
26+
# indices have been specified)
27+
# @option arguments [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that are
28+
# open, closed or both. (options: open, closed)
29+
#
30+
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/search-percolate.html
31+
#
32+
def mpercolate(arguments={})
33+
raise ArgumentError, "Required argument 'body' missing" unless arguments[:body]
34+
method = HTTP_GET
35+
path = "_mpercolate"
36+
37+
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
38+
body = arguments[:body]
39+
40+
case
41+
when body.is_a?(Array)
42+
payload = body.map { |d| d.is_a?(String) ? d : Elasticsearch::API.serializer.dump(d) }
43+
payload << "" unless payload.empty?
44+
payload = payload.join("\n")
45+
else
46+
payload = body
47+
end
48+
49+
perform_request(method, path, params, payload).body
50+
end
51+
52+
# Register this action with its valid params when the module is loaded.
53+
#
54+
# @since 6.2.0
55+
ParamsRegistry.register(:mpercolate, [
56+
:ignore_unavailable,
57+
:allow_no_indices,
58+
:expand_wildcards,
59+
:percolate_format ].freeze)
60+
end
61+
end
62+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'spec_helper'
2+
3+
describe 'client#mpercolate' do
4+
5+
let(:expected_args) do
6+
[
7+
'GET',
8+
'_mpercolate',
9+
params,
10+
body
11+
]
12+
end
13+
14+
let(:body) do
15+
nil
16+
end
17+
18+
let(:params) do
19+
{}
20+
end
21+
22+
context 'when a body is provided as a document' do
23+
24+
let(:body) do
25+
"{\"percolate\":{\"index\":\"my-index\",\"type\":\"my-type\"}}\n{\"doc\":{\"message\":\"foo bar\"}}\n" +
26+
"{\"percolate\":{\"index\":\"my-other-index\",\"type\":\"my-other-type\",\"id\":\"1\"}}\n{}\n"
27+
end
28+
29+
it 'performs the request' do
30+
expect(client_double.mpercolate(body: [
31+
{ percolate: { index: "my-index", type: "my-type" } },
32+
{ doc: { message: "foo bar" } },
33+
{ percolate: { index: "my-other-index", type: "my-other-type", id: "1" } },
34+
{ }
35+
])).to eq({})
36+
end
37+
end
38+
39+
context 'when a body is provided as a string' do
40+
41+
let(:body) do
42+
%Q|{"foo":"bar"}\n{"moo":"lam"}|
43+
end
44+
45+
it 'performs the request' do
46+
expect(client_double.mpercolate(body: %Q|{"foo":"bar"}\n{"moo":"lam"}|)).to eq({})
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)