Commit b7410431dc9ad062e13df184c2643320eb8e8c9e
1 parent
0339d7cf
Exists in
master
and in
19 other branches
Improved syntax for boost_by_distance
Showing
3 changed files
with
36 additions
and
9 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/query.rb
... | ... | @@ -459,17 +459,25 @@ module Searchkick |
459 | 459 | |
460 | 460 | def set_boost_by_distance(custom_filters) |
461 | 461 | boost_by_distance = options[:boost_by_distance] || {} |
462 | - boost_by_distance = {function: :gauss, scale: "5mi"}.merge(boost_by_distance) | |
463 | - if !boost_by_distance[:field] || !boost_by_distance[:origin] | |
464 | - raise ArgumentError, "boost_by_distance requires :field and :origin" | |
462 | + | |
463 | + # legacy format | |
464 | + if boost_by_distance[:field] | |
465 | + boost_by_distance = {boost_by_distance[:field] => boost_by_distance.except(:field)} | |
465 | 466 | end |
466 | - function_params = boost_by_distance.select { |k, _| [:origin, :scale, :offset, :decay].include?(k) } | |
467 | - function_params[:origin] = location_value(function_params[:origin]) | |
468 | - custom_filters << { | |
469 | - boost_by_distance[:function] => { | |
470 | - boost_by_distance[:field] => function_params | |
467 | + | |
468 | + boost_by_distance.each do |field, attributes| | |
469 | + attributes = {function: :gauss, scale: "5mi"}.merge(attributes) | |
470 | + unless attributes[:origin] | |
471 | + raise ArgumentError, "boost_by_distance requires :origin" | |
472 | + end | |
473 | + function_params = attributes.select { |k, _| [:origin, :scale, :offset, :decay].include?(k) } | |
474 | + function_params[:origin] = location_value(function_params[:origin]) | |
475 | + custom_filters << { | |
476 | + attributes[:function] => { | |
477 | + field => function_params | |
478 | + } | |
471 | 479 | } |
472 | - } | |
480 | + end | |
473 | 481 | end |
474 | 482 | |
475 | 483 | def set_boost_by(multiply_filters, custom_filters) | ... | ... |
test/boost_test.rb
... | ... | @@ -138,6 +138,24 @@ class BoostTest < Minitest::Test |
138 | 138 | assert_order "san", ["San Francisco", "San Antonio", "San Marino"], boost_by_distance: {field: :location, origin: {lat: 37, lon: -122}, scale: "1000mi"} |
139 | 139 | end |
140 | 140 | |
141 | + def test_boost_by_distance_v2 | |
142 | + store [ | |
143 | + {name: "San Francisco", latitude: 37.7833, longitude: -122.4167}, | |
144 | + {name: "San Antonio", latitude: 29.4167, longitude: -98.5000}, | |
145 | + {name: "San Marino", latitude: 43.9333, longitude: 12.4667} | |
146 | + ] | |
147 | + assert_order "san", ["San Francisco", "San Antonio", "San Marino"], boost_by_distance: {location: {origin: [37, -122], scale: "1000mi"}} | |
148 | + end | |
149 | + | |
150 | + def test_boost_by_distance_v2_hash | |
151 | + store [ | |
152 | + {name: "San Francisco", latitude: 37.7833, longitude: -122.4167}, | |
153 | + {name: "San Antonio", latitude: 29.4167, longitude: -98.5000}, | |
154 | + {name: "San Marino", latitude: 43.9333, longitude: 12.4667} | |
155 | + ] | |
156 | + assert_order "san", ["San Francisco", "San Antonio", "San Marino"], boost_by_distance: {location: {origin: {lat: 37, lon: -122}, scale: "1000mi"}} | |
157 | + end | |
158 | + | |
141 | 159 | def test_boost_by_indices |
142 | 160 | store_names ["Rex"], Animal |
143 | 161 | store_names ["Rexx"], Product | ... | ... |