diff --git a/client/java/solrj/CHANGES.txt b/client/java/solrj/CHANGES.txt index f3fa4793822..ddcfb64166d 100644 --- a/client/java/solrj/CHANGES.txt +++ b/client/java/solrj/CHANGES.txt @@ -18,6 +18,8 @@ Detailed Change List New Features ---------------------- + 1. SOLR-794: added escape() method to ClientUtils. (koji) + Optimizations ---------------------- diff --git a/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java b/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java index 90038e537dd..187ead85664 100644 --- a/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java +++ b/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java @@ -191,15 +191,32 @@ public class ClientUtils private static final Pattern escapePattern = Pattern.compile( "(\\W)" ); /** - * See: http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping Special Characters + * Non-word characters are escaped by a preceding \. */ public static String escapeQueryChars( String input ) { Matcher matcher = escapePattern.matcher( input ); return matcher.replaceAll( "\\\\$1" ); } - + /** + * See: http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping Special Characters + */ + public static String escape(String s) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + // These characters are part of the query syntax and must be escaped + if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' + || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~' + || c == '*' || c == '?' || c == '|' || c == '&') { + sb.append('\\'); + } + sb.append(c); + } + return sb.toString(); + } + public static String toQueryString( SolrParams params, boolean xml ) { StringBuilder sb = new StringBuilder(128); try {