SOLR-1083 -- An Evaluator for escaping query characters

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@757682 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-03-24 08:09:49 +00:00
parent 6bd4909102
commit dfdc5a80e1
3 changed files with 44 additions and 2 deletions

View File

@ -117,6 +117,9 @@ New Features
location of the data source.
(Jason Shepherd, Noble Paul via shalin)
28.SOLR-1083: An Evaluator for escaping query characters.
(Noble Paul, shalin)
Optimizations
----------------------
1. SOLR-846: Reduce memory consumption during delta import by removing keys when used

View File

@ -23,6 +23,7 @@ import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVE
import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
import static org.apache.solr.handler.dataimport.DocBuilder.loadClass;
import org.apache.solr.util.DateMathParser;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -50,6 +51,8 @@ public class EvaluatorBag {
public static final String URL_ENCODE_EVALUATOR = "encodeUrl";
public static final String ESCAPE_SOLR_QUERY_CHARS = "escapeQueryChars";
public static final String SQL_ESCAPE_EVALUATOR = "escapeSql";
static final Pattern FORMAT_METHOD = Pattern
.compile("^(\\w*?)\\((.*?)\\)$");
@ -74,6 +77,27 @@ public class EvaluatorBag {
};
}
/**
* <p/>Returns an <code>Evaluator</code> instance meant to be used for escaping reserved characters in Solr
* queries</p>
*
* @return an <code>Evaluator</code> instance capable of escaping reserved characters in solr queries.
*
* @see org.apache.solr.client.solrj.util.ClientUtils#escapeQueryChars(String)
*/
public static Evaluator getSolrQueryEscapingEvaluator() {
return new Evaluator() {
public String evaluate(String expression, Context context) {
List l = parseParams(expression, context.getVariableResolver());
if (l.size() != 1) {
throw new DataImportHandlerException(SEVERE, "'escapeQueryChars' must have at least one parameter ");
}
String s = l.get(0).toString();
return ClientUtils.escapeQueryChars(s);
}
};
}
/**
* <p/> Returns an <code>Evaluator</code> instance capable of URL-encoding expressions. The expressions are evaluated
* using a <code>VariableResolver</code> </p>
@ -165,6 +189,7 @@ public class EvaluatorBag {
evaluators.put(DATE_FORMAT_EVALUATOR, getDateFormatEvaluator());
evaluators.put(SQL_ESCAPE_EVALUATOR, getSqlEscapingEvaluator());
evaluators.put(URL_ENCODE_EVALUATOR, getUrlEvaluator());
evaluators.put(ESCAPE_SOLR_QUERY_CHARS, getSolrQueryEscapingEvaluator());
SolrCore core = docBuilder == null ? null : docBuilder.dataImporter.getCore();
for (Map<String, String> map : fn) {
try {

View File

@ -80,6 +80,7 @@ public class TestEvaluatorBag {
Evaluator urlEvaluator = EvaluatorBag.getUrlEvaluator();
runTests(urlTests, urlEvaluator);
}
@Test
public void parseParams() {
Map m = new HashMap();
@ -91,19 +92,32 @@ public class TestEvaluatorBag {
Assert.assertEquals("B",((EvaluatorBag.VariableWrapper)l.get(1)).resolve());
Assert.assertEquals("hello!",l.get(2));
Assert.assertEquals("ds,o,u'za",l.get(3));
}
@Test
public void testEscapeSolrQueryFunction() {
final VariableResolverImpl resolver = new VariableResolverImpl();
ContextImpl context = new ContextImpl(null, resolver, null, 0, Collections.EMPTY_MAP, null, null);
resolver.context = context;
Map m= new HashMap();
m.put("query","c:t");
resolver.addNamespace("dataimporter.functions", EvaluatorBag
.getFunctionsNamespace(Collections.EMPTY_LIST, null));
resolver.addNamespace("e",m);
String s = resolver
.replaceTokens("${dataimporter.functions.escapeQueryChars(e.query)}");
org.junit.Assert.assertEquals("c\\:t", s);
}
/**
* Test method for {@link EvaluatorBag#getDateFormatEvaluator()}.
*/
@Test
@Ignore
public void testGetDateFormatEvaluator() {
Evaluator dateFormatEval = EvaluatorBag.getDateFormatEvaluator();
resolver.context = new ContextImpl(null, resolver, null, 0, Collections.EMPTY_MAP, null, null);
assertEquals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()),
assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()),
dateFormatEval.evaluate("'NOW','yyyy-MM-dd HH:mm'", resolver.context));
Map<String, Object> map = new HashMap<String, Object>();