Commit 418f40b84ab30b546df127cdb7f470b1035ce82a

Authored by Nick Plante
1 parent 94378bbe

Allow nested any (or) filter execution

Showing 2 changed files with 8 additions and 4 deletions   Show diff stats
lib/searchkick/search.rb
... ... @@ -248,6 +248,8 @@ module Searchkick
248 248 filters << {not: term_filters.call(field, op_value)}
249 249 when :all
250 250 filters << {terms: {field => op_value, execution: "and"}}
  251 + when :any
  252 + filters << {terms: {field => op_value, execution: "or"}}
251 253 else
252 254 range_query =
253 255 case op
... ...
test/sql_test.rb
... ... @@ -40,7 +40,7 @@ class TestSql &lt; Minitest::Unit::TestCase
40 40 store [
41 41 {name: "Product A", store_id: 1, in_stock: true, backordered: true, created_at: now, orders_count: 4, user_ids: [1, 2, 3]},
42 42 {name: "Product B", store_id: 2, in_stock: true, backordered: false, created_at: now - 1, orders_count: 3, user_ids: [1]},
43   - {name: "Product C", store_id: 3, in_stock: false, backordered: true, created_at: now - 2, orders_count: 2},
  43 + {name: "Product C", store_id: 3, in_stock: false, backordered: true, created_at: now - 2, orders_count: 2, user_ids: [1, 3]},
44 44 {name: "Product D", store_id: 4, in_stock: false, backordered: false, created_at: now - 3, orders_count: 1},
45 45 ]
46 46 assert_search "product", ["Product A", "Product B"], where: {in_stock: true}
... ... @@ -65,11 +65,13 @@ class TestSql &lt; Minitest::Unit::TestCase
65 65 assert_search "product", ["Product A", "Product B", "Product C"], where: {or: [[{orders_count: [2, 4]}, {store_id: [1, 2]}]]}
66 66 assert_search "product", ["Product A", "Product D"], where: {or: [[{orders_count: 1}, {created_at: {gte: now - 1}, backordered: true}]]}
67 67 # all
68   - assert_search "product", ["Product A"], where: {user_ids: {all: [1, 3]}}
  68 + assert_search "product", ["Product A", "Product C"], where: {user_ids: {all: [1, 3]}}
69 69 assert_search "product", [], where: {user_ids: {all: [1, 2, 3, 4]}}
  70 + # any / nested terms
  71 + assert_search "product", ["Product B", "Product C"], where: {user_ids: {not: [2], any: [1,3]}}
70 72 # not / exists
71   - assert_search "product", ["Product C", "Product D"], where: {user_ids: nil}
72   - assert_search "product", ["Product A", "Product B"], where: {user_ids: {not: nil}}
  73 + assert_search "product", ["Product D"], where: {user_ids: nil}
  74 + assert_search "product", ["Product A", "Product B", "Product C"], where: {user_ids: {not: nil}}
73 75 assert_search "product", ["Product A", "Product C", "Product D"], where: {user_ids: [3, nil]}
74 76 assert_search "product", ["Product B"], where: {user_ids: {not: [3, nil]}}
75 77 end
... ...