Commit fe5931dd3f8994a6bf62c4f0e67f186e4ff93e60

Authored by Andrew Kane
1 parent 14fee2c9

Fixed empty array in where clause - closes #113

Showing 2 changed files with 15 additions and 1 deletions   Show diff stats
lib/searchkick/search.rb
... ... @@ -185,7 +185,11 @@ module Searchkick
185 185 term_filters =
186 186 proc do |field, value|
187 187 if value.is_a?(Array) # in query
188   - {or: value.map{|v| term_filters.call(field, v) }}
  188 + if value.any?
  189 + {or: value.map{|v| term_filters.call(field, v) }}
  190 + else
  191 + {terms: {field => value}} # match nothing
  192 + end
189 193 elsif value.nil?
190 194 {missing: {"field" => field, existence: true, null_value: true}}
191 195 else
... ...
test/sql_test.rb
... ... @@ -95,6 +95,16 @@ class TestSql < Minitest::Unit::TestCase
95 95 assert_search "product", ["Product A"], where: {id: product.id.to_s}
96 96 end
97 97  
  98 + def test_where_empty
  99 + store_names ["Product A"]
  100 + assert_search "product", ["Product A"], where: {}
  101 + end
  102 +
  103 + def test_where_empty_array
  104 + store_names ["Product A"]
  105 + assert_search "product", [], where: {store_id: []}
  106 + end
  107 +
98 108 def test_near
99 109 store [
100 110 {name: "San Francisco", latitude: 37.7833, longitude: -122.4167},
... ...