Skip to content

Apache Lucene open-source search software

escape all non-alpha/numeric characters with \

Ryan McKinleydeafb3207ed0
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 926fa4d2d2b..715b3893512 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
@@ -29,6 +29,8 @@ import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
@@ -161,38 +163,15 @@ public class ClientUtils
}
}
-
+ private static final Pattern escapePattern = Pattern.compile( "(\\W)" );
+
/**
* See: http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping Special Characters
*/
public static String escapeQueryChars( String input )
{
- char buff[] = input.toCharArray();
- StringBuilder str = new StringBuilder( buff.length+5 );
- for( char c : buff ) {
- switch( c ) {
- case '+':
- case '-':
- case '&':
- case '|':
- case '(':
- case ')':
- case '{':
- case '}':
- case '[':
- case ']':
- case '^':
- case '"':
- case '*':
- case ':':
- case '~':
- case '!':
- case '\\':
- str.append( '\\' );
- }
- str.append( c );
- }
- return str.toString();
+ Matcher matcher = escapePattern.matcher( input );
+ return matcher.replaceAll( "\\\\$1" );
}
diff --git a/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java b/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java
new file mode 100644
index 00000000000..77beeb35ce9
--- /dev/null
+++ b/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.client.solrj.util;
+
+import junit.framework.TestCase;
+
+/**
+ *
+ * @version $Id$
+ * @since solr 1.3
+ */
+public class ClientUtilsTest extends TestCase {
+
+ public void testEscapeQuery()
+ {
+ assertEquals( "nochange", ClientUtils.escapeQueryChars( "nochange" ) );
+ assertEquals( "12345", ClientUtils.escapeQueryChars( "12345" ) );
+ assertEquals( "with\\ space", ClientUtils.escapeQueryChars( "with space" ) );
+ assertEquals( "h\\:ello\\!", ClientUtils.escapeQueryChars( "h:ello!" ) );
+ assertEquals( "h\\~\\!", ClientUtils.escapeQueryChars( "h~!" ) );
+ }
+}