From cbb15d8ea732dba793f4347acb5647b340cd1278 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 21 Dec 2016 19:11:57 -0800 Subject: [PATCH] Added _and, _or, and _not to where option --- CHANGELOG.md | 4 ++++ lib/searchkick/query.rb | 18 ++++++++++++++++++ test/where_test.rb | 8 ++++++++ 3 files changed, 30 insertions(+), 0 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fe3ed7..ed972af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.4.3 [unreleased] + +- Added `_and`, `_or`, `_not` to `where` option + ## 1.4.2 - Added support for directional synonyms diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index d8dd9d0..ad02b10 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -784,6 +784,24 @@ module Searchkick filters << {bool: {should: or_clause.map { |or_statement| {bool: {filter: where_filters(or_statement)}} }}} end end + elsif field == :_or + if below20? + filters << {or: value.map { |or_statement| {and: where_filters(or_statement)} }} + else + filters << {bool: {should: value.map { |or_statement| {bool: {filter: where_filters(or_statement)}} }}} + end + elsif field == :_not + if below20? + filters << {not: {and: where_filters(value)}} + else + filters << {bool: {must_not: where_filters(value)}} + end + elsif field == :_and + if below20? + filters << {and: value.map { |or_statement| {and: where_filters(or_statement)} }} + else + filters << {bool: {must: value.map { |or_statement| {bool: {filter: where_filters(or_statement)}} }}} + end else # expand ranges if value.is_a?(Range) diff --git a/test/where_test.rb b/test/where_test.rb index 038a00f..a471f06 100644 --- a/test/where_test.rb +++ b/test/where_test.rb @@ -31,6 +31,14 @@ class WhereTest < Minitest::Test assert_search "product", ["Product A", "Product B", "Product C"], where: {or: [[{in_stock: true}, {store_id: 3}]]} assert_search "product", ["Product A", "Product B", "Product C"], where: {or: [[{orders_count: [2, 4]}, {store_id: [1, 2]}]]} assert_search "product", ["Product A", "Product D"], where: {or: [[{orders_count: 1}, {created_at: {gte: now - 1}, backordered: true}]]} + # _or + assert_search "product", ["Product A", "Product B", "Product C"], where: {_or: [{in_stock: true}, {store_id: 3}]} + assert_search "product", ["Product A", "Product B", "Product C"], where: {_or: [{orders_count: [2, 4]}, {store_id: [1, 2]}]} + assert_search "product", ["Product A", "Product D"], where: {_or: [{orders_count: 1}, {created_at: {gte: now - 1}, backordered: true}]} + # _and + assert_search "product", ["Product A"], where: {_and: [{in_stock: true}, {backordered: true}]} + # _not + assert_search "product", ["Product B", "Product C"], where: {_not: {_or: [{orders_count: 1}, {created_at: {gte: now - 1}, backordered: true}]}} # all assert_search "product", ["Product A", "Product C"], where: {user_ids: {all: [1, 3]}} assert_search "product", [], where: {user_ids: {all: [1, 2, 3, 4]}} -- libgit2 0.21.0