Commit 0a9bfdd2433c9aba64ac4f9d600882ba5e99f708
1 parent
798397df
Exists in
master
and in
17 other branches
Added like query
Showing
3 changed files
with
14 additions
and
0 deletions
Show diff stats
README.md
... | ... | @@ -105,6 +105,7 @@ where: { |
105 | 105 | store_id: {not: 2}, # not |
106 | 106 | aisle_id: {not: [25, 30]}, # not in |
107 | 107 | user_ids: {all: [1, 3]}, # all elements in array |
108 | + category: {like: "%frozen%"}, # like [master] | |
108 | 109 | category: /frozen .+/, # regexp |
109 | 110 | store_id: {exists: true}, # exists [master] |
110 | 111 | category: {prefix: "Frozen"}, # prefix | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -935,6 +935,9 @@ module Searchkick |
935 | 935 | } |
936 | 936 | } |
937 | 937 | } |
938 | + when :like | |
939 | + regex = Regexp.escape(op_value).gsub("%", ".*").gsub("_", ".") | |
940 | + filters << {regexp: {field => {value: regex}}} | |
938 | 941 | when :prefix |
939 | 942 | filters << {prefix: {field => op_value}} |
940 | 943 | when :regexp # support for regexp queries without using a regexp ruby object | ... | ... |
test/where_test.rb
... | ... | @@ -128,6 +128,16 @@ class WhereTest < Minitest::Test |
128 | 128 | assert_search "product", ["Product A"], where: {user_ids: {exists: true}} |
129 | 129 | end |
130 | 130 | |
131 | + def test_like | |
132 | + store_names ["Product ABC", "Product DEF"] | |
133 | + assert_search "product", ["Product ABC"], where: {name: {like: "%ABC%"}} | |
134 | + assert_search "product", ["Product ABC"], where: {name: {like: "%ABC"}} | |
135 | + assert_search "product", [], where: {name: {like: "ABC"}} | |
136 | + assert_search "product", [], where: {name: {like: "ABC%"}} | |
137 | + assert_search "product", [], where: {name: {like: "ABC%"}} | |
138 | + assert_search "product", ["Product ABC"], where: {name: {like: "Product_ABC"}} | |
139 | + end | |
140 | + | |
131 | 141 | # def test_script |
132 | 142 | # store [ |
133 | 143 | # {name: "Product A", store_id: 1}, | ... | ... |