Skip to content

Commit 31008c3

Browse files
committed
[API] Adds knn_search
1 parent 454cad9 commit 31008c3

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 API
20+
module Actions
21+
# Performs a kNN search.
22+
# This functionality is Experimental and may be changed or removed
23+
# completely in a future release. Elastic will take a best effort approach
24+
# to fix any issues, but experimental features are not subject to the
25+
# support SLA of official GA features.
26+
#
27+
# @option arguments [List] :index A comma-separated list of index names to search; use `_all` or empty string to perform the operation on all indices
28+
# @option arguments [List] :routing A comma-separated list of specific routing values
29+
# @option arguments [Hash] :headers Custom HTTP headers
30+
# @option arguments [Hash] :body The search definition
31+
#
32+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/master/search-search.html
33+
#
34+
def knn_search(arguments = {})
35+
raise ArgumentError, "Required argument 'index' missing" unless arguments[:index]
36+
37+
headers = arguments.delete(:headers) || {}
38+
39+
body = arguments.delete(:body)
40+
41+
arguments = arguments.clone
42+
43+
_index = arguments.delete(:index)
44+
45+
method = if body
46+
Elasticsearch::API::HTTP_POST
47+
else
48+
Elasticsearch::API::HTTP_GET
49+
end
50+
51+
path = "#{Utils.__listify(_index)}/_knn_search"
52+
params = Utils.process_params(arguments)
53+
54+
perform_request(method, path, params, body, headers).body
55+
end
56+
end
57+
end
58+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 'test_helper'
19+
20+
module Elasticsearch
21+
module Test
22+
class XPackInfoTest < Minitest::Test
23+
context 'Knn Search' do
24+
subject { FakeClient.new }
25+
26+
should "perform correct request" do
27+
subject.expects(:perform_request).with do |method, url, params, body|
28+
assert_equal('GET', method)
29+
assert_equal('foo/_knn_search', url)
30+
assert_equal({}, params)
31+
assert_nil(body)
32+
true
33+
end.returns(FakeResponse.new)
34+
35+
subject.knn_search(index: 'foo')
36+
end
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)