Commit 0ebb8fb7e0ae6975aec332a9b8bd8fe274a8ec91

Authored by Jono Warren
Committed by GitHub
1 parent dd85a230

Add support for case-insensitive regex filter (#1510)

Showing 2 changed files with 18 additions and 8 deletions   Show diff stats
lib/searchkick/query.rb
... ... @@ -1028,10 +1028,6 @@ module Searchkick
1028 1028 elsif value.nil?
1029 1029 {bool: {must_not: {exists: {field: field}}}}
1030 1030 elsif value.is_a?(Regexp)
1031   - if value.casefold?
1032   - Searchkick.warn("Case-insensitive flag does not work with Elasticsearch")
1033   - end
1034   -
1035 1031 source = value.source
1036 1032 unless source.start_with?("\\A") && source.end_with?("\\z")
1037 1033 # https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html
... ... @@ -1053,7 +1049,14 @@ module Searchkick
1053 1049 # source = "#{source}.*"
1054 1050 end
1055 1051  
1056   - {regexp: {field => {value: source, flags: "NONE"}}}
  1052 + if below710?
  1053 + if value.casefold?
  1054 + Searchkick.warn("Case-insensitive flag does not work with Elasticsearch < 7.10")
  1055 + end
  1056 + {regexp: {field => {value: source, flags: "NONE"}}}
  1057 + else
  1058 + {regexp: {field => {value: source, flags: "NONE", case_insensitive: value.casefold?}}}
  1059 + end
1057 1060 else
1058 1061 # TODO add this for other values
1059 1062 if value.as_json.is_a?(Enumerable)
... ... @@ -1151,5 +1154,9 @@ module Searchkick
1151 1154 def below75?
1152 1155 Searchkick.server_below?("7.5.0")
1153 1156 end
  1157 +
  1158 + def below710?
  1159 + Searchkick.server_below?("7.10.0")
  1160 + end
1154 1161 end
1155 1162 end
... ...
test/where_test.rb
... ... @@ -141,9 +141,12 @@ class WhereTest &lt; Minitest::Test
141 141 def test_regexp_case
142 142 store_names ["abcde"]
143 143 assert_search "*", [], where: {name: /\AABCDE\z/}
144   - # flags don't work
145   - assert_warns "Case-insensitive flag does not work with Elasticsearch" do
146   - assert_search "*", [], where: {name: /\AABCDE\z/i}
  144 + if Searchkick.server_below?("7.10.0")
  145 + assert_warns "Case-insensitive flag does not work with Elasticsearch < 7.10" do
  146 + assert_search "*", [], where: {name: /\AABCDE\z/i}
  147 + end
  148 + else
  149 + assert_search "*", ["abcde"], where: {name: /\AABCDE\z/i}
147 150 end
148 151 end
149 152  
... ...