moved lowercase transformation to own method

This commit is contained in:
Timo Westkämper 2010-03-21 15:36:38 +00:00
parent 8e6813466d
commit d9030cfdfb

View File

@ -5,6 +5,8 @@
*/
package com.mysema.query.search;
import java.util.Locale;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
@ -32,22 +34,16 @@ public class LuceneSerializer {
this.lowerCase = lowerCase;
}
private Query toOrQuery(Operation<?, ?> operation) {
return toTwoHandSidedQuery(operation, Occur.SHOULD);
private String normalize(String term) {
return lowerCase ? term.toLowerCase(Locale.ENGLISH) : term;
}
private Query toAndQuery(Operation<?, ?> operation) {
return toTwoHandSidedQuery(operation, Occur.MUST);
}
private Query toTwoHandSidedQuery(Operation<?, ?> operation, Occur occur) {
// TODO Flatten similar queries(?)
Query lhs = toQuery(operation.getArg(0));
Query rhs = toQuery(operation.getArg(1));
BooleanQuery bq = new BooleanQuery();
bq.add(lhs, occur);
bq.add(rhs, occur);
return bq;
public String toField(Path<?> path) {
return path.getMetadata().getExpression().toString();
}
private Query toNotQuery(Operation<?, ?> operation) {
@ -56,14 +52,25 @@ public class LuceneSerializer {
return bq;
}
private Query toOrQuery(Operation<?, ?> operation) {
return toTwoHandSidedQuery(operation, Occur.SHOULD);
}
private Query toPhraseQuery(Operation<?, ?> operation, String[] terms) {
PhraseQuery pq = new PhraseQuery();
for (String term : terms) {
pq.add(new Term(toField((PString) operation.getArg(0)), lowerCase ? term.toLowerCase() : term));
pq.add(new Term(toField((PString) operation.getArg(0)), normalize(term)));
}
return pq;
}
public Query toQuery(Expr<?> expr) {
if (expr instanceof Operation<?, ?>) {
return toQuery((Operation<?, ?>) expr);
}
throw new IllegalArgumentException("expr was not of type Operation");
}
private Query toQuery(Operation<?, ?> operation) {
Operator<?> op = operation.getOperator();
if (op == Ops.OR) {
@ -80,7 +87,7 @@ public class LuceneSerializer {
if (terms.length > 1) {
return toPhraseQuery(operation, terms);
}
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), lowerCase ? term.toLowerCase() : term));
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), normalize(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.");
@ -90,7 +97,7 @@ public class LuceneSerializer {
if (terms.length > 1) {
return toPhraseQuery(operation, terms);
}
return new TermQuery(new Term(toField((PString) operation.getArg(0)), lowerCase ? term.toLowerCase() : term));
return new TermQuery(new Term(toField((PString) operation.getArg(0)), normalize(term)));
} else if (op == Ops.NOT) {
return toNotQuery(operation);
} else if (op == Ops.STARTS_WITH) {
@ -102,7 +109,7 @@ public class LuceneSerializer {
return toPhraseQuery(operation, terms);
}
String term = operation.getArg(1).toString();
return new PrefixQuery(new Term(toField((PString) operation.getArg(0)), lowerCase ? term.toLowerCase() : term));
return new PrefixQuery(new Term(toField((PString) operation.getArg(0)), normalize(term)));
} else if (op == Ops.STRING_CONTAINS) {
if (!(operation.getArg(1) instanceof Constant<?>)) {
throw new IllegalArgumentException("operation argument was not of type Constant.");
@ -112,7 +119,7 @@ public class LuceneSerializer {
if (terms.length > 1) {
return toPhraseQuery(operation, terms);
}
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), "*" + (lowerCase ? term.toLowerCase() : term) + "*"));
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), "*" + (normalize(term)) + "*"));
} else if (op == Ops.ENDS_WITH) {
if (!(operation.getArg(1) instanceof Constant<?>)) {
throw new IllegalArgumentException("operation argument was not of type Constant.");
@ -122,19 +129,18 @@ public class LuceneSerializer {
if (terms.length > 1) {
return toPhraseQuery(operation, terms);
}
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), "*" + (lowerCase ? term.toLowerCase() : term)));
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), "*" + (normalize(term))));
}
throw new UnsupportedOperationException();
}
public Query toQuery(Expr<?> expr) {
if (expr instanceof Operation<?, ?>) {
return toQuery((Operation<?, ?>) expr);
}
throw new IllegalArgumentException("expr was not of type Operation");
}
public String toField(Path<?> path) {
return path.getMetadata().getExpression().toString();
private Query toTwoHandSidedQuery(Operation<?, ?> operation, Occur occur) {
// TODO Flatten similar queries(?)
Query lhs = toQuery(operation.getArg(0));
Query rhs = toQuery(operation.getArg(1));
BooleanQuery bq = new BooleanQuery();
bq.add(lhs, occur);
bq.add(rhs, occur);
return bq;
}
}