Commit 42d59120c7b6860b803b8eb92d64a450ac6f0de8

Authored by Tomer Brisker
Committed by GitHub
1 parent e97325a0

Skip optional regex operators when using `:like` syntax (#1440)

Similar to the fix in 6664a9ea3fc8bf70210937bfd619666672bde220, this
also sets the optional Lucene regex flags to "NONE" when using :like
syntax for searching. Otherwise, searches for strings including any of
the optional operators (# @ & < > ~) causes issues since they are not
escaped.
Showing 2 changed files with 8 additions and 1 deletions   Show diff stats
lib/searchkick/query.rb
... ... @@ -967,7 +967,7 @@ module Searchkick
967 967 regex.gsub!(v, "\\" + v)
968 968 end
969 969 regex = regex.gsub(/(?<!\\)%/, ".*").gsub(/(?<!\\)_/, ".").gsub("\\%", "%").gsub("\\_", "_")
970   - filters << {regexp: {field => {value: regex}}}
  970 + filters << {regexp: {field => {value: regex, flags: "NONE"}}}
971 971 when :prefix
972 972 filters << {prefix: {field => {value: op_value}}}
973 973 when :regexp # support for regexp queries without using a regexp ruby object
... ...
test/where_test.rb
... ... @@ -166,6 +166,13 @@ class WhereTest &lt; Minitest::Test
166 166 assert_search "product", ["Product ABC\""], where: {name: {like: "%ABC\""}}
167 167 end
168 168  
  169 + def test_like_optional_operators
  170 + store_names ["Product A&B", "Product B", "Product <3", "Product @Home"]
  171 + assert_search "product", ["Product A&B"], where: {name: {like: "%A&B"}}
  172 + assert_search "product", ["Product <3"], where: {name: {like: "%<%"}}
  173 + assert_search "product", ["Product @Home"], where: {name: {like: "%@Home%"}}
  174 + end
  175 +
169 176 # def test_script
170 177 # store [
171 178 # {name: "Product A", store_id: 1},
... ...