Commit 7b36894483721a5d02d0c561a86d587bbf8f2b67
1 parent
7ca2b43d
Exists in
master
and in
15 other branches
Fixed like queries with " character - #1365
Showing
3 changed files
with
19 additions
and
1 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/query.rb
@@ -953,7 +953,14 @@ module Searchkick | @@ -953,7 +953,14 @@ module Searchkick | ||
953 | # % matches zero or more characters | 953 | # % matches zero or more characters |
954 | # _ matches one character | 954 | # _ matches one character |
955 | # \ is escape character | 955 | # \ is escape character |
956 | - regex = Regexp.escape(op_value).gsub(/(?<!\\)%/, ".*").gsub(/(?<!\\)_/, ".").gsub("\\%", "%").gsub("\\_", "_") | 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("\\_", "_") | ||
957 | filters << {regexp: {field => {value: regex}}} | 964 | filters << {regexp: {field => {value: regex}}} |
958 | when :prefix | 965 | when :prefix |
959 | filters << {prefix: {field => op_value}} | 966 | filters << {prefix: {field => op_value}} |
test/where_test.rb
@@ -161,6 +161,13 @@ class WhereTest < Minitest::Test | @@ -161,6 +161,13 @@ class WhereTest < Minitest::Test | ||
161 | assert_search "product", ["Product 100%"], where: {name: {like: "% 100\\%"}} | 161 | assert_search "product", ["Product 100%"], where: {name: {like: "% 100\\%"}} |
162 | end | 162 | end |
163 | 163 | ||
164 | + def test_like_special_characters | ||
165 | + store_names ["Product ABC\"", "Product B"] | ||
166 | + like = "%ABC\"" | ||
167 | + assert_equal 1, Product.where("name LIKE ?", like).count | ||
168 | + assert_search "product", ["Product ABC\""], where: {name: {like: like}} | ||
169 | + end | ||
170 | + | ||
164 | # def test_script | 171 | # def test_script |
165 | # store [ | 172 | # store [ |
166 | # {name: "Product A", store_id: 1}, | 173 | # {name: "Product A", store_id: 1}, |