Author: rwesten
Date: Fri Sep 30 10:31:10 2011
New Revision: 1177581
URL: http://svn.apache.org/viewvc?rev=1177581&view=rev
Log:
fixed a bug that cause Lucene special chars not to be correctly escaped in the
case of Wildcard queries to the SolrYard. Now only '*' and '?' are NOT escaped
in such cases
Modified:
incubator/stanbol/trunk/commons/solr/src/main/java/org/apache/stanbol/commons/solr/utils/SolrUtil.java
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/query/QueryUtils.java
Modified:
incubator/stanbol/trunk/commons/solr/src/main/java/org/apache/stanbol/commons/solr/utils/SolrUtil.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/src/main/java/org/apache/stanbol/commons/solr/utils/SolrUtil.java?rev=1177581&r1=1177580&r2=1177581&view=diff
==============================================================================
---
incubator/stanbol/trunk/commons/solr/src/main/java/org/apache/stanbol/commons/solr/utils/SolrUtil.java
(original)
+++
incubator/stanbol/trunk/commons/solr/src/main/java/org/apache/stanbol/commons/solr/utils/SolrUtil.java
Fri Sep 30 10:31:10 2011
@@ -21,8 +21,10 @@ import java.util.regex.Pattern;
public final class SolrUtil {
private SolrUtil() {}
- private static final String LUCENE_ESCAPE_CHARS =
"[\\\\+\\-\\!\\(\\)\\:\\^\\[\\]\\{\\}\\~\\*\\?]";
+ private static final String LUCENE_ESCAPE_CHARS =
"[\\\\+\\-\\!\\(\\)\\:\\^\\[\\]\\{\\}\\~\\*\\?\\\"]";
private static final Pattern LUCENE_PATTERN =
Pattern.compile(LUCENE_ESCAPE_CHARS);
+ private static final String WILDCARD_ESCAPE_CHARS =
"[\\\\+\\-\\!\\(\\)\\:\\^\\[\\]\\{\\}\\~\\\"]";
+ private static final Pattern WILDCARD_PATTERN =
Pattern.compile(WILDCARD_ESCAPE_CHARS);
private static final String REPLACEMENT_STRING = "\\\\$0";
/**
@@ -33,6 +35,18 @@ public final class SolrUtil {
* @return the escaped string
*/
public static String escapeSolrSpecialChars(String string) {
- return string != null ?
LUCENE_PATTERN.matcher(string).replaceAll(REPLACEMENT_STRING) : null;
+ String escaped = string != null ?
LUCENE_PATTERN.matcher(string).replaceAll(REPLACEMENT_STRING) : null;
+ return escaped;
+ }
+ /**
+ * Escapes all Solr special chars except the '*' and '?' as used for
Wildcard
+ * searches
+ * @param string the string representing a wildcard search that needs to
+ * be escaped
+ * @return the escaped version of the wildcard search
+ */
+ public static String escapeWildCardString(String string){
+ String escaped = string != null ?
WILDCARD_PATTERN.matcher(string).replaceAll(REPLACEMENT_STRING) : null;
+ return escaped;
}
}
Modified:
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/query/QueryUtils.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/query/QueryUtils.java?rev=1177581&r1=1177580&r2=1177581&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/query/QueryUtils.java
(original)
+++
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/query/QueryUtils.java
Fri Sep 30 10:31:10 2011
@@ -45,6 +45,9 @@ public final class QueryUtils {
*
* @param value
* the index value
+ * @param escape if <code>true</code> all Solr special chars are escaped if
+ * <code>false</code> than '*' and '?' as used for wildcard searches are
+ * not escaped.
* @return the (possible multiple) values that need to be connected with
AND
*/
public static String[] encodeQueryValue(IndexValue indexValue, boolean
escape) {
@@ -55,6 +58,8 @@ public final class QueryUtils {
String value = indexValue.getValue();
if (escape) {
value = SolrUtil.escapeSolrSpecialChars(value);
+ } else {
+ value = SolrUtil.escapeWildCardString(value);
}
if (IndexDataTypeEnum.TXT.getIndexType().equals(indexValue.getType()))
{
value = value.toLowerCase();