Permalink
Browse files

escape all lucene special characters, remove special case for !, upda…

…te unit tests
  • Loading branch information...
1 parent 552d93d commit e84409c497022273e58b3a69445e1c88655be404 Dan Dockery committed Nov 11, 2011
Showing with 61 additions and 25 deletions.
  1. +1 −7 lib/elastic_searchable.rb
  2. +60 −18 test/test_elastic_searchable.rb
@@ -43,13 +43,7 @@ def request(method, url, options = {})
end
def escape_query(string)
- q = string.to_s.gsub(/([\(\)\[\]\{\}\?\\\"\\^])/,'\\\\\1')
- # escape any exclamation marks followed by whitespace or the end of the string
- # lucene will interpret exclamation marks as a negation operator
- # This should allow users to continue to use ! for negation while avoiding most errors
- q.gsub!(/!(!|\s|\z|\\\))/, '\\!\1')
- q.gsub!('!!','!\\!')
- q
+ string.to_s.gsub(/([\(\)\[\]\{\}\?\\\"!\^\+\-\*:~])/,'\\\\\1')
end
private
@@ -476,40 +476,82 @@ def self.max_per_page
assert_equal '\!', result
end
- should "escape two exclamation marks" do
- queryString = '!!'
+ should "escape ^" do
+ queryString = '^'
result = ElasticSearchable.escape_query(queryString)
- assert_equal '\!\!', result
+ assert_equal '\^', result
end
- should "escape five exclamation marks" do
- queryString = '!!!!!'
+ should "escape +" do
+ queryString = '+'
result = ElasticSearchable.escape_query(queryString)
- assert_equal '\!\!\!\!\!', result
+ assert_equal '\+', result
end
- should "leave exlamation at the beginning of a word intact" do
- queryString = '!monkey'
+ should "escape -" do
+ queryString = '-'
result = ElasticSearchable.escape_query(queryString)
- assert_equal '!monkey', result
+ assert_equal '\-', result
end
- should "escape exclamation mark before whitespace" do
- queryString = '! '
+ should "escape (" do
+ queryString = '('
result = ElasticSearchable.escape_query(queryString)
- assert_equal '\! ', result
+ assert_equal '\(', result
end
- should "escape exclamation mark before closing parens" do
- queryString = '!)'
+ should "escape )" do
+ queryString = ')'
result = ElasticSearchable.escape_query(queryString)
- assert_equal '\!\)', result
+ assert_equal '\)', result
end
- should "escape ^" do
- queryString = '^'
+ should "escape {" do
+ queryString = '}'
result = ElasticSearchable.escape_query(queryString)
- assert_equal '\^', result
+ assert_equal '\}', result
+ end
+
+ should "escape [" do
+ queryString = '['
+ result = ElasticSearchable.escape_query(queryString)
+ assert_equal '\[', result
+ end
+
+ should "escape ]" do
+ queryString = ']'
+ result = ElasticSearchable.escape_query(queryString)
+ assert_equal '\]', result
+ end
+
+ should 'escape "' do
+ queryString = '"'
+ result = ElasticSearchable.escape_query(queryString)
+ assert_equal '\"', result
+ end
+
+ should "escape ~" do
+ queryString = '~'
+ result = ElasticSearchable.escape_query(queryString)
+ assert_equal '\~', result
+ end
+
+ should "escape *" do
+ queryString = '*'
+ result = ElasticSearchable.escape_query(queryString)
+ assert_equal '\*', result
+ end
+
+ should "escape :" do
+ queryString = ':'
+ result = ElasticSearchable.escape_query(queryString)
+ assert_equal '\:', result
+ end
+
+ should "escape ?" do
+ queryString = '?'
+ result = ElasticSearchable.escape_query(queryString)
+ assert_equal '\?', result
end
end
end

0 comments on commit e84409c

Please sign in to comment.