Skip to content
Permalink
Browse files

Fixed anchored regular expressions and added warnings for certain reg…

…ular expressions
  • Loading branch information...
ankane committed Jun 5, 2019
1 parent 4f1c988 commit 4efa577e4fa349e2b3ff68bfee42932a7fd6faf5
Showing with 36 additions and 3 deletions.
  1. +5 −0 CHANGELOG.md
  2. +14 −1 lib/searchkick/query.rb
  3. +17 −2 test/where_test.rb
@@ -1,3 +1,8 @@
## 4.0.3 [unreleased]

- Added warnings for certain regular expressions
- Fixed anchored regular expressions

## 4.0.2

- Added block form of `scroll`
@@ -985,7 +985,20 @@ def term_filters(field, value)
elsif value.nil?
{bool: {must_not: {exists: {field: field}}}}
elsif value.is_a?(Regexp)
{regexp: {field => {value: value.source, flags: "NONE"}}}
if value.casefold?
warn "[searchkick] Case-insensitive flag does not work with Elasticsearch"
end

source = value.source
unless source.start_with?("\\A") && source.end_with?("\\z")
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html
warn "[searchkick] Regular expressions are always anchored in Elasticsearch"
end

# remove \A and \z
source = source.sub(/\\A/, "").sub(/\\z\z/, "")

{regexp: {field => {value: source, flags: "NONE"}}}
else
{term: {field => value}}
end
@@ -78,7 +78,7 @@ def test_where

def test_regexp
store_names ["Product A"]
assert_search "*", ["Product A"], where: {name: /Pro.+/}
assert_search "*", ["Product A"], where: {name: /\APro.+\z/}
end

def test_alternate_regexp
@@ -88,7 +88,22 @@ def test_alternate_regexp

def test_special_regexp
store_names ["Product <A>", "Item <B>"]
assert_search "*", ["Product <A>"], where: {name: /Pro.+<.+/}
assert_search "*", ["Product <A>"], where: {name: /\APro.+<.+\z/}
end

# regular expressions are always anchored in ES
def test_regexp_not_anchored
store_names ["abcde"]
assert_search "*", [], where: {name: /abcd/}
assert_search "*", ["abcde"], where: {name: /abcde/}
assert_search "*", ["abcde"], where: {name: /\Aabcde\z/}
end

def test_regexp_case
store_names ["abcde"]
assert_search "*", [], where: {name: /\AABCDE\z/}
# flags don't work
assert_search "*", [], where: {name: /\AABCDE\z/i}
end

def test_prefix

0 comments on commit 4efa577

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