Skip to content

Commit 9c1f951

Browse files
committed
[STORE] Changed, that search requests are executed through SearchRequest class
Extracted the `gateway.search` call into a specific class, `SearchRequest`, primarily to allow hooking into the `SearchRequest#execute!` method in Rails instrumentation. Updated unit tests, this change should be totally opaque to the user. Related: elastic#238
1 parent 0448d18 commit 9c1f951

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'elasticsearch'
22
require 'elasticsearch/model/indexing'
3+
require 'elasticsearch/model/searching'
34
require 'hashie'
45

56
require 'active_support/inflector'

elasticsearch-persistence/lib/elasticsearch/persistence/model.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def gateway(&block)
7575
:document_type=,
7676
:index_name,
7777
:index_name=,
78-
:search,
7978
:find,
8079
:exists?,
8180
:create_index!,

elasticsearch-persistence/lib/elasticsearch/persistence/model/find.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ module Persistence
33
module Model
44

55
module Find
6+
class SearchRequest < Elasticsearch::Model::Searching::SearchRequest
7+
def execute!
8+
klass.gateway.search(definition[:body] || definition[:q], options)
9+
end
10+
end
11+
612
module ClassMethods
713

14+
def search(query_or_definition, options={})
15+
SearchRequest.new(self, query_or_definition, options).execute!
16+
end
17+
818
# Returns all models (up to 10,000)
919
#
1020
# @example Retrieve all people
@@ -17,8 +27,9 @@ module ClassMethods
1727
# Person.all query: { match: { last_name: 'Smith' } }
1828
# # => [#<Person:0x007ff1d8fb04b0 ... ]
1929
#
20-
def all(options={})
21-
gateway.search( { query: { match_all: {} }, size: 10_000 }.merge(options) )
30+
def all(query={ query: { match_all: {} } }, options={})
31+
query[:size] ||= 10_000
32+
search(query, options)
2233
end
2334

2435
# Returns the number of models

elasticsearch-persistence/test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
exit(0) if RUBY_1_8
44

5+
$LOAD_PATH.unshift File.expand_path('../../../elasticsearch-model/lib', __FILE__) if File.exists? File.expand_path('../../../elasticsearch-model/lib', __FILE__)
6+
57
require 'simplecov' and SimpleCov.start { add_filter "/test|test_/" } if ENV["COVERAGE"]
68

79
# Register `at_exit` handler for integration tests shutdown.

elasticsearch-persistence/test/unit/model_find_test.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ class DummyFindModel
3131
end
3232

3333
setup do
34-
@gateway = stub(client: stub(), index_name: 'foo', document_type: 'bar')
34+
@gateway = stub(client: stub(), index_name: 'foo', document_type: 'bar')
3535
DummyFindModel.stubs(:gateway).returns(@gateway)
3636

37+
DummyFindModel.stubs(:index_name).returns('foo')
38+
DummyFindModel.stubs(:document_type).returns('bar')
39+
3740
@response = MultiJson.load <<-JSON
3841
{
3942
"took": 14,
@@ -63,21 +66,21 @@ class DummyFindModel
6366
end
6467

6568
should "find all records" do
66-
@gateway
67-
.expects(:search)
68-
.with({ query: { match_all: {} }, size: 10_000 })
69+
DummyFindModel
70+
.stubs(:search)
71+
.with({ query: { match_all: {} }, size: 10_000 }, {})
6972
.returns(@response)
7073

7174
DummyFindModel.all
7275
end
7376

7477
should "pass options when finding all records" do
75-
@gateway
78+
DummyFindModel
7679
.expects(:search)
77-
.with({ query: { match: { title: 'test' } }, size: 10_000, routing: 'abc123' })
80+
.with({ query: { match: { title: 'test' } }, size: 10_000 }, { routing: 'abc123' })
7881
.returns(@response)
7982

80-
DummyFindModel.all( { query: { match: { title: 'test' } }, routing: 'abc123' } )
83+
DummyFindModel.all( { query: { match: { title: 'test' } } }, { routing: 'abc123' } )
8184
end
8285

8386
context "finding via scan/scroll" do

0 commit comments

Comments
 (0)