Commit 0337e063246def3a47733a0a7afb3a02b30852e2
1 parent
cd7d3220
Exists in
master
and in
21 other branches
add boost_mode
Showing
2 changed files
with
30 additions
and
30 deletions
Show diff stats
lib/searchkick/query.rb
@@ -167,14 +167,17 @@ module Searchkick | @@ -167,14 +167,17 @@ module Searchkick | ||
167 | custom_filters = [] | 167 | custom_filters = [] |
168 | 168 | ||
169 | boost_by = options[:boost_by] || {} | 169 | boost_by = options[:boost_by] || {} |
170 | + | ||
170 | if boost_by.is_a?(Array) | 171 | if boost_by.is_a?(Array) |
171 | - boost_by = Hash[boost_by.map { |f| [f, {factor: 1}] }] | 172 | + boost_by_sum = Hash[boost_by.map { |f| [f, {factor: 1}] }] |
173 | + elsif boost_by.is_a?(Hash) | ||
174 | + boost_by_multiply, boost_by_sum = boost_by.partition { |k,v| v[:boost_mode] == "multiply" }.map{|i| Hash[i] } | ||
172 | end | 175 | end |
173 | if options[:boost] | 176 | if options[:boost] |
174 | - boost_by[options[:boost]] = {factor: 1} | 177 | + boost_by_sum[options[:boost]] = {factor: 1} |
175 | end | 178 | end |
176 | 179 | ||
177 | - boost_by.each do |field, value| | 180 | + boost_by_sum.each do |field, value| |
178 | script_score = | 181 | script_score = |
179 | if below12 | 182 | if below12 |
180 | {script_score: {script: "#{value[:factor].to_f} * log(doc['#{field}'].value + 2.718281828)"}} | 183 | {script_score: {script: "#{value[:factor].to_f} * log(doc['#{field}'].value + 2.718281828)"}} |
@@ -191,6 +194,28 @@ module Searchkick | @@ -191,6 +194,28 @@ module Searchkick | ||
191 | }.merge(script_score) | 194 | }.merge(script_score) |
192 | end | 195 | end |
193 | 196 | ||
197 | + if boost_by_multiply | ||
198 | + multiply_filters = [] | ||
199 | + | ||
200 | + boost_by_multiply.each do |field, value| | ||
201 | + script_score = | ||
202 | + if below12 | ||
203 | + {script_score: {script: "#{value[:factor].to_f} * doc['#{field}'].value"}} | ||
204 | + else | ||
205 | + value[:factor] ||= 1 | ||
206 | + {field_value_factor: {field: field, factor: value[:factor].to_f}} | ||
207 | + end | ||
208 | + | ||
209 | + multiply_filters << { | ||
210 | + filter: { | ||
211 | + exists: { | ||
212 | + field: field | ||
213 | + } | ||
214 | + } | ||
215 | + }.merge(script_score) | ||
216 | + end | ||
217 | + end | ||
218 | + | ||
194 | boost_where = options[:boost_where] || {} | 219 | boost_where = options[:boost_where] || {} |
195 | if options[:user_id] && personalize_field | 220 | if options[:user_id] && personalize_field |
196 | boost_where[personalize_field] = options[:user_id] | 221 | boost_where[personalize_field] = options[:user_id] |
@@ -238,31 +263,7 @@ module Searchkick | @@ -238,31 +263,7 @@ module Searchkick | ||
238 | } | 263 | } |
239 | end | 264 | end |
240 | 265 | ||
241 | - multiply_filters = [] | ||
242 | - | ||
243 | - multiply_by = options[:multiply_by] || {} | ||
244 | - if multiply_by.is_a?(Array) | ||
245 | - multiply_by = Hash[multiply_by.map { |f| [f, {factor: 1}] }] | ||
246 | - end | ||
247 | - | ||
248 | - multiply_by.each do |field, value| | ||
249 | - script_score = | ||
250 | - if below12 | ||
251 | - {script_score: {script: "#{value[:factor].to_f} * doc['#{field}'].value"}} | ||
252 | - else | ||
253 | - {field_value_factor: {field: field, factor: value[:factor].to_f}} | ||
254 | - end | ||
255 | - | ||
256 | - multiply_filters << { | ||
257 | - filter: { | ||
258 | - exists: { | ||
259 | - field: field | ||
260 | - } | ||
261 | - } | ||
262 | - }.merge(script_score) | ||
263 | - end | ||
264 | - | ||
265 | - if multiply_filters.any? | 266 | + if multiply_filters && multiply_filters.any? |
266 | payload = { | 267 | payload = { |
267 | function_score: { | 268 | function_score: { |
268 | functions: multiply_filters, | 269 | functions: multiply_filters, |
test/boost_test.rb
@@ -108,8 +108,7 @@ class TestBoost < Minitest::Test | @@ -108,8 +108,7 @@ class TestBoost < Minitest::Test | ||
108 | {name: "Tomato C", found_rate: 0.5} | 108 | {name: "Tomato C", found_rate: 0.5} |
109 | ] | 109 | ] |
110 | 110 | ||
111 | - assert_order "tomato", ["Tomato B", "Tomato A", "Tomato C"], multiply_by: [:found_rate] | ||
112 | - assert_order "tomato", ["Tomato B", "Tomato A", "Tomato C"], multiply_by: {found_rate: {factor: 1}} | 111 | + assert_order "tomato", ["Tomato B", "Tomato A", "Tomato C"], boost_by: {found_rate: {factor: 1, boost_mode: "multiply"}} |
113 | end | 112 | end |
114 | 113 | ||
115 | def test_boost_where | 114 | def test_boost_where |