Commit 51a2e3605750b9c72430396f636485c757f6e188
Exists in
master
and in
21 other branches
Merge branch 'tommi-lew-master'
Showing
4 changed files
with
22 additions
and
10 deletions
Show diff stats
CHANGELOG.md
README.md
... | ... | @@ -136,6 +136,7 @@ Boost matching documents |
136 | 136 | ```ruby |
137 | 137 | boost_where: {user_id: 1} |
138 | 138 | boost_where: {user_id: {value: 1, factor: 100}} # default factor is 1000 |
139 | +boost_where: {user_id: [{value: 1, factor: 100}, {value: 2, factor: 200}]} | |
139 | 140 | ``` |
140 | 141 | |
141 | 142 | [Conversions](#keep-getting-better) are also a great way to boost. | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -198,17 +198,18 @@ module Searchkick |
198 | 198 | boost_where.merge!(options[:personalize]) |
199 | 199 | end |
200 | 200 | boost_where.each do |field, value| |
201 | - if value.is_a?(Hash) | |
201 | + if value.is_a?(Array) | |
202 | + value.each do |value_factor| | |
203 | + value, factor = value_factor[:value], value_factor[:factor] | |
204 | + custom_filters << custom_filter(field, value, factor) | |
205 | + end | |
206 | + elsif value.is_a?(Hash) | |
202 | 207 | value, factor = value[:value], value[:factor] |
208 | + custom_filters << custom_filter(field, value, factor) | |
203 | 209 | else |
204 | 210 | factor = 1000 |
211 | + custom_filters << custom_filter(field, value, factor) | |
205 | 212 | end |
206 | - custom_filters << { | |
207 | - filter: { | |
208 | - term: {field => value} | |
209 | - }, | |
210 | - boost_factor: factor | |
211 | - } | |
212 | 213 | end |
213 | 214 | |
214 | 215 | boost_by_distance = options[:boost_by_distance] |
... | ... | @@ -535,5 +536,14 @@ module Searchkick |
535 | 536 | end |
536 | 537 | end |
537 | 538 | |
539 | + def custom_filter(field, value, factor) | |
540 | + { | |
541 | + filter: { | |
542 | + term: {field => value} | |
543 | + }, | |
544 | + boost_factor: factor | |
545 | + } | |
546 | + end | |
547 | + | |
538 | 548 | end |
539 | 549 | end | ... | ... |
test/boost_test.rb
... | ... | @@ -96,12 +96,12 @@ class TestBoost < Minitest::Test |
96 | 96 | def test_boost_where |
97 | 97 | store [ |
98 | 98 | {name: "Tomato A"}, |
99 | - {name: "Tomato B", user_ids: [1, 2, 3]}, | |
100 | - {name: "Tomato C"}, | |
101 | - {name: "Tomato D"} | |
99 | + {name: "Tomato B", user_ids: [1, 2]}, | |
100 | + {name: "Tomato C", user_ids: [3]} | |
102 | 101 | ] |
103 | 102 | assert_first "tomato", "Tomato B", boost_where: {user_ids: 2} |
104 | 103 | assert_first "tomato", "Tomato B", boost_where: {user_ids: {value: 2, factor: 10}} |
104 | + assert_order "tomato", ["Tomato C", "Tomato B", "Tomato A"], boost_where: {user_ids: [{value: 1, factor: 10}, {value: 3, factor: 20}]} | |
105 | 105 | end |
106 | 106 | |
107 | 107 | def test_boost_by_distance | ... | ... |