Commit 0d976e94b3cd2cb688f92e487c0350a599d648ad
1 parent
f786c804
Exists in
master
and in
21 other branches
Improved tests [skip ci]
Showing
4 changed files
with
32 additions
and
30 deletions
Show diff stats
lib/searchkick.rb
@@ -74,6 +74,28 @@ module Searchkick | @@ -74,6 +74,28 @@ module Searchkick | ||
74 | Gem::Version.new(server_version.sub("-", ".")) < Gem::Version.new(version.sub("-", ".")) | 74 | Gem::Version.new(server_version.sub("-", ".")) < Gem::Version.new(version.sub("-", ".")) |
75 | end | 75 | end |
76 | 76 | ||
77 | + def self.search(term = nil, options = {}, &block) | ||
78 | + query = Searchkick::Query.new(nil, term, options) | ||
79 | + block.call(query.body) if block | ||
80 | + if options[:execute] == false | ||
81 | + query | ||
82 | + else | ||
83 | + query.execute | ||
84 | + end | ||
85 | + end | ||
86 | + | ||
87 | + def self.multi_search(queries) | ||
88 | + if queries.any? | ||
89 | + responses = client.msearch(body: queries.flat_map { |q| [q.params.except(:body), q.body] })["responses"] | ||
90 | + queries.each_with_index do |query, i| | ||
91 | + query.handle_response(responses[i]) | ||
92 | + end | ||
93 | + end | ||
94 | + nil | ||
95 | + end | ||
96 | + | ||
97 | + # callbacks | ||
98 | + | ||
77 | def self.enable_callbacks | 99 | def self.enable_callbacks |
78 | self.callbacks_value = nil | 100 | self.callbacks_value = nil |
79 | end | 101 | end |
@@ -121,26 +143,6 @@ module Searchkick | @@ -121,26 +143,6 @@ module Searchkick | ||
121 | def self.callbacks_value=(value) | 143 | def self.callbacks_value=(value) |
122 | Thread.current[:searchkick_callbacks_enabled] = value | 144 | Thread.current[:searchkick_callbacks_enabled] = value |
123 | end | 145 | end |
124 | - | ||
125 | - def self.search(term = nil, options = {}, &block) | ||
126 | - query = Searchkick::Query.new(nil, term, options) | ||
127 | - block.call(query.body) if block | ||
128 | - if options[:execute] == false | ||
129 | - query | ||
130 | - else | ||
131 | - query.execute | ||
132 | - end | ||
133 | - end | ||
134 | - | ||
135 | - def self.multi_search(queries) | ||
136 | - if queries.any? | ||
137 | - responses = client.msearch(body: queries.flat_map { |q| [q.params.except(:body), q.body] })["responses"] | ||
138 | - queries.each_with_index do |query, i| | ||
139 | - query.handle_response(responses[i]) | ||
140 | - end | ||
141 | - end | ||
142 | - nil | ||
143 | - end | ||
144 | end | 146 | end |
145 | 147 | ||
146 | # TODO find better ActiveModel hook | 148 | # TODO find better ActiveModel hook |
test/highlight_test.rb
@@ -41,10 +41,10 @@ class HighlightTest < Minitest::Test | @@ -41,10 +41,10 @@ class HighlightTest < Minitest::Test | ||
41 | assert_equal "<b><em>Hello</em></b>", Product.search("hello", fields: [:name], highlight: {encoder: "html"}, misspellings: false).with_details.first[1][:highlight][:name] | 41 | assert_equal "<b><em>Hello</em></b>", Product.search("hello", fields: [:name], highlight: {encoder: "html"}, misspellings: false).with_details.first[1][:highlight][:name] |
42 | end | 42 | end |
43 | 43 | ||
44 | - def test_json | 44 | + def test_body |
45 | skip if ENV["MATCH"] == "word_start" | 45 | skip if ENV["MATCH"] == "word_start" |
46 | store_names ["Two Door Cinema Club"] | 46 | store_names ["Two Door Cinema Club"] |
47 | - json = { | 47 | + body = { |
48 | query: { | 48 | query: { |
49 | match: { | 49 | match: { |
50 | "name.analyzed" => "cinema" | 50 | "name.analyzed" => "cinema" |
@@ -58,6 +58,6 @@ class HighlightTest < Minitest::Test | @@ -58,6 +58,6 @@ class HighlightTest < Minitest::Test | ||
58 | } | 58 | } |
59 | } | 59 | } |
60 | } | 60 | } |
61 | - assert_equal "Two Door <strong>Cinema</strong> Club", Product.search(json: json).with_details.first[1][:highlight][:"name.analyzed"] | 61 | + assert_equal "Two Door <strong>Cinema</strong> Club", Product.search(body: body).with_details.first[1][:highlight][:"name.analyzed"] |
62 | end | 62 | end |
63 | end | 63 | end |
test/index_test.rb
@@ -93,7 +93,7 @@ class IndexTest < Minitest::Test | @@ -93,7 +93,7 @@ class IndexTest < Minitest::Test | ||
93 | end | 93 | end |
94 | 94 | ||
95 | def test_missing_index | 95 | def test_missing_index |
96 | - assert_raises(Searchkick::MissingIndexError) { Product.search "test", index_name: "not_found" } | 96 | + assert_raises(Searchkick::MissingIndexError) { Product.search("test", index_name: "not_found") } |
97 | end | 97 | end |
98 | 98 | ||
99 | def test_unsupported_version | 99 | def test_unsupported_version |
@@ -103,8 +103,8 @@ class IndexTest < Minitest::Test | @@ -103,8 +103,8 @@ class IndexTest < Minitest::Test | ||
103 | end | 103 | end |
104 | end | 104 | end |
105 | 105 | ||
106 | - def test_invalid_query | ||
107 | - assert_raises(Searchkick::InvalidQueryError) { Product.search(query: {boom: true}) } | 106 | + def test_invalid_body |
107 | + assert_raises(Searchkick::InvalidQueryError) { Product.search(body: {boom: true}) } | ||
108 | end | 108 | end |
109 | 109 | ||
110 | def test_transaction | 110 | def test_transaction |
test/sql_test.rb
@@ -76,9 +76,9 @@ class SqlTest < Minitest::Test | @@ -76,9 +76,9 @@ class SqlTest < Minitest::Test | ||
76 | assert_equal "Product A", Product.search("product", load: false).first.name | 76 | assert_equal "Product A", Product.search("product", load: false).first.name |
77 | end | 77 | end |
78 | 78 | ||
79 | - def test_load_false_with_include | 79 | + def test_load_false_with_includes |
80 | store_names ["Product A"] | 80 | store_names ["Product A"] |
81 | - assert_kind_of Hash, Product.search("product", load: false, include: [:store]).first | 81 | + assert_kind_of Hash, Product.search("product", load: false, includes: [:store]).first |
82 | end | 82 | end |
83 | 83 | ||
84 | def test_load_false_nested_object | 84 | def test_load_false_nested_object |
@@ -233,9 +233,9 @@ class SqlTest < Minitest::Test | @@ -233,9 +233,9 @@ class SqlTest < Minitest::Test | ||
233 | 233 | ||
234 | # other tests | 234 | # other tests |
235 | 235 | ||
236 | - def test_include | 236 | + def test_includes |
237 | skip unless defined?(ActiveRecord) | 237 | skip unless defined?(ActiveRecord) |
238 | store_names ["Product A"] | 238 | store_names ["Product A"] |
239 | - assert Product.search("product", include: [:store]).first.association(:store).loaded? | 239 | + assert Product.search("product", includes: [:store]).first.association(:store).loaded? |
240 | end | 240 | end |
241 | end | 241 | end |