Skip to content

Commit 0ff8d71

Browse files
committed
[API] Add percolate back
1 parent 4c3b5f7 commit 0ff8d71

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module Elasticsearch
2+
module API
3+
module Actions
4+
5+
# Return names of queries matching a document.
6+
#
7+
# Percolator allows you to register queries and then evaluate a document against them:
8+
# the IDs of matching queries are returned in the response.
9+
#
10+
# @deprecated The `_percolate` API has been deprecated in favour of a special field mapping and the
11+
# `percolate` query;
12+
# see https://www.elastic.co/guide/en/elasticsearch/reference/5.5/breaking_50_percolator.html
13+
#
14+
# See full example for Elasticsearch 5.x and higher in <https://github.com/elastic/elasticsearch-ruby/blob/master/examples/percolator/percolator_alerts.rb>
15+
#
16+
# @option arguments [String] :index The index of the document being percolated. (*Required*)
17+
# @option arguments [String] :type The type of the document being percolated. (*Required*)
18+
# @option arguments [String] :id Fetch the document specified by index/type/id and
19+
# use it instead of the passed `doc`
20+
# @option arguments [Hash] :body The percolator request definition using the percolate DSL
21+
# @option arguments [List] :routing A comma-separated list of specific routing values
22+
# @option arguments [String] :preference Specify the node or shard the operation should be performed on
23+
# (default: random)
24+
# @option arguments [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
25+
# unavailable (missing or closed)
26+
# @option arguments [Boolean] :allow_no_indices Whether to ignore if a wildcard indices expression resolves into
27+
# no concrete indices. (This includes `_all` string or when no
28+
# indices have been specified)
29+
# @option arguments [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that are
30+
# open, closed or both. (options: open, closed)
31+
# @option arguments [String] :percolate_index The index to percolate the document into. Defaults to passed `index`.
32+
# @option arguments [String] :percolate_format Return an array of matching query IDs instead of objects.
33+
# (options: ids)
34+
# @option arguments [String] :percolate_type The type to percolate document into. Defaults to passed `type`.
35+
# @option arguments [Number] :version Explicit version number for concurrency control
36+
# @option arguments [String] :version_type Specific version type (options: internal, external, external_gte, force)
37+
#
38+
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/search-percolate.html
39+
#
40+
def percolate(arguments={})
41+
Utils.__report_unsupported_method :percolate
42+
43+
raise ArgumentError, "Required argument 'index' missing" unless arguments[:index]
44+
raise ArgumentError, "Required argument 'type' missing" unless arguments[:type]
45+
method = HTTP_GET
46+
path = Utils.__pathify Utils.__escape(arguments[:index]),
47+
Utils.__escape(arguments[:type]),
48+
Utils.__escape(arguments[:id]),
49+
'_percolate'
50+
51+
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
52+
body = arguments[:body]
53+
54+
perform_request(method, path, params, body).body
55+
end
56+
57+
# Register this action with its valid params when the module is loaded.
58+
#
59+
# @since 6.2.0
60+
ParamsRegistry.register(:percolate, [
61+
:routing,
62+
:preference,
63+
:ignore_unavailable,
64+
:allow_no_indices,
65+
:expand_wildcards,
66+
:percolate_index,
67+
:percolate_type,
68+
:percolate_format,
69+
:version,
70+
:version_type ].freeze)
71+
end
72+
end
73+
end
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper'
2+
3+
describe 'client#percolate' do
4+
5+
let(:expected_args) do
6+
[
7+
'GET',
8+
url,
9+
{ },
10+
body
11+
]
12+
end
13+
14+
let(:body) do
15+
{ doc: { foo: 'bar' }}
16+
end
17+
18+
let(:url) do
19+
'foo/bar/_percolate'
20+
end
21+
22+
let(:client) do
23+
Class.new { include Elasticsearch::API }.new
24+
end
25+
26+
it 'requires the :index argument' do
27+
expect {
28+
client.percolate(type: 'bar', body: {})
29+
}.to raise_exception(ArgumentError)
30+
end
31+
32+
it 'requires the :type argument' do
33+
expect {
34+
client.percolate(index: 'foo', body: {})
35+
}.to raise_exception(ArgumentError)
36+
end
37+
38+
it 'performs the request' do
39+
expect(client_double.percolate(index: 'foo', type: 'bar', body: { doc: { foo: 'bar' } })).to eq({})
40+
end
41+
42+
context 'when the request needs to be URL-escaped' do
43+
44+
let(:url) do
45+
'foo%5Ebar/bar%2Fbam/_percolate'
46+
end
47+
48+
it 'URL-escapes the parts' do
49+
expect(client_double.percolate(index: 'foo^bar', type: 'bar/bam', body: { doc: { foo: 'bar' } })).to eq({})
50+
end
51+
end
52+
53+
context 'when the document id needs to be URL-escaped' do
54+
55+
let(:url) do
56+
'foo%5Ebar/bar%2Fbam/some%2Fid/_percolate'
57+
end
58+
59+
let(:body) do
60+
nil
61+
end
62+
63+
it 'URL-escapes the id' do
64+
expect(client_double.percolate(index: 'foo^bar', type: 'bar/bam', id: 'some/id')).to eq({})
65+
end
66+
end
67+
end

0 commit comments

Comments
 (0)