Commit 52ddd7bb9dc5b22d2249a2616e894d2492a812ac

Authored by Andrew Kane
1 parent 9f2a51a4
Exists in wildcard

Use wildcard for like queries

Showing 2 changed files with 5 additions and 9 deletions   Show diff stats
lib/searchkick/query.rb
... ... @@ -953,15 +953,9 @@ module Searchkick
953 953 # % matches zero or more characters
954 954 # _ matches one character
955 955 # \ is escape character
956   - # escape Lucene reserved characters
957   - # https://www.elastic.co/guide/en/elasticsearch/reference/current/regexp-syntax.html#regexp-optional-operators
958   - reserved = %w(. ? + * | { } [ ] ( ) " \\)
959   - regex = op_value.dup
960   - reserved.each do |v|
961   - regex.gsub!(v, "\\" + v)
962   - end
963   - regex = regex.gsub(/(?<!\\)%/, ".*").gsub(/(?<!\\)_/, ".").gsub("\\%", "%").gsub("\\_", "_")
964   - filters << {regexp: {field => {value: regex}}}
  956 + wildcard = op_value.gsub("*", "\\*").gsub("?", "\\?")
  957 + wildcard = wildcard.gsub(/(?<!\\)%/, "*").gsub(/(?<!\\)_/, "?").gsub("\\%", "%").gsub("\\_", "_")
  958 + filters << {wildcard: {field => {value: wildcard}}}
965 959 when :prefix
966 960 filters << {prefix: {field => op_value}}
967 961 when :regexp # support for regexp queries without using a regexp ruby object
... ...
test/where_test.rb
... ... @@ -164,6 +164,8 @@ class WhereTest &lt; Minitest::Test
164 164 def test_like_special_characters
165 165 store_names ["Product ABC\"", "Product B"]
166 166 assert_search "product", ["Product ABC\""], where: {name: {like: "%ABC\""}}
  167 + assert_search "product", [], where: {name: {like: "*"}}
  168 + assert_search "product", ["Product ABC\"", "Product B"], where: {name: {like: "%"}}
167 169 end
168 170  
169 171 # def test_script
... ...