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
@@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
3 | - Added support for NoBrainer | 3 | - Added support for NoBrainer |
4 | - Added `stem_conversions: false` option | 4 | - Added `stem_conversions: false` option |
5 | - Fixed suggestions with partial match boost | 5 | - Fixed suggestions with partial match boost |
6 | +- Added support for multiple `boost_where` values on the same field | ||
6 | 7 | ||
7 | ## 0.8.5 | 8 | ## 0.8.5 |
8 | 9 |
README.md
@@ -136,6 +136,7 @@ Boost matching documents | @@ -136,6 +136,7 @@ Boost matching documents | ||
136 | ```ruby | 136 | ```ruby |
137 | boost_where: {user_id: 1} | 137 | boost_where: {user_id: 1} |
138 | boost_where: {user_id: {value: 1, factor: 100}} # default factor is 1000 | 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 | [Conversions](#keep-getting-better) are also a great way to boost. | 142 | [Conversions](#keep-getting-better) are also a great way to boost. |
lib/searchkick/query.rb
@@ -198,17 +198,18 @@ module Searchkick | @@ -198,17 +198,18 @@ module Searchkick | ||
198 | boost_where.merge!(options[:personalize]) | 198 | boost_where.merge!(options[:personalize]) |
199 | end | 199 | end |
200 | boost_where.each do |field, value| | 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 | value, factor = value[:value], value[:factor] | 207 | value, factor = value[:value], value[:factor] |
208 | + custom_filters << custom_filter(field, value, factor) | ||
203 | else | 209 | else |
204 | factor = 1000 | 210 | factor = 1000 |
211 | + custom_filters << custom_filter(field, value, factor) | ||
205 | end | 212 | end |
206 | - custom_filters << { | ||
207 | - filter: { | ||
208 | - term: {field => value} | ||
209 | - }, | ||
210 | - boost_factor: factor | ||
211 | - } | ||
212 | end | 213 | end |
213 | 214 | ||
214 | boost_by_distance = options[:boost_by_distance] | 215 | boost_by_distance = options[:boost_by_distance] |
@@ -535,5 +536,14 @@ module Searchkick | @@ -535,5 +536,14 @@ module Searchkick | ||
535 | end | 536 | end |
536 | end | 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 | end | 548 | end |
539 | end | 549 | end |
test/boost_test.rb
@@ -96,12 +96,12 @@ class TestBoost < Minitest::Test | @@ -96,12 +96,12 @@ class TestBoost < Minitest::Test | ||
96 | def test_boost_where | 96 | def test_boost_where |
97 | store [ | 97 | store [ |
98 | {name: "Tomato A"}, | 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 | assert_first "tomato", "Tomato B", boost_where: {user_ids: 2} | 102 | assert_first "tomato", "Tomato B", boost_where: {user_ids: 2} |
104 | assert_first "tomato", "Tomato B", boost_where: {user_ids: {value: 2, factor: 10}} | 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 | end | 105 | end |
106 | 106 | ||
107 | def test_boost_by_distance | 107 | def test_boost_by_distance |