Remove deprecations from QueryParser and generify (LUCENE-1257) QueryParser

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@827717 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2009-10-20 16:06:29 +00:00
parent 5ceb81834d
commit 649a0e83a4
6 changed files with 41 additions and 188 deletions

View File

@ -200,10 +200,10 @@
Then, create a build.properties file either in your home
directory, or within the Lucene directory and set the javacc.home
property to the path where JavaCC is installed. For example,
if you installed JavaCC in /usr/local/java/javacc-3.2, then set the
if you installed JavaCC in /usr/local/java/javacc-4.1, then set the
javacc.home property to:
javacc.home=/usr/local/java/javacc-3.2
javacc.home=/usr/local/java/javacc-4.1
If you get an error like the one below, then you have not installed
things correctly. Please check all your paths and try again.

View File

@ -93,7 +93,6 @@ public class CollationTestBase extends TestCase {
// supported).
// Test TermRangeQuery
aqp.setUseOldRangeQuery(false);
ScoreDoc[] result
= is.search(aqp.parse("[ \u062F TO \u0698 ]"), null, 1000).scoreDocs;
assertEquals("The index Term should not be included.", 0, result.length);
@ -101,14 +100,6 @@ public class CollationTestBase extends TestCase {
result = is.search(aqp.parse("[ \u0633 TO \u0638 ]"), null, 1000).scoreDocs;
assertEquals("The index Term should be included.", 1, result.length);
// Test TermRangeQuery
aqp.setUseOldRangeQuery(true);
result = is.search(aqp.parse("[ \u062F TO \u0698 ]"), null, 1000).scoreDocs;
assertEquals("The index Term should not be included.", 0, result.length);
result = is.search(aqp.parse("[ \u0633 TO \u0638 ]"), null, 1000).scoreDocs;
assertEquals("The index Term should be included.", 1, result.length);
is.close();
}

View File

@ -543,7 +543,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
// rather
// than RangeFilters
QueryParser parser = new QueryParser(FIELD_NAME, new StandardAnalyzer());
parser.setUseOldRangeQuery(true);
parser.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
query = parser.parse(queryString);
doSearching(query);

View File

@ -36,7 +36,7 @@ import org.apache.lucene.search.Query;
public class MultiFieldQueryParser extends QueryParser
{
protected String[] fields;
protected Map boosts;
protected Map<String,Float> boosts;
/**
* Creates a MultiFieldQueryParser.
@ -65,7 +65,7 @@ public class MultiFieldQueryParser extends QueryParser
* <p>In other words, all the query's terms must appear, but it doesn't matter in
* what fields they appear.</p>
*/
public MultiFieldQueryParser(String[] fields, Analyzer analyzer, Map boosts) {
public MultiFieldQueryParser(String[] fields, Analyzer analyzer, Map<String,Float> boosts) {
this(fields,analyzer);
this.boosts = boosts;
}
@ -97,14 +97,14 @@ public class MultiFieldQueryParser extends QueryParser
protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException {
if (field == null) {
List clauses = new ArrayList();
List<BooleanClause> clauses = new ArrayList<BooleanClause>();
for (int i = 0; i < fields.length; i++) {
Query q = super.getFieldQuery(fields[i], queryText);
if (q != null) {
//If the user passes a map of boosts
if (boosts != null) {
//Get the boost from the map and apply them
Float boost = (Float)boosts.get(fields[i]);
Float boost = boosts.get(fields[i]);
if (boost != null) {
q.setBoost(boost.floatValue());
}
@ -139,7 +139,7 @@ public class MultiFieldQueryParser extends QueryParser
protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException
{
if (field == null) {
List clauses = new ArrayList();
List<BooleanClause> clauses = new ArrayList<BooleanClause>();
for (int i = 0; i < fields.length; i++) {
clauses.add(new BooleanClause(getFuzzyQuery(fields[i], termStr, minSimilarity),
BooleanClause.Occur.SHOULD));
@ -152,7 +152,7 @@ public class MultiFieldQueryParser extends QueryParser
protected Query getPrefixQuery(String field, String termStr) throws ParseException
{
if (field == null) {
List clauses = new ArrayList();
List<BooleanClause> clauses = new ArrayList<BooleanClause>();
for (int i = 0; i < fields.length; i++) {
clauses.add(new BooleanClause(getPrefixQuery(fields[i], termStr),
BooleanClause.Occur.SHOULD));
@ -164,7 +164,7 @@ public class MultiFieldQueryParser extends QueryParser
protected Query getWildcardQuery(String field, String termStr) throws ParseException {
if (field == null) {
List clauses = new ArrayList();
List<BooleanClause> clauses = new ArrayList<BooleanClause>();
for (int i = 0; i < fields.length; i++) {
clauses.add(new BooleanClause(getWildcardQuery(fields[i], termStr),
BooleanClause.Occur.SHOULD));
@ -177,7 +177,7 @@ public class MultiFieldQueryParser extends QueryParser
protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException {
if (field == null) {
List clauses = new ArrayList();
List<BooleanClause> clauses = new ArrayList<BooleanClause>();
for (int i = 0; i < fields.length; i++) {
clauses.add(new BooleanClause(getRangeQuery(fields[i], part1, part2, inclusive),
BooleanClause.Occur.SHOULD));

View File

@ -137,7 +137,7 @@ public class QueryParser implements QueryParserConstants {
// the default date resolution
DateTools.Resolution dateResolution = null;
// maps field names to date resolutions
Map fieldToDateResolution = null;
Map<String,DateTools.Resolution> fieldToDateResolution = null;
// The collator to use when determining range inclusion,
// for use when constructing RangeQuerys.
@ -335,29 +335,6 @@ public class QueryParser implements QueryParserConstants {
return lowercaseExpandedTerms;
}
/**
* @deprecated Please use {@link #setMultiTermRewriteMethod} instead.
*/
public void setUseOldRangeQuery(boolean useOldRangeQuery) {
if (useOldRangeQuery) {
setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
} else {
setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
}
}
/**
* @deprecated Please use {@link #getMultiTermRewriteMethod} instead.
*/
public boolean getUseOldRangeQuery() {
if (getMultiTermRewriteMethod() == MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE) {
return true;
} else {
return false;
}
}
/**
* By default QueryParser uses {@link MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}
* when creating a PrefixQuery, WildcardQuery or RangeQuery. This implementation is generally preferable because it
@ -418,7 +395,7 @@ public class QueryParser implements QueryParserConstants {
if (fieldToDateResolution == null) {
// lazily initialize HashMap
fieldToDateResolution = new HashMap();
fieldToDateResolution = new HashMap<String,DateTools.Resolution>();
}
fieldToDateResolution.put(fieldName, dateResolution);
@ -473,20 +450,13 @@ public class QueryParser implements QueryParserConstants {
return rangeCollator;
}
/**
* @deprecated use {@link #addClause(List, int, int, Query)} instead.
*/
protected void addClause(Vector clauses, int conj, int mods, Query q) {
addClause((List) clauses, conj, mods, q);
}
protected void addClause(List clauses, int conj, int mods, Query q) {
protected void addClause(List<BooleanClause> clauses, int conj, int mods, Query q) {
boolean required, prohibited;
// If this term is introduced by AND, make the preceding term required,
// unless it's already prohibited
if (clauses.size() > 0 && conj == CONJ_AND) {
BooleanClause c = (BooleanClause) clauses.get(clauses.size()-1);
BooleanClause c = clauses.get(clauses.size()-1);
if (!c.isProhibited())
c.setOccur(BooleanClause.Occur.MUST);
}
@ -496,7 +466,7 @@ public class QueryParser implements QueryParserConstants {
// unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)
// notice if the input is a OR b, first term is parsed as required; without
// this modification a OR b would parsed as +a OR b
BooleanClause c = (BooleanClause) clauses.get(clauses.size()-1);
BooleanClause c = clauses.get(clauses.size()-1);
if (!c.isProhibited())
c.setOccur(BooleanClause.Occur.SHOULD);
}
@ -635,7 +605,7 @@ public class QueryParser implements QueryParserConstants {
// phrase query:
MultiPhraseQuery mpq = newMultiPhraseQuery();
mpq.setSlop(phraseSlop);
List multiTerms = new ArrayList();
List<Term> multiTerms = new ArrayList<Term>();
int position = -1;
for (int i = 0; i < numTokens; i++) {
String term = null;
@ -653,9 +623,9 @@ public class QueryParser implements QueryParserConstants {
if (positionIncrement > 0 && multiTerms.size() > 0) {
if (enablePositionIncrements) {
mpq.add((Term[])multiTerms.toArray(new Term[0]),position);
mpq.add(multiTerms.toArray(new Term[0]),position);
} else {
mpq.add((Term[])multiTerms.toArray(new Term[0]));
mpq.add(multiTerms.toArray(new Term[0]));
}
multiTerms.clear();
}
@ -663,9 +633,9 @@ public class QueryParser implements QueryParserConstants {
multiTerms.add(new Term(field, term));
}
if (enablePositionIncrements) {
mpq.add((Term[])multiTerms.toArray(new Term[0]),position);
mpq.add(multiTerms.toArray(new Term[0]),position);
} else {
mpq.add((Term[])multiTerms.toArray(new Term[0]));
mpq.add(multiTerms.toArray(new Term[0]));
}
return mpq;
}
@ -885,26 +855,8 @@ public class QueryParser implements QueryParserConstants {
*
* @return Resulting {@link Query} object.
* @exception ParseException throw in overridden method to disallow
* @deprecated use {@link #getBooleanQuery(List)} instead
*/
protected Query getBooleanQuery(Vector clauses) throws ParseException {
return getBooleanQuery((List) clauses, false);
}
/**
* Factory method for generating query, given a set of clauses.
* By default creates a boolean query composed of clauses passed in.
*
* Can be overridden by extending classes, to modify query being
* returned.
*
* @param clauses List that contains {@link BooleanClause} instances
* to join.
*
* @return Resulting {@link Query} object.
* @exception ParseException throw in overridden method to disallow
*/
protected Query getBooleanQuery(List clauses) throws ParseException {
protected Query getBooleanQuery(List<BooleanClause> clauses) throws ParseException {
return getBooleanQuery(clauses, false);
}
@ -921,37 +873,16 @@ public class QueryParser implements QueryParserConstants {
*
* @return Resulting {@link Query} object.
* @exception ParseException throw in overridden method to disallow
* @deprecated use {@link #getBooleanQuery(List, boolean)} instead
*/
protected Query getBooleanQuery(Vector clauses, boolean disableCoord)
throws ParseException
{
return getBooleanQuery((List) clauses, disableCoord);
}
/**
* Factory method for generating query, given a set of clauses.
* By default creates a boolean query composed of clauses passed in.
*
* Can be overridden by extending classes, to modify query being
* returned.
*
* @param clauses List that contains {@link BooleanClause} instances
* to join.
* @param disableCoord true if coord scoring should be disabled.
*
* @return Resulting {@link Query} object.
* @exception ParseException throw in overridden method to disallow
*/
protected Query getBooleanQuery(List clauses, boolean disableCoord)
protected Query getBooleanQuery(List<BooleanClause> clauses, boolean disableCoord)
throws ParseException
{
if (clauses.size()==0) {
return null; // all clause words were filtered away by the analyzer.
}
BooleanQuery query = newBooleanQuery(disableCoord);
for (int i = 0; i < clauses.size(); i++) {
query.add((BooleanClause)clauses.get(i));
for(final BooleanClause clause: clauses) {
query.add(clause);
}
return query;
}
@ -1234,7 +1165,7 @@ public class QueryParser implements QueryParserConstants {
}
final public Query Query(String field) throws ParseException {
List clauses = new ArrayList();
List<BooleanClause> clauses = new ArrayList<BooleanClause>();
Query q, firstQuery=null;
int conj, mods;
mods = Modifiers();

View File

@ -161,7 +161,7 @@ public class QueryParser {
// the default date resolution
DateTools.Resolution dateResolution = null;
// maps field names to date resolutions
Map fieldToDateResolution = null;
Map<String,DateTools.Resolution> fieldToDateResolution = null;
// The collator to use when determining range inclusion,
// for use when constructing RangeQuerys.
@ -359,29 +359,6 @@ public class QueryParser {
return lowercaseExpandedTerms;
}
/**
* @deprecated Please use {@link #setMultiTermRewriteMethod} instead.
*/
public void setUseOldRangeQuery(boolean useOldRangeQuery) {
if (useOldRangeQuery) {
setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
} else {
setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
}
}
/**
* @deprecated Please use {@link #getMultiTermRewriteMethod} instead.
*/
public boolean getUseOldRangeQuery() {
if (getMultiTermRewriteMethod() == MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE) {
return true;
} else {
return false;
}
}
/**
* By default QueryParser uses {@link MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}
* when creating a PrefixQuery, WildcardQuery or RangeQuery. This implementation is generally preferable because it
@ -442,7 +419,7 @@ public class QueryParser {
if (fieldToDateResolution == null) {
// lazily initialize HashMap
fieldToDateResolution = new HashMap();
fieldToDateResolution = new HashMap<String,DateTools.Resolution>();
}
fieldToDateResolution.put(fieldName, dateResolution);
@ -497,20 +474,13 @@ public class QueryParser {
return rangeCollator;
}
/**
* @deprecated use {@link #addClause(List, int, int, Query)} instead.
*/
protected void addClause(Vector clauses, int conj, int mods, Query q) {
addClause((List) clauses, conj, mods, q);
}
protected void addClause(List clauses, int conj, int mods, Query q) {
protected void addClause(List<BooleanClause> clauses, int conj, int mods, Query q) {
boolean required, prohibited;
// If this term is introduced by AND, make the preceding term required,
// unless it's already prohibited
if (clauses.size() > 0 && conj == CONJ_AND) {
BooleanClause c = (BooleanClause) clauses.get(clauses.size()-1);
BooleanClause c = clauses.get(clauses.size()-1);
if (!c.isProhibited())
c.setOccur(BooleanClause.Occur.MUST);
}
@ -520,7 +490,7 @@ public class QueryParser {
// unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)
// notice if the input is a OR b, first term is parsed as required; without
// this modification a OR b would parsed as +a OR b
BooleanClause c = (BooleanClause) clauses.get(clauses.size()-1);
BooleanClause c = clauses.get(clauses.size()-1);
if (!c.isProhibited())
c.setOccur(BooleanClause.Occur.SHOULD);
}
@ -659,7 +629,7 @@ public class QueryParser {
// phrase query:
MultiPhraseQuery mpq = newMultiPhraseQuery();
mpq.setSlop(phraseSlop);
List multiTerms = new ArrayList();
List<Term> multiTerms = new ArrayList<Term>();
int position = -1;
for (int i = 0; i < numTokens; i++) {
String term = null;
@ -677,9 +647,9 @@ public class QueryParser {
if (positionIncrement > 0 && multiTerms.size() > 0) {
if (enablePositionIncrements) {
mpq.add((Term[])multiTerms.toArray(new Term[0]),position);
mpq.add(multiTerms.toArray(new Term[0]),position);
} else {
mpq.add((Term[])multiTerms.toArray(new Term[0]));
mpq.add(multiTerms.toArray(new Term[0]));
}
multiTerms.clear();
}
@ -687,9 +657,9 @@ public class QueryParser {
multiTerms.add(new Term(field, term));
}
if (enablePositionIncrements) {
mpq.add((Term[])multiTerms.toArray(new Term[0]),position);
mpq.add(multiTerms.toArray(new Term[0]),position);
} else {
mpq.add((Term[])multiTerms.toArray(new Term[0]));
mpq.add(multiTerms.toArray(new Term[0]));
}
return mpq;
}
@ -909,26 +879,8 @@ public class QueryParser {
*
* @return Resulting {@link Query} object.
* @exception ParseException throw in overridden method to disallow
* @deprecated use {@link #getBooleanQuery(List)} instead
*/
protected Query getBooleanQuery(Vector clauses) throws ParseException {
return getBooleanQuery((List) clauses, false);
}
/**
* Factory method for generating query, given a set of clauses.
* By default creates a boolean query composed of clauses passed in.
*
* Can be overridden by extending classes, to modify query being
* returned.
*
* @param clauses List that contains {@link BooleanClause} instances
* to join.
*
* @return Resulting {@link Query} object.
* @exception ParseException throw in overridden method to disallow
*/
protected Query getBooleanQuery(List clauses) throws ParseException {
protected Query getBooleanQuery(List<BooleanClause> clauses) throws ParseException {
return getBooleanQuery(clauses, false);
}
@ -945,37 +897,16 @@ public class QueryParser {
*
* @return Resulting {@link Query} object.
* @exception ParseException throw in overridden method to disallow
* @deprecated use {@link #getBooleanQuery(List, boolean)} instead
*/
protected Query getBooleanQuery(Vector clauses, boolean disableCoord)
throws ParseException
{
return getBooleanQuery((List) clauses, disableCoord);
}
/**
* Factory method for generating query, given a set of clauses.
* By default creates a boolean query composed of clauses passed in.
*
* Can be overridden by extending classes, to modify query being
* returned.
*
* @param clauses List that contains {@link BooleanClause} instances
* to join.
* @param disableCoord true if coord scoring should be disabled.
*
* @return Resulting {@link Query} object.
* @exception ParseException throw in overridden method to disallow
*/
protected Query getBooleanQuery(List clauses, boolean disableCoord)
protected Query getBooleanQuery(List<BooleanClause> clauses, boolean disableCoord)
throws ParseException
{
if (clauses.size()==0) {
return null; // all clause words were filtered away by the analyzer.
}
BooleanQuery query = newBooleanQuery(disableCoord);
for (int i = 0; i < clauses.size(); i++) {
query.add((BooleanClause)clauses.get(i));
for(final BooleanClause clause: clauses) {
query.add(clause);
}
return query;
}
@ -1286,7 +1217,7 @@ Query TopLevelQuery(String field) :
Query Query(String field) :
{
List clauses = new ArrayList();
List<BooleanClause> clauses = new ArrayList<BooleanClause>();
Query q, firstQuery=null;
int conj, mods;
}