Commit 4ca16ca42ca665f67df9fa93d81ebc7397d0f7d8
Exists in
master
and in
19 other branches
Merge branch 'add-missing-to-boost-by' of https://github.com/jordanderson/search…
…kick into jordanderson-add-missing-to-boost-by
Showing
2 changed files
with
28 additions
and
0 deletions
Show diff stats
lib/searchkick/query.rb
@@ -883,6 +883,14 @@ module Searchkick | @@ -883,6 +883,14 @@ module Searchkick | ||
883 | } | 883 | } |
884 | } | 884 | } |
885 | 885 | ||
886 | + if value[:missing].present? | ||
887 | + if below50? | ||
888 | + raise ArgumentError, "Option 'missing' for boost_by supported in Elasticsearch 5 or greater" | ||
889 | + else | ||
890 | + script_score[:field_value_factor].merge!({missing: value[:missing].to_f}) | ||
891 | + end | ||
892 | + end | ||
893 | + | ||
886 | { | 894 | { |
887 | filter: { | 895 | filter: { |
888 | exists: { | 896 | exists: { |
test/boost_test.rb
@@ -116,6 +116,26 @@ class BoostTest < Minitest::Test | @@ -116,6 +116,26 @@ class BoostTest < Minitest::Test | ||
116 | assert_order "tomato", ["Tomato C", "Tomato B", "Tomato A"], boost_by: {orders_count: {factor: 10}} | 116 | assert_order "tomato", ["Tomato C", "Tomato B", "Tomato A"], boost_by: {orders_count: {factor: 10}} |
117 | end | 117 | end |
118 | 118 | ||
119 | + def test_boost_by_missing_field | ||
120 | + store [ | ||
121 | + {name: "Tomato A"}, | ||
122 | + {name: "Tomato B", orders_count: 10}, | ||
123 | + {name: "Tomato C", orders_count: 100} | ||
124 | + ] | ||
125 | + | ||
126 | + assert_raises(Searchkick::InvalidQueryError) do | ||
127 | + assert_order "tomato", ["Tomato C", "Tomato B", "Tomato A"], boost_by: {orders_count: {factor: 5}, orders_value: {factor: 5}} | ||
128 | + end | ||
129 | + | ||
130 | + if elasticsearch_below50? | ||
131 | + assert_raises(ArgumentError) do | ||
132 | + assert_order "tomato", ["Tomato C", "Tomato B", "Tomato A"], boost_by: {orders_count: {factor: 5}, orders_value: {factor: 5, missing: 1}} | ||
133 | + end | ||
134 | + else | ||
135 | + assert_order "tomato", ["Tomato C", "Tomato B", "Tomato A"], boost_by: {orders_count: {factor: 5}, orders_value: {factor: 5, missing: 1}} | ||
136 | + end | ||
137 | + end | ||
138 | + | ||
119 | def test_boost_by_boost_mode_multiply | 139 | def test_boost_by_boost_mode_multiply |
120 | store [ | 140 | store [ |
121 | {name: "Tomato A", found_rate: 0.9}, | 141 | {name: "Tomato A", found_rate: 0.9}, |