Skip to content
Permalink
Browse files

Prep code to support unanchored regular expressions

  • Loading branch information...
ankane committed Jun 5, 2019
1 parent 4efa577 commit f13fb3290498ecdcb12e5c2b56f700a6ab3ced44
Showing with 24 additions and 3 deletions.
  1. +14 −2 lib/searchkick/query.rb
  2. +10 −1 test/where_test.rb
@@ -995,8 +995,20 @@ def term_filters(field, value)
warn "[searchkick] Regular expressions are always anchored in Elasticsearch"
end

# remove \A and \z
source = source.sub(/\\A/, "").sub(/\\z\z/, "")
# TODO handle other anchor characters, like ^, $, \Z
if source.start_with?("\\A")
source = source[2..-1]
else
# TODO uncomment in future release
# source = ".*#{source}"
end

if source.end_with?("\\z")
source = source[0..-3]
else
# TODO uncomment in future release
# source = "#{source}.*"
end

{regexp: {field => {value: source, flags: "NONE"}}}
else
@@ -91,12 +91,21 @@ def test_special_regexp
assert_search "*", ["Product <A>"], where: {name: /\APro.+<.+\z/}
end

# regular expressions are always anchored in ES
def test_regexp_not_anchored
store_names ["abcde"]
# regular expressions are always anchored right now
# TODO change in future release
assert_search "*", [], where: {name: /abcd/}
assert_search "*", [], where: {name: /bcde/}
assert_search "*", ["abcde"], where: {name: /abcde/}
assert_search "*", ["abcde"], where: {name: /.*bcd.*/}
end

def test_regexp_anchored
store_names ["abcde"]
assert_search "*", ["abcde"], where: {name: /\Aabcde\z/}
assert_search "*", [], where: {name: /\Abcd/}
assert_search "*", [], where: {name: /bcd\z/}
end

def test_regexp_case

0 comments on commit f13fb32

Please sign in to comment.
You can’t perform that action at this time.