LUCENE-1061: make it simpler to override concrete Query implementations created by QueryParser by factoring out protected newXXXQuery() methods

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@690535 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-08-30 17:12:19 +00:00
parent 09114186ba
commit 1bbfaa5445
3 changed files with 232 additions and 41 deletions

View File

@ -125,6 +125,11 @@ API Changes
19. LUCENE-1367: Add IndexCommit.isDeleted(). (Shalin Shekhar Mangar
via Mike McCandless)
20. LUCENE-1061: Factored out all "new XXXQuery(...)" in
QueryParser.java into protected methods newXXXQuery(...) so that
subclasses can create their own subclasses of each Query type.
(John Wang via Mike McCandless)
Bug fixes
1. LUCENE-1134: Fixed BooleanQuery.rewrite to only optimize a single

View File

@ -166,7 +166,7 @@ public class QueryParser implements QueryParserConstants {
try {
// TopLevelQuery is a Query followed by the end-of-input (EOF)
Query res = TopLevelQuery(field);
return res!=null ? res : new BooleanQuery();
return res!=null ? res : newBooleanQuery(false);
}
catch (ParseException tme) {
// rethrow to include the original query:
@ -453,11 +453,11 @@ public class QueryParser implements QueryParserConstants {
required = (!prohibited && conj != CONJ_OR);
}
if (required && !prohibited)
clauses.addElement(new BooleanClause(q, BooleanClause.Occur.MUST));
clauses.addElement(newBooleanClause(q, BooleanClause.Occur.MUST));
else if (!required && !prohibited)
clauses.addElement(new BooleanClause(q, BooleanClause.Occur.SHOULD));
clauses.addElement(newBooleanClause(q, BooleanClause.Occur.SHOULD));
else if (!required && prohibited)
clauses.addElement(new BooleanClause(q, BooleanClause.Occur.MUST_NOT));
clauses.addElement(newBooleanClause(q, BooleanClause.Occur.MUST_NOT));
else
throw new RuntimeException("Clause cannot be both required and prohibited");
}
@ -503,15 +503,15 @@ public class QueryParser implements QueryParserConstants {
return null;
else if (v.size() == 1) {
nextToken = (org.apache.lucene.analysis.Token) v.elementAt(0);
return new TermQuery(new Term(field, nextToken.term()));
return newTermQuery(new Term(field, nextToken.term()));
} else {
if (severalTokensAtSamePosition) {
if (positionCount == 1) {
// no phrase query:
BooleanQuery q = new BooleanQuery(true);
BooleanQuery q = newBooleanQuery(true);
for (int i = 0; i < v.size(); i++) {
nextToken = (org.apache.lucene.analysis.Token) v.elementAt(i);
TermQuery currentQuery = new TermQuery(
Query currentQuery = newTermQuery(
new Term(field, nextToken.term()));
q.add(currentQuery, BooleanClause.Occur.SHOULD);
}
@ -519,7 +519,7 @@ public class QueryParser implements QueryParserConstants {
}
else {
// phrase query:
MultiPhraseQuery mpq = new MultiPhraseQuery();
MultiPhraseQuery mpq = newMultiPhraseQuery();
mpq.setSlop(phraseSlop);
List multiTerms = new ArrayList();
int position = -1;
@ -545,7 +545,7 @@ public class QueryParser implements QueryParserConstants {
}
}
else {
PhraseQuery pq = new PhraseQuery();
PhraseQuery pq = newPhraseQuery();
pq.setSlop(phraseSlop);
int position = -1;
for (int i = 0; i < v.size(); i++) {
@ -628,11 +628,87 @@ public class QueryParser implements QueryParserConstants {
}
catch (Exception e) { }
return newRangeQuery(field, part1, part2, inclusive);
}
/**
* Builds a new BooleanQuery instance
* @param disableCoord disable coord
* @return new BooleanQuery instance
*/
protected BooleanQuery newBooleanQuery(boolean disableCoord) {
return new BooleanQuery(disableCoord);
}
/**
* Builds a new BooleanClause instance
* @param q sub query
* @param occur how this clause should occur when matching documents
* @return new BooleanClause instance
*/
protected BooleanClause newBooleanClause(Query q, BooleanClause.Occur occur) {
return new BooleanClause(q, occur);
}
/**
* Builds a new TermQuery instance
* @param term term
* @return new TermQuery instance
*/
protected Query newTermQuery(Term term){
return new TermQuery(term);
}
/**
* Builds a new PhraseQuery instance
* @return new PhraseQuery instance
*/
protected PhraseQuery newPhraseQuery(){
return new PhraseQuery();
}
/**
* Builds a new MultiPhraseQuery instance
* @return new MultiPhraseQuery instance
*/
protected MultiPhraseQuery newMultiPhraseQuery(){
return new MultiPhraseQuery();
}
/**
* Builds a new PrefixQuery instance
* @param prefix Prefix term
* @return new PrefixQuery instance
*/
protected Query newPrefixQuery(Term prefix){
return new PrefixQuery(prefix);
}
/**
* Builds a new FuzzyQuery instance
* @param term Term
* @param minimumSimilarity minimum similarity
* @param prefixLength prefix length
* @return new FuzzyQuery Instance
*/
protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength) {
return new FuzzyQuery(term,minimumSimilarity,prefixLength);
}
/**
* Builds a new RangeQuery instance
* @param field Field
* @param part1 min
* @param part2 max
* @param inclusive true if range is inclusive
* @return new RangeQuery instance
*/
protected Query newRangeQuery(String field, String part1, String part2, boolean inclusive) {
if(useOldRangeQuery)
{
return new RangeQuery(new Term(field, part1),
new Term(field, part2),
inclusive);
return new RangeQuery(new Term(field, part1),
new Term(field, part2),
inclusive);
}
else
{
@ -640,6 +716,23 @@ public class QueryParser implements QueryParserConstants {
}
}
/**
* Builds a new MatchAllDocsQuery instance
* @return new MatchAllDocsQuery instance
*/
protected Query newMatchAllDocsQuery() {
return new MatchAllDocsQuery();
}
/**
* Builds a new WildcardQuery instance
* @param t wildcard term
* @return new WildcardQuery instance
*/
protected Query newWildcardQuery(Term t) {
return new WildcardQuery(t);
}
/**
* Factory method for generating query, given a set of clauses.
* By default creates a boolean query composed of clauses passed in.
@ -677,7 +770,7 @@ public class QueryParser implements QueryParserConstants {
if (clauses.size()==0) {
return null; // all clause words were filtered away by the analyzer.
}
BooleanQuery query = new BooleanQuery(disableCoord);
BooleanQuery query = newBooleanQuery(disableCoord);
for (int i = 0; i < clauses.size(); i++) {
query.add((BooleanClause)clauses.elementAt(i));
}
@ -708,7 +801,7 @@ public class QueryParser implements QueryParserConstants {
protected Query getWildcardQuery(String field, String termStr) throws ParseException
{
if ("*".equals(field)) {
if ("*".equals(termStr)) return new MatchAllDocsQuery();
if ("*".equals(termStr)) return newMatchAllDocsQuery();
}
if (!allowLeadingWildcard && (termStr.startsWith("*") || termStr.startsWith("?")))
throw new ParseException("'*' or '?' not allowed as first character in WildcardQuery");
@ -716,7 +809,7 @@ public class QueryParser implements QueryParserConstants {
termStr = termStr.toLowerCase();
}
Term t = new Term(field, termStr);
return new WildcardQuery(t);
return newWildcardQuery(t);
}
/**
@ -750,7 +843,7 @@ public class QueryParser implements QueryParserConstants {
termStr = termStr.toLowerCase();
}
Term t = new Term(field, termStr);
return new PrefixQuery(t);
return newPrefixQuery(t);
}
@ -771,7 +864,7 @@ public class QueryParser implements QueryParserConstants {
termStr = termStr.toLowerCase();
}
Term t = new Term(field, termStr);
return new FuzzyQuery(t, minSimilarity, fuzzyPrefixLength);
return newFuzzyQuery(t, minSimilarity, fuzzyPrefixLength);
}
/**
@ -1321,6 +1414,12 @@ public class QueryParser implements QueryParserConstants {
finally { jj_save(0, xla); }
}
final private boolean jj_3R_3() {
if (jj_scan_token(STAR)) return true;
if (jj_scan_token(COLON)) return true;
return false;
}
final private boolean jj_3R_2() {
if (jj_scan_token(TERM)) return true;
if (jj_scan_token(COLON)) return true;
@ -1337,12 +1436,6 @@ public class QueryParser implements QueryParserConstants {
return false;
}
final private boolean jj_3R_3() {
if (jj_scan_token(STAR)) return true;
if (jj_scan_token(COLON)) return true;
return false;
}
public QueryParserTokenManager token_source;
public Token token, jj_nt;
private int jj_ntk;

View File

@ -190,7 +190,7 @@ public class QueryParser {
try {
// TopLevelQuery is a Query followed by the end-of-input (EOF)
Query res = TopLevelQuery(field);
return res!=null ? res : new BooleanQuery();
return res!=null ? res : newBooleanQuery(false);
}
catch (ParseException tme) {
// rethrow to include the original query:
@ -477,11 +477,11 @@ public class QueryParser {
required = (!prohibited && conj != CONJ_OR);
}
if (required && !prohibited)
clauses.addElement(new BooleanClause(q, BooleanClause.Occur.MUST));
clauses.addElement(newBooleanClause(q, BooleanClause.Occur.MUST));
else if (!required && !prohibited)
clauses.addElement(new BooleanClause(q, BooleanClause.Occur.SHOULD));
clauses.addElement(newBooleanClause(q, BooleanClause.Occur.SHOULD));
else if (!required && prohibited)
clauses.addElement(new BooleanClause(q, BooleanClause.Occur.MUST_NOT));
clauses.addElement(newBooleanClause(q, BooleanClause.Occur.MUST_NOT));
else
throw new RuntimeException("Clause cannot be both required and prohibited");
}
@ -527,15 +527,15 @@ public class QueryParser {
return null;
else if (v.size() == 1) {
nextToken = (org.apache.lucene.analysis.Token) v.elementAt(0);
return new TermQuery(new Term(field, nextToken.term()));
return newTermQuery(new Term(field, nextToken.term()));
} else {
if (severalTokensAtSamePosition) {
if (positionCount == 1) {
// no phrase query:
BooleanQuery q = new BooleanQuery(true);
BooleanQuery q = newBooleanQuery(true);
for (int i = 0; i < v.size(); i++) {
nextToken = (org.apache.lucene.analysis.Token) v.elementAt(i);
TermQuery currentQuery = new TermQuery(
Query currentQuery = newTermQuery(
new Term(field, nextToken.term()));
q.add(currentQuery, BooleanClause.Occur.SHOULD);
}
@ -543,7 +543,7 @@ public class QueryParser {
}
else {
// phrase query:
MultiPhraseQuery mpq = new MultiPhraseQuery();
MultiPhraseQuery mpq = newMultiPhraseQuery();
mpq.setSlop(phraseSlop);
List multiTerms = new ArrayList();
int position = -1;
@ -569,11 +569,11 @@ public class QueryParser {
}
}
else {
PhraseQuery pq = new PhraseQuery();
PhraseQuery pq = newPhraseQuery();
pq.setSlop(phraseSlop);
int position = -1;
for (int i = 0; i < v.size(); i++) {
t = (org.apache.lucene.analysis.Token) v.elementAt(i);
nextToken = (org.apache.lucene.analysis.Token) v.elementAt(i);
if (enablePositionIncrements) {
position += nextToken.getPositionIncrement();
pq.add(new Term(field, nextToken.term()),position);
@ -652,11 +652,87 @@ public class QueryParser {
}
catch (Exception e) { }
return newRangeQuery(field, part1, part2, inclusive);
}
/**
* Builds a new BooleanQuery instance
* @param disableCoord disable coord
* @return new BooleanQuery instance
*/
protected BooleanQuery newBooleanQuery(boolean disableCoord) {
return new BooleanQuery(disableCoord);
}
/**
* Builds a new BooleanClause instance
* @param q sub query
* @param occur how this clause should occur when matching documents
* @return new BooleanClause instance
*/
protected BooleanClause newBooleanClause(Query q, BooleanClause.Occur occur) {
return new BooleanClause(q, occur);
}
/**
* Builds a new TermQuery instance
* @param term term
* @return new TermQuery instance
*/
protected Query newTermQuery(Term term){
return new TermQuery(term);
}
/**
* Builds a new PhraseQuery instance
* @return new PhraseQuery instance
*/
protected PhraseQuery newPhraseQuery(){
return new PhraseQuery();
}
/**
* Builds a new MultiPhraseQuery instance
* @return new MultiPhraseQuery instance
*/
protected MultiPhraseQuery newMultiPhraseQuery(){
return new MultiPhraseQuery();
}
/**
* Builds a new PrefixQuery instance
* @param prefix Prefix term
* @return new PrefixQuery instance
*/
protected Query newPrefixQuery(Term prefix){
return new PrefixQuery(prefix);
}
/**
* Builds a new FuzzyQuery instance
* @param term Term
* @param minimumSimilarity minimum similarity
* @param prefixLength prefix length
* @return new FuzzyQuery Instance
*/
protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength) {
return new FuzzyQuery(term,minimumSimilarity,prefixLength);
}
/**
* Builds a new RangeQuery instance
* @param field Field
* @param part1 min
* @param part2 max
* @param inclusive true if range is inclusive
* @return new RangeQuery instance
*/
protected Query newRangeQuery(String field, String part1, String part2, boolean inclusive) {
if(useOldRangeQuery)
{
return new RangeQuery(new Term(field, part1),
new Term(field, part2),
inclusive);
return new RangeQuery(new Term(field, part1),
new Term(field, part2),
inclusive);
}
else
{
@ -664,6 +740,23 @@ public class QueryParser {
}
}
/**
* Builds a new MatchAllDocsQuery instance
* @return new MatchAllDocsQuery instance
*/
protected Query newMatchAllDocsQuery() {
return new MatchAllDocsQuery();
}
/**
* Builds a new WildcardQuery instance
* @param t wildcard term
* @return new WildcardQuery instance
*/
protected Query newWildcardQuery(Term t) {
return new WildcardQuery(t);
}
/**
* Factory method for generating query, given a set of clauses.
* By default creates a boolean query composed of clauses passed in.
@ -701,7 +794,7 @@ public class QueryParser {
if (clauses.size()==0) {
return null; // all clause words were filtered away by the analyzer.
}
BooleanQuery query = new BooleanQuery(disableCoord);
BooleanQuery query = newBooleanQuery(disableCoord);
for (int i = 0; i < clauses.size(); i++) {
query.add((BooleanClause)clauses.elementAt(i));
}
@ -732,7 +825,7 @@ public class QueryParser {
protected Query getWildcardQuery(String field, String termStr) throws ParseException
{
if ("*".equals(field)) {
if ("*".equals(termStr)) return new MatchAllDocsQuery();
if ("*".equals(termStr)) return newMatchAllDocsQuery();
}
if (!allowLeadingWildcard && (termStr.startsWith("*") || termStr.startsWith("?")))
throw new ParseException("'*' or '?' not allowed as first character in WildcardQuery");
@ -740,7 +833,7 @@ public class QueryParser {
termStr = termStr.toLowerCase();
}
Term t = new Term(field, termStr);
return new WildcardQuery(t);
return newWildcardQuery(t);
}
/**
@ -774,7 +867,7 @@ public class QueryParser {
termStr = termStr.toLowerCase();
}
Term t = new Term(field, termStr);
return new PrefixQuery(t);
return newPrefixQuery(t);
}
@ -795,7 +888,7 @@ public class QueryParser {
termStr = termStr.toLowerCase();
}
Term t = new Term(field, termStr);
return new FuzzyQuery(t, minSimilarity, fuzzyPrefixLength);
return newFuzzyQuery(t, minSimilarity, fuzzyPrefixLength);
}
/**