Commit e3a4f7eeae77ca978731e53853e115d9cc5c6017
Exists in
master
and in
21 other branches
Merge branch 'master' into bulk_updates
Showing
7 changed files
with
43 additions
and
3 deletions
Show diff stats
CHANGELOG.md
README.md
@@ -1148,7 +1148,13 @@ end | @@ -1148,7 +1148,13 @@ end | ||
1148 | Change timeout | 1148 | Change timeout |
1149 | 1149 | ||
1150 | ```ruby | 1150 | ```ruby |
1151 | -Searchkick.timeout = 5 # defaults to 10 | 1151 | +Searchkick.timeout = 15 # defaults to 10 |
1152 | +``` | ||
1153 | + | ||
1154 | +Set a lower timeout for searches [master] | ||
1155 | + | ||
1156 | +```ruby | ||
1157 | +Searchkick.search_timeout = 3 | ||
1152 | ``` | 1158 | ``` |
1153 | 1159 | ||
1154 | Change the search method name in `config/initializers/searchkick.rb` | 1160 | Change the search method name in `config/initializers/searchkick.rb` |
lib/searchkick.rb
@@ -8,6 +8,7 @@ require "searchkick/query" | @@ -8,6 +8,7 @@ require "searchkick/query" | ||
8 | require "searchkick/reindex_job" | 8 | require "searchkick/reindex_job" |
9 | require "searchkick/model" | 9 | require "searchkick/model" |
10 | require "searchkick/tasks" | 10 | require "searchkick/tasks" |
11 | +require "searchkick/middleware" | ||
11 | require "searchkick/logging" if defined?(Rails) | 12 | require "searchkick/logging" if defined?(Rails) |
12 | 13 | ||
13 | # background jobs | 14 | # background jobs |
@@ -30,6 +31,7 @@ module Searchkick | @@ -30,6 +31,7 @@ module Searchkick | ||
30 | attr_accessor :search_method_name | 31 | attr_accessor :search_method_name |
31 | attr_accessor :wordnet_path | 32 | attr_accessor :wordnet_path |
32 | attr_accessor :timeout | 33 | attr_accessor :timeout |
34 | + attr_writer :search_timeout | ||
33 | attr_accessor :models | 35 | attr_accessor :models |
34 | attr_writer :env | 36 | attr_writer :env |
35 | end | 37 | end |
@@ -43,13 +45,19 @@ module Searchkick | @@ -43,13 +45,19 @@ module Searchkick | ||
43 | Elasticsearch::Client.new( | 45 | Elasticsearch::Client.new( |
44 | url: ENV["ELASTICSEARCH_URL"], | 46 | url: ENV["ELASTICSEARCH_URL"], |
45 | transport_options: {request: {timeout: timeout}} | 47 | transport_options: {request: {timeout: timeout}} |
46 | - ) | 48 | + ) do |f| |
49 | + f.use Searchkick::Middleware | ||
50 | + end | ||
47 | end | 51 | end |
48 | 52 | ||
49 | class << self | 53 | class << self |
50 | attr_writer :client | 54 | attr_writer :client |
51 | end | 55 | end |
52 | 56 | ||
57 | + def self.search_timeout | ||
58 | + @search_timeout || timeout | ||
59 | + end | ||
60 | + | ||
53 | def self.server_version | 61 | def self.server_version |
54 | @server_version ||= client.info["version"]["number"] | 62 | @server_version ||= client.info["version"]["number"] |
55 | end | 63 | end |
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
1 | +require "faraday/middleware" | ||
2 | + | ||
3 | +module Searchkick | ||
4 | + class Middleware < Faraday::Middleware | ||
5 | + def call(env) | ||
6 | + if env.method == :get && env.url.path.to_s.end_with?("/_search") | ||
7 | + env.request.timeout = Searchkick.search_timeout | ||
8 | + end | ||
9 | + @app.call(env) | ||
10 | + end | ||
11 | + end | ||
12 | +end |
lib/searchkick/query.rb
@@ -335,7 +335,7 @@ module Searchkick | @@ -335,7 +335,7 @@ module Searchkick | ||
335 | raise ArgumentError, "boost_by_distance requires :field and :origin" | 335 | raise ArgumentError, "boost_by_distance requires :field and :origin" |
336 | end | 336 | end |
337 | function_params = boost_by_distance.select { |k, _| [:origin, :scale, :offset, :decay].include?(k) } | 337 | function_params = boost_by_distance.select { |k, _| [:origin, :scale, :offset, :decay].include?(k) } |
338 | - function_params[:origin] = function_params[:origin].reverse | 338 | + function_params[:origin] = location_value(function_params[:origin]) |
339 | custom_filters << { | 339 | custom_filters << { |
340 | boost_by_distance[:function] => { | 340 | boost_by_distance[:function] => { |
341 | boost_by_distance[:field] => function_params | 341 | boost_by_distance[:field] => function_params |
test/boost_test.rb
@@ -132,4 +132,13 @@ class BoostTest < Minitest::Test | @@ -132,4 +132,13 @@ class BoostTest < Minitest::Test | ||
132 | ] | 132 | ] |
133 | assert_order "san", ["San Francisco", "San Antonio", "San Marino"], boost_by_distance: {field: :location, origin: [37, -122], scale: "1000mi"} | 133 | assert_order "san", ["San Francisco", "San Antonio", "San Marino"], boost_by_distance: {field: :location, origin: [37, -122], scale: "1000mi"} |
134 | end | 134 | end |
135 | + | ||
136 | + def test_boost_by_distance_hash | ||
137 | + store [ | ||
138 | + {name: "San Francisco", latitude: 37.7833, longitude: -122.4167}, | ||
139 | + {name: "San Antonio", latitude: 29.4167, longitude: -98.5000}, | ||
140 | + {name: "San Marino", latitude: 43.9333, longitude: 12.4667} | ||
141 | + ] | ||
142 | + assert_order "san", ["San Francisco", "San Antonio", "San Marino"], boost_by_distance: {field: :location, origin: {lat: 37, lon: -122}, scale: "1000mi"} | ||
143 | + end | ||
135 | end | 144 | end |
test/test_helper.rb
@@ -11,6 +11,7 @@ Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test) | @@ -11,6 +11,7 @@ Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test) | ||
11 | 11 | ||
12 | File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") | 12 | File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") |
13 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") | 13 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") |
14 | +Searchkick.search_timeout = 5 | ||
14 | 15 | ||
15 | puts "Running against Elasticsearch #{Searchkick.server_version}" | 16 | puts "Running against Elasticsearch #{Searchkick.server_version}" |
16 | 17 |