"contains" now escapes user's input, so user can't input own wildcards, that is what "like" is for.

This commit is contained in:
Vesa Martilla 2010-03-19 13:54:18 +00:00
parent 672bb057eb
commit ad2b0c7a39
2 changed files with 15 additions and 6 deletions

View File

@ -7,6 +7,7 @@ package com.mysema.query.search;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.PhraseQuery;
@ -76,21 +77,21 @@ public class LuceneSerializer {
if (!(operation.getArg(1) instanceof Constant<?>)) {
throw new IllegalArgumentException("operation argument was not of type Constant.");
}
String[] terms = StringUtils.split(operation.getArg(1).toString());
String term = operation.getArg(1).toString();
String[] terms = StringUtils.split(term);
if (terms.length > 1) {
toPhraseQuery(operation, terms);
}
String term = operation.getArg(1).toString();
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), lowerCase ? term.toLowerCase() : term));
} else if (op == Ops.EQ_OBJECT || op == Ops.EQ_PRIMITIVE) {
if (!(operation.getArg(1) instanceof Constant<?>)) {
throw new IllegalArgumentException("operation argument was not of type Constant.");
}
String[] terms = StringUtils.split(operation.getArg(1).toString());
String term = operation.getArg(1).toString();
String[] terms = StringUtils.split(term);
if (terms.length > 1) {
return toPhraseQuery(operation, terms);
}
String term = operation.getArg(1).toString();
return new TermQuery(new Term(toField((PString) operation.getArg(0)), lowerCase ? term.toLowerCase() : term));
} else if (op == Ops.NOT) {
@ -110,11 +111,11 @@ public class LuceneSerializer {
if (!(operation.getArg(1) instanceof Constant<?>)) {
throw new IllegalArgumentException("operation argument was not of type Constant.");
}
String[] terms = StringUtils.split(operation.getArg(1).toString());
String term = QueryParser.escape(operation.getArg(1).toString());
String[] terms = StringUtils.split(term);
if (terms.length > 1) {
toPhraseQuery(operation, terms);
}
String term = operation.getArg(1).toString();
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), "*" + (lowerCase ? term.toLowerCase() : term) + "*"));
}
throw new UnsupportedOperationException();

View File

@ -252,6 +252,14 @@ public class SimpleTest {
assertEquals("title:*rassic Pa*", q.toString());
}
@Test
public void test_contains_User_Inputted_Wildcards_Dont_Work() throws Exception {
Query q = serializer.toQuery(title.contains("r*i"));
TopDocs docs = searcher.search(q, 100);
assertEquals(0, docs.totalHits);
assertEquals("title:*r\\*i*", q.toString());
}
@Test
public void specs(){
Session session = null;