I have \"documents\" (activerecords) with an attribute called deviations. The attribute has values like \"Bin X\" \"Bin $\" \"Bin q\" \"Bin %\" etc.
I am trying to use t
You can sanitize your query string. Here is a sanitizer that works for everything that I've tried throwing at it:
def sanitize_string_for_elasticsearch_string_query(str)
# Escape special characters
# http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html#Escaping Special Characters
escaped_characters = Regexp.escape('\\/+-&|!(){}[]^~*?:')
str = str.gsub(/([#{escaped_characters}])/, '\\\\\1')
# AND, OR and NOT are used by lucene as logical operators. We need
# to escape them
['AND', 'OR', 'NOT'].each do |word|
escaped_word = word.split('').map {|char| "\\#{char}" }.join('')
str = str.gsub(/\s*\b(#{word.upcase})\b\s*/, " #{escaped_word} ")
end
# Escape odd quotes
quote_count = str.count '"'
str = str.gsub(/(.*)"(.*)/, '\1\"\3') if quote_count % 2 == 1
str
end
params[:query] = sanitize_string_for_elasticsearch_string_query(params[:query])