Commit c1b57c728d0d04b3a45b9ff6bd5a7f5d29a221ea

Authored by Andrew Kane
1 parent 87858fcd

Refactored boost_mode

Showing 2 changed files with 43 additions and 49 deletions   Show diff stats
lib/searchkick/query.rb
... ... @@ -15,9 +15,6 @@ module Searchkick
15 15 @term = term
16 16 @options = options
17 17  
18   - below12 = Gem::Version.new(Searchkick.server_version) < Gem::Version.new("1.2.0")
19   - below14 = Gem::Version.new(Searchkick.server_version) < Gem::Version.new("1.4.0")
20   -
21 18 boost_fields = {}
22 19 fields =
23 20 if options[:fields]
... ... @@ -134,7 +131,7 @@ module Searchkick
134 131 if conversions_field && options[:conversions] != false
135 132 # wrap payload in a bool query
136 133 script_score =
137   - if below12
  134 + if below12?
138 135 {script_score: {script: "doc['count'].value"}}
139 136 else
140 137 {field_value_factor: {field: "count"}}
... ... @@ -165,56 +162,21 @@ module Searchkick
165 162 end
166 163  
167 164 custom_filters = []
  165 + multiply_filters = []
168 166  
169 167 boost_by = options[:boost_by] || {}
170 168  
171 169 if boost_by.is_a?(Array)
172   - boost_by_sum = Hash[boost_by.map { |f| [f, {factor: 1}] }]
  170 + boost_by = Hash[boost_by.map { |f| [f, {factor: 1}] }]
173 171 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 + multiply_by, boost_by = boost_by.partition { |k,v| v[:boost_mode] == "multiply" }.map{ |i| Hash[i] }
175 173 end
176 174 if options[:boost]
177   - boost_by_sum[options[:boost]] = {factor: 1}
178   - end
179   -
180   - boost_by_sum.each do |field, value|
181   - script_score =
182   - if below12
183   - {script_score: {script: "#{value[:factor].to_f} * log(doc['#{field}'].value + 2.718281828)"}}
184   - else
185   - {field_value_factor: {field: field, factor: value[:factor].to_f, modifier: "ln2p"}}
186   - end
187   -
188   - custom_filters << {
189   - filter: {
190   - exists: {
191   - field: field
192   - }
193   - }
194   - }.merge(script_score)
  175 + boost_by[options[:boost]] = {factor: 1}
195 176 end
196 177  
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
  178 + custom_filters.concat boost_filters(boost_by, log: true)
  179 + multiply_filters.concat boost_filters(multiply_by || {})
218 180  
219 181 boost_where = options[:boost_where] || {}
220 182 if options[:user_id] && personalize_field
... ... @@ -263,7 +225,7 @@ module Searchkick
263 225 }
264 226 end
265 227  
266   - if multiply_filters && multiply_filters.any?
  228 + if multiply_filters.any?
267 229 payload = {
268 230 function_score: {
269 231 functions: multiply_filters,
... ... @@ -330,7 +292,7 @@ module Searchkick
330 292 payload[:facets][field] = {
331 293 terms_stats: {
332 294 key_field: field,
333   - value_script: below14 ? "doc.score" : "_score",
  295 + value_script: below14? ? "doc.score" : "_score",
334 296 size: size
335 297 }
336 298 }
... ... @@ -603,5 +565,37 @@ module Searchkick
603 565 }
604 566 end
605 567  
  568 + def boost_filters(boost_by, options = {})
  569 + boost_by.map do |field, value|
  570 + log = value.key?(:log) ? value[:log] : options[:log]
  571 + script_score =
  572 + if below12?
  573 + script = log ? "log(doc['#{field}'].value + 2.718281828)" : "doc['#{field}'].value"
  574 + {script_score: {script: "#{value[:factor].to_f} * #{script}"}}
  575 + else
  576 + {field_value_factor: {field: field, factor: value[:factor].to_f, modifier: log ? "ln2p" : nil}}
  577 + end
  578 +
  579 + {
  580 + filter: {
  581 + exists: {
  582 + field: field
  583 + }
  584 + }
  585 + }.merge(script_score)
  586 + end
  587 + end
  588 +
  589 + def below12?
  590 + below_version?("1.2.0")
  591 + end
  592 +
  593 + def below14?
  594 + below_version?("1.4.0")
  595 + end
  596 +
  597 + def below_version?(version)
  598 + Gem::Version.new(Searchkick.server_version) < Gem::Version.new(version)
  599 + end
606 600 end
607 601 end
... ...
test/boost_test.rb
... ... @@ -101,14 +101,14 @@ class TestBoost &lt; Minitest::Test
101 101 assert_order "tomato", ["Tomato C", "Tomato B", "Tomato A"], boost_by: {orders_count: {factor: 10}}
102 102 end
103 103  
104   - def test_multiply_by
  104 + def test_boost_by_boost_mode_multiply
105 105 store [
106 106 {name: "Tomato A", found_rate: 0.9},
107 107 {name: "Tomato B"},
108 108 {name: "Tomato C", found_rate: 0.5}
109 109 ]
110 110  
111   - assert_order "tomato", ["Tomato B", "Tomato A", "Tomato C"], boost_by: {found_rate: {factor: 1, boost_mode: "multiply"}}
  111 + assert_order "tomato", ["Tomato B", "Tomato A", "Tomato C"], boost_by: {found_rate: {boost_mode: "multiply"}}
112 112 end
113 113  
114 114 def test_boost_where
... ...