diff --git a/README.md b/README.md index 47d58bc..f6f8fb1 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ where: { aisle_id: {not: [25, 30]}, # not in user_ids: {all: [1, 3]}, # all elements in array category: /frozen .+/, # regexp + store_id: {_exists: true}, # exists [master] _or: [{in_stock: true}, {backordered: true}], _and: [{in_stock: true}, {backordered: true}], _not: {store_id: 1} # negate a condition diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index 44812aa..621b038 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -945,6 +945,8 @@ module Searchkick end when :in filters << term_filters(field, op_value) + when :_exists + filters << {exists: {field: field}} else range_query = case op diff --git a/test/where_test.rb b/test/where_test.rb index 15c0cbc..b40ff96 100644 --- a/test/where_test.rb +++ b/test/where_test.rb @@ -67,7 +67,11 @@ class WhereTest < Minitest::Test assert_search "product", ["Product B", "Product C"], where: {user_ids: {not: [2], in: [1, 3]}} assert_search "product", ["Product B", "Product C"], where: {user_ids: {_not: [2], in: [1, 3]}} - # not / exists + # exists + assert_search "product", ["Product A", "Product B", "Product C"], where: {user_ids: {_exists: true}} + assert_search "product", ["Product A", "Product B", "Product C", "Product D"], where: {store_id: {_exists: true}} + + # not assert_search "product", ["Product D"], where: {user_ids: nil} assert_search "product", ["Product A", "Product B", "Product C"], where: {user_ids: {not: nil}} assert_search "product", ["Product A", "Product B", "Product C"], where: {user_ids: {_not: nil}} -- libgit2 0.21.0