mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-21 21:14:12 +08:00
Made improvements to LuceneSerializer (checking types etc.) as suggested by tiwe. Also fixed LIKE query logic, it was by default doing *query* which was of course not desired, the changes made there were also implemented in the tests.
This commit is contained in:
parent
12b29cb5f4
commit
94339b7ce2
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.search;
|
||||
|
||||
@ -16,6 +16,7 @@ import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
|
||||
import com.mysema.query.types.expr.Constant;
|
||||
import com.mysema.query.types.expr.Expr;
|
||||
import com.mysema.query.types.operation.Operation;
|
||||
import com.mysema.query.types.operation.Operator;
|
||||
@ -24,8 +25,6 @@ import com.mysema.query.types.path.PString;
|
||||
import com.mysema.query.types.path.Path;
|
||||
|
||||
public class LuceneSerializer {
|
||||
private static final char WILDCARD_ALL = '*';
|
||||
|
||||
private final boolean lowerCase;
|
||||
|
||||
public LuceneSerializer(boolean lowerCase) {
|
||||
@ -69,36 +68,41 @@ public class LuceneSerializer {
|
||||
Operator<?> op = operation.getOperator();
|
||||
if (op == Ops.OR) {
|
||||
return toOrQuery(operation);
|
||||
|
||||
|
||||
} else if (op == Ops.AND) {
|
||||
return toAndQuery(operation);
|
||||
|
||||
|
||||
} else if (op == Ops.LIKE) {
|
||||
if (!(operation.getArg(1) instanceof Constant<?>)) {
|
||||
throw new IllegalArgumentException("operation argument was not of type Constant.");
|
||||
}
|
||||
String[] terms = StringUtils.split(operation.getArg(1).toString());
|
||||
if (terms.length > 1) {
|
||||
toPhraseQuery(operation, terms);
|
||||
}
|
||||
String term = operation.getArg(1).toString();
|
||||
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), WILDCARD_ALL + (lowerCase ? term.toLowerCase() : term) + WILDCARD_ALL));
|
||||
|
||||
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());
|
||||
if (terms.length > 1) {
|
||||
return toPhraseQuery(operation, terms);
|
||||
}
|
||||
// TODO : make sure second arg is constant
|
||||
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) {
|
||||
return toNotQuery(operation);
|
||||
|
||||
} else if (op == Ops.STARTS_WITH) {
|
||||
} else if (op == Ops.STARTS_WITH && operation.getArg(1) instanceof Constant<?>) {
|
||||
if (!(operation.getArg(1) instanceof Constant<?>)) {
|
||||
throw new IllegalArgumentException("operation argument was not of type Constant.");
|
||||
}
|
||||
String[] terms = StringUtils.split(operation.getArg(1).toString());
|
||||
if (terms.length > 1) {
|
||||
return toPhraseQuery(operation, terms);
|
||||
}
|
||||
// TODO : make sure second arg is constant
|
||||
String term = operation.getArg(1).toString();
|
||||
return new PrefixQuery(new Term(toField((PString) operation.getArg(0)), lowerCase ? term.toLowerCase() : term));
|
||||
}
|
||||
@ -108,10 +112,8 @@ public class LuceneSerializer {
|
||||
public Query toQuery(Expr<?> expr) {
|
||||
if (expr instanceof Operation<?, ?>) {
|
||||
return toQuery((Operation<?, ?>) expr);
|
||||
}else{
|
||||
throw new IllegalArgumentException("expr was not of type Operation");
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("expr was not of type Operation");
|
||||
}
|
||||
|
||||
public String toField(Path<?> path) {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.search;
|
||||
|
||||
@ -81,7 +81,7 @@ public class SimpleTest {
|
||||
|
||||
@Test
|
||||
public void test_like() throws Exception {
|
||||
Query q = serializer.toQuery(author.like("ichael"));
|
||||
Query q = serializer.toQuery(author.like("*ichael*"));
|
||||
TopDocs docs = searcher.search(q, 100);
|
||||
assertEquals(1, docs.totalHits);
|
||||
assertEquals("author:*ichael*", q.toString());
|
||||
@ -92,12 +92,12 @@ public class SimpleTest {
|
||||
Query q = serializer.toQuery(author.like("Mi?hael"));
|
||||
TopDocs docs = searcher.search(q, 100);
|
||||
assertEquals(1, docs.totalHits);
|
||||
assertEquals("author:*mi?hael*", q.toString());
|
||||
assertEquals("author:mi?hael", q.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_like_Custom_Wildcard_Multiple_Character() throws Exception {
|
||||
Query q = serializer.toQuery(text.like("U*X"));
|
||||
Query q = serializer.toQuery(text.like("*U*X*"));
|
||||
TopDocs docs = searcher.search(q, 100);
|
||||
assertEquals(1, docs.totalHits);
|
||||
assertEquals("text:*u*x*", q.toString());
|
||||
@ -115,18 +115,18 @@ public class SimpleTest {
|
||||
|
||||
@Test
|
||||
public void test_like_or_like() throws Exception {
|
||||
Query q = serializer.toQuery(title.like("House").or(year.like("99")));
|
||||
Query q = serializer.toQuery(title.like("House").or(year.like("*99*")));
|
||||
TopDocs docs = searcher.search(q, 100);
|
||||
assertEquals(1, docs.totalHits);
|
||||
assertEquals("title:*house* year:*99*", q.toString());
|
||||
assertEquals("title:house year:*99*", q.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_like_and_like() throws Exception {
|
||||
Query q = serializer.toQuery(title.like("assic").and(year.like("99")));
|
||||
Query q = serializer.toQuery(title.like("*assic*").and(year.like("199?")));
|
||||
TopDocs docs = searcher.search(q, 100);
|
||||
assertEquals(1, docs.totalHits);
|
||||
assertEquals("+title:*assic* +year:*99*", q.toString());
|
||||
assertEquals("+title:*assic* +year:199?", q.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -195,10 +195,10 @@ public class SimpleTest {
|
||||
|
||||
@Test
|
||||
public void test_like_not_Does_Not_Find_Results() throws Exception {
|
||||
Query q = serializer.toQuery(title.like("House").not());
|
||||
Query q = serializer.toQuery(title.like("*H*e*").not());
|
||||
TopDocs docs = searcher.search(q, 100);
|
||||
assertEquals(0, docs.totalHits);
|
||||
assertEquals("-title:*house*", q.toString());
|
||||
assertEquals("-title:*h*e*", q.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Loading…
Reference in New Issue
Block a user