Commit cbb15d8ea732dba793f4347acb5647b340cd1278

Authored by Andrew Kane
1 parent 47273037

Added _and, _or, and _not to where option

  1 +## 1.4.3 [unreleased]
  2 +
  3 +- Added `_and`, `_or`, `_not` to `where` option
  4 +
1 ## 1.4.2 5 ## 1.4.2
2 6
3 - Added support for directional synonyms 7 - Added support for directional synonyms
lib/searchkick/query.rb
@@ -784,6 +784,24 @@ module Searchkick @@ -784,6 +784,24 @@ module Searchkick
784 filters << {bool: {should: or_clause.map { |or_statement| {bool: {filter: where_filters(or_statement)}} }}} 784 filters << {bool: {should: or_clause.map { |or_statement| {bool: {filter: where_filters(or_statement)}} }}}
785 end 785 end
786 end 786 end
  787 + elsif field == :_or
  788 + if below20?
  789 + filters << {or: value.map { |or_statement| {and: where_filters(or_statement)} }}
  790 + else
  791 + filters << {bool: {should: value.map { |or_statement| {bool: {filter: where_filters(or_statement)}} }}}
  792 + end
  793 + elsif field == :_not
  794 + if below20?
  795 + filters << {not: {and: where_filters(value)}}
  796 + else
  797 + filters << {bool: {must_not: where_filters(value)}}
  798 + end
  799 + elsif field == :_and
  800 + if below20?
  801 + filters << {and: value.map { |or_statement| {and: where_filters(or_statement)} }}
  802 + else
  803 + filters << {bool: {must: value.map { |or_statement| {bool: {filter: where_filters(or_statement)}} }}}
  804 + end
787 else 805 else
788 # expand ranges 806 # expand ranges
789 if value.is_a?(Range) 807 if value.is_a?(Range)
test/where_test.rb
@@ -31,6 +31,14 @@ class WhereTest &lt; Minitest::Test @@ -31,6 +31,14 @@ class WhereTest &lt; Minitest::Test
31 assert_search "product", ["Product A", "Product B", "Product C"], where: {or: [[{in_stock: true}, {store_id: 3}]]} 31 assert_search "product", ["Product A", "Product B", "Product C"], where: {or: [[{in_stock: true}, {store_id: 3}]]}
32 assert_search "product", ["Product A", "Product B", "Product C"], where: {or: [[{orders_count: [2, 4]}, {store_id: [1, 2]}]]} 32 assert_search "product", ["Product A", "Product B", "Product C"], where: {or: [[{orders_count: [2, 4]}, {store_id: [1, 2]}]]}
33 assert_search "product", ["Product A", "Product D"], where: {or: [[{orders_count: 1}, {created_at: {gte: now - 1}, backordered: true}]]} 33 assert_search "product", ["Product A", "Product D"], where: {or: [[{orders_count: 1}, {created_at: {gte: now - 1}, backordered: true}]]}
  34 + # _or
  35 + assert_search "product", ["Product A", "Product B", "Product C"], where: {_or: [{in_stock: true}, {store_id: 3}]}
  36 + assert_search "product", ["Product A", "Product B", "Product C"], where: {_or: [{orders_count: [2, 4]}, {store_id: [1, 2]}]}
  37 + assert_search "product", ["Product A", "Product D"], where: {_or: [{orders_count: 1}, {created_at: {gte: now - 1}, backordered: true}]}
  38 + # _and
  39 + assert_search "product", ["Product A"], where: {_and: [{in_stock: true}, {backordered: true}]}
  40 + # _not
  41 + assert_search "product", ["Product B", "Product C"], where: {_not: {_or: [{orders_count: 1}, {created_at: {gte: now - 1}, backordered: true}]}}
34 # all 42 # all
35 assert_search "product", ["Product A", "Product C"], where: {user_ids: {all: [1, 3]}} 43 assert_search "product", ["Product A", "Product C"], where: {user_ids: {all: [1, 3]}}
36 assert_search "product", [], where: {user_ids: {all: [1, 2, 3, 4]}} 44 assert_search "product", [], where: {user_ids: {all: [1, 2, 3, 4]}}