Commit 83afd813575cd29207cedf85de00ab0c79b45958
1 parent
bbd7077a
Exists in
master
and in
2 other branches
Anchor regular expressions by default [skip ci]
Showing
3 changed files
with
11 additions
and
28 deletions
Show diff stats
CHANGELOG.md
1 | ## 5.0.0 (unreleased) | 1 | ## 5.0.0 (unreleased) |
2 | 2 | ||
3 | +- Anchor regular expressions by default | ||
3 | - Raise error when `search` called on relations | 4 | - Raise error when `search` called on relations |
4 | - Raise `ArgumentError` (instead of warning) for invalid regular expression modifiers | 5 | - Raise `ArgumentError` (instead of warning) for invalid regular expression modifiers |
5 | - Dropped support for Ruby < 2.6 and Active Record < 5.2 | 6 | - Dropped support for Ruby < 2.6 and Active Record < 5.2 |
lib/searchkick/query.rb
@@ -1036,24 +1036,18 @@ module Searchkick | @@ -1036,24 +1036,18 @@ module Searchkick | ||
1036 | {bool: {must_not: {exists: {field: field}}}} | 1036 | {bool: {must_not: {exists: {field: field}}}} |
1037 | elsif value.is_a?(Regexp) | 1037 | elsif value.is_a?(Regexp) |
1038 | source = value.source | 1038 | source = value.source |
1039 | - unless source.start_with?("\\A") && source.end_with?("\\z") | ||
1040 | - # https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html | ||
1041 | - Searchkick.warn("Regular expressions are always anchored in Elasticsearch") | ||
1042 | - end | ||
1043 | 1039 | ||
1044 | # TODO handle other anchor characters, like ^, $, \Z | 1040 | # TODO handle other anchor characters, like ^, $, \Z |
1045 | if source.start_with?("\\A") | 1041 | if source.start_with?("\\A") |
1046 | source = source[2..-1] | 1042 | source = source[2..-1] |
1047 | else | 1043 | else |
1048 | - # TODO uncomment in Searchkick 5 | ||
1049 | - # source = ".*#{source}" | 1044 | + source = ".*#{source}" |
1050 | end | 1045 | end |
1051 | 1046 | ||
1052 | if source.end_with?("\\z") | 1047 | if source.end_with?("\\z") |
1053 | source = source[0..-3] | 1048 | source = source[0..-3] |
1054 | else | 1049 | else |
1055 | - # TODO uncomment in Searchkick 5 | ||
1056 | - # source = "#{source}.*" | 1050 | + source = "#{source}.*" |
1057 | end | 1051 | end |
1058 | 1052 | ||
1059 | if below710? | 1053 | if below710? |
test/where_test.rb
@@ -113,31 +113,19 @@ class WhereTest < Minitest::Test | @@ -113,31 +113,19 @@ class WhereTest < Minitest::Test | ||
113 | 113 | ||
114 | def test_regexp_not_anchored | 114 | def test_regexp_not_anchored |
115 | store_names ["abcde"] | 115 | store_names ["abcde"] |
116 | - # regular expressions are always anchored right now | ||
117 | - # TODO change in future release | ||
118 | - assert_warns "Regular expressions are always anchored in Elasticsearch" do | ||
119 | - assert_search "*", [], where: {name: /abcd/} | ||
120 | - end | ||
121 | - assert_warns "Regular expressions are always anchored in Elasticsearch" do | ||
122 | - assert_search "*", [], where: {name: /bcde/} | ||
123 | - end | ||
124 | - assert_warns "Regular expressions are always anchored in Elasticsearch" do | ||
125 | - assert_search "*", ["abcde"], where: {name: /abcde/} | ||
126 | - end | ||
127 | - assert_warns "Regular expressions are always anchored in Elasticsearch" do | ||
128 | - assert_search "*", ["abcde"], where: {name: /.*bcd.*/} | ||
129 | - end | 116 | + assert_search "*", ["abcde"], where: {name: /abcd/} |
117 | + assert_search "*", ["abcde"], where: {name: /bcde/} | ||
118 | + assert_search "*", ["abcde"], where: {name: /abcde/} | ||
119 | + assert_search "*", ["abcde"], where: {name: /.*bcd.*/} | ||
130 | end | 120 | end |
131 | 121 | ||
132 | def test_regexp_anchored | 122 | def test_regexp_anchored |
133 | store_names ["abcde"] | 123 | store_names ["abcde"] |
134 | assert_search "*", ["abcde"], where: {name: /\Aabcde\z/} | 124 | assert_search "*", ["abcde"], where: {name: /\Aabcde\z/} |
135 | - assert_warns "Regular expressions are always anchored in Elasticsearch" do | ||
136 | - assert_search "*", [], where: {name: /\Abcd/} | ||
137 | - end | ||
138 | - assert_warns "Regular expressions are always anchored in Elasticsearch" do | ||
139 | - assert_search "*", [], where: {name: /bcd\z/} | ||
140 | - end | 125 | + assert_search "*", ["abcde"], where: {name: /\Aabc/} |
126 | + assert_search "*", ["abcde"], where: {name: /cde\z/} | ||
127 | + assert_search "*", [], where: {name: /\Abcd/} | ||
128 | + assert_search "*", [], where: {name: /bcd\z/} | ||
141 | end | 129 | end |
142 | 130 | ||
143 | def test_regexp_case | 131 | def test_regexp_case |