Sunspot automaticly escape special characters -
using sunspot , solr 4+ there way automatically escape special characters.
for example in simple fulltext search like:
post.search fulltext term end
if term
contains of special chars (http://lucene.apache.org/core/4_0_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#regexp_searches) should auto escaped.
inside initializers file add following code
re_escape_solr = /([-+!\(\)\{\}\[\]^"~*?:\\]|&&|\|\|)/ class string def escape_solr gsub(re_escape_solr) { |e| "\\#{e}" } end end
and whenever searching can call
post.search fulltext term.escape_solr end
Comments
Post a Comment