Commit f13fb3290498ecdcb12e5c2b56f700a6ab3ced44
1 parent
4efa577e
Exists in
master
and in
17 other branches
Prep code to support unanchored regular expressions
Showing
2 changed files
with
24 additions
and
3 deletions
Show diff stats
lib/searchkick/query.rb
... | ... | @@ -995,8 +995,20 @@ module Searchkick |
995 | 995 | warn "[searchkick] Regular expressions are always anchored in Elasticsearch" |
996 | 996 | end |
997 | 997 | |
998 | - # remove \A and \z | |
999 | - source = source.sub(/\\A/, "").sub(/\\z\z/, "") | |
998 | + # TODO handle other anchor characters, like ^, $, \Z | |
999 | + if source.start_with?("\\A") | |
1000 | + source = source[2..-1] | |
1001 | + else | |
1002 | + # TODO uncomment in future release | |
1003 | + # source = ".*#{source}" | |
1004 | + end | |
1005 | + | |
1006 | + if source.end_with?("\\z") | |
1007 | + source = source[0..-3] | |
1008 | + else | |
1009 | + # TODO uncomment in future release | |
1010 | + # source = "#{source}.*" | |
1011 | + end | |
1000 | 1012 | |
1001 | 1013 | {regexp: {field => {value: source, flags: "NONE"}}} |
1002 | 1014 | else | ... | ... |
test/where_test.rb
... | ... | @@ -91,12 +91,21 @@ class WhereTest < Minitest::Test |
91 | 91 | assert_search "*", ["Product <A>"], where: {name: /\APro.+<.+\z/} |
92 | 92 | end |
93 | 93 | |
94 | - # regular expressions are always anchored in ES | |
95 | 94 | def test_regexp_not_anchored |
96 | 95 | store_names ["abcde"] |
96 | + # regular expressions are always anchored right now | |
97 | + # TODO change in future release | |
97 | 98 | assert_search "*", [], where: {name: /abcd/} |
99 | + assert_search "*", [], where: {name: /bcde/} | |
98 | 100 | assert_search "*", ["abcde"], where: {name: /abcde/} |
101 | + assert_search "*", ["abcde"], where: {name: /.*bcd.*/} | |
102 | + end | |
103 | + | |
104 | + def test_regexp_anchored | |
105 | + store_names ["abcde"] | |
99 | 106 | assert_search "*", ["abcde"], where: {name: /\Aabcde\z/} |
107 | + assert_search "*", [], where: {name: /\Abcd/} | |
108 | + assert_search "*", [], where: {name: /bcd\z/} | |
100 | 109 | end |
101 | 110 | |
102 | 111 | def test_regexp_case | ... | ... |