
您可以清理查询字符串。这是一种消毒剂,适用于我尝试扔给它的所有东西:
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})bs*/, " #{escaped_word} ") end # Escape odd quotes quote_count = str.count '"' str = str.gsub(/(.*)"(.*)/, '1"3') if quote_count % 2 == 1 strendparams[:query] = sanitize_string_for_elasticsearch_string_query(params[:query])欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)