Migrate setBoost/getBoost into base QUery class; don't wrap queries with BooleanQuery in query parser unless necessary; more test cases for query parser

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@149640 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brian Goetz 2002-01-17 02:00:09 +00:00
parent 9f805c7d1d
commit ae45d392f8
8 changed files with 64 additions and 83 deletions

View File

@ -320,21 +320,29 @@ int Modifiers() : {
Query Query(String field) : Query Query(String field) :
{ {
Vector clauses = new Vector(); Vector clauses = new Vector();
Query q; Query q, firstQuery=null;
int conj, mods; int conj, mods;
} }
{ {
mods=Modifiers() q=Clause(field) mods=Modifiers() q=Clause(field)
{ addClause(clauses, CONJ_NONE, mods, q); } {
addClause(clauses, CONJ_NONE, mods, q);
if (mods == MOD_NONE)
firstQuery=q;
}
( (
conj=Conjunction() mods=Modifiers() q=Clause(field) conj=Conjunction() mods=Modifiers() q=Clause(field)
{ addClause(clauses, conj, mods, q); } { addClause(clauses, conj, mods, q); }
)* )*
{ {
BooleanQuery query = new BooleanQuery(); if (clauses.size() == 1 && firstQuery != null)
for (int i = 0; i < clauses.size(); i++) return firstQuery;
query.add((BooleanClause)clauses.elementAt(i)); else {
return query; BooleanQuery query = new BooleanQuery();
for (int i = 0; i < clauses.size(); i++)
query.add((BooleanClause)clauses.elementAt(i));
return query;
}
} }
} }
@ -375,7 +383,7 @@ Query Term(String field) : {
| term=<NUMBER> | term=<NUMBER>
) )
[ <FUZZY> { fuzzy=true; } ] [ <FUZZY> { fuzzy=true; } ]
[ <CARAT> boost=<NUMBER> ] [ <CARAT> boost=<NUMBER> [ <FUZZY> { fuzzy=true; } ] ]
{ {
if (wildcard) if (wildcard)
q = new WildcardQuery(new Term(field, term.image)); q = new WildcardQuery(new Term(field, term.image));
@ -409,14 +417,7 @@ Query Term(String field) : {
} }
catch (Exception ignored) { } catch (Exception ignored) { }
if (q instanceof TermQuery) q.setBoost(f);
((TermQuery) q).setBoost(f);
else if (q instanceof PhraseQuery)
((PhraseQuery) q).setBoost(f);
else if (q instanceof MultiTermQuery)
((MultiTermQuery) q).setBoost(f);
else if (q instanceof RangeQuery)
((RangeQuery) q).setBoost(f);
} }
return q; return q;
} }

View File

@ -70,7 +70,6 @@ public class MultiTermQuery extends Query {
private Term term; private Term term;
private FilteredTermEnum enum; private FilteredTermEnum enum;
private IndexReader reader; private IndexReader reader;
private float boost = 1.0f;
private BooleanQuery query; private BooleanQuery query;
/** Enable or disable lucene style toString(field) format */ /** Enable or disable lucene style toString(field) format */
@ -87,18 +86,6 @@ public class MultiTermQuery extends Query {
this.enum = enum; this.enum = enum;
} }
/** Sets the boost for this term to <code>b</code>. Documents containing
* this term will (in addition to the normal weightings) have their score
* multiplied by <code>boost</code>. */
final public void setBoost(float boost) {
this.boost = boost;
}
/** Returns the boost for this term. */
final public float getBoost() {
return boost;
}
final float sumOfSquaredWeights(Searcher searcher) throws IOException { final float sumOfSquaredWeights(Searcher searcher) throws IOException {
return getQuery().sumOfSquaredWeights(searcher); return getQuery().sumOfSquaredWeights(searcher);
} }

View File

@ -71,7 +71,6 @@ final public class PhraseQuery extends Query {
private float idf = 0.0f; private float idf = 0.0f;
private float weight = 0.0f; private float weight = 0.0f;
private float boost = 1.0f;
private int slop = 0; private int slop = 0;
@ -79,15 +78,6 @@ final public class PhraseQuery extends Query {
public PhraseQuery() { public PhraseQuery() {
} }
/** Sets the boost for this term to <code>b</code>. Documents containing
this term will (in addition to the normal weightings) have their score
multiplied by <code>b</code>. */
public final void setBoost(float b) { boost = b; }
/** Gets the boost for this term. Documents containing
this term will (in addition to the normal weightings) have their score
multiplied by <code>b</code>. The boost is 1.0 by default. */
public final float getBoost() { return boost; }
/** Sets the number of other words permitted between words in query phrase. /** Sets the number of other words permitted between words in query phrase.
If zero, then this is an exact phrase search. For larger values this works If zero, then this is an exact phrase search. For larger values this works
like a <code>WITHIN</code> or <code>NEAR</code> operator. like a <code>WITHIN</code> or <code>NEAR</code> operator.

View File

@ -64,7 +64,6 @@ import org.apache.lucene.index.IndexReader;
final public class PrefixQuery extends Query { final public class PrefixQuery extends Query {
private Term prefix; private Term prefix;
private IndexReader reader; private IndexReader reader;
private float boost = 1.0f;
private BooleanQuery query; private BooleanQuery query;
/** Constructs a query for terms starting with <code>prefix</code>. */ /** Constructs a query for terms starting with <code>prefix</code>. */
@ -73,18 +72,6 @@ final public class PrefixQuery extends Query {
this.reader = reader; this.reader = reader;
} }
/** Sets the boost for this term to <code>b</code>. Documents containing
this term will (in addition to the normal weightings) have their score
multiplied by <code>boost</code>. */
public void setBoost(float boost) {
this.boost = boost;
}
/** Returns the boost for this term. */
public float getBoost() {
return boost;
}
final void prepare(IndexReader reader) { final void prepare(IndexReader reader) {
this.query = null; this.query = null;
this.reader = reader; this.reader = reader;

View File

@ -73,6 +73,9 @@ import org.apache.lucene.index.IndexReader;
*/ */
abstract public class Query { abstract public class Query {
// query boost factor
protected float boost = 1.0f;
// query weighting // query weighting
abstract float sumOfSquaredWeights(Searcher searcher) throws IOException; abstract float sumOfSquaredWeights(Searcher searcher) throws IOException;
abstract void normalize(float norm); abstract void normalize(float norm);
@ -91,6 +94,16 @@ abstract public class Query {
return query.scorer(reader); return query.scorer(reader);
} }
/** Sets the boost for this term to <code>b</code>. Documents containing
this term will (in addition to the normal weightings) have their score
multiplied by <code>b</code>. */
public void setBoost(float b) { boost = b; }
/** Gets the boost for this term. Documents containing
this term will (in addition to the normal weightings) have their score
multiplied by <code>b</code>. The boost is 1.0 by default. */
public float getBoost() { return boost; }
/** Prints a query to a string, with <code>field</code> as the default field /** Prints a query to a string, with <code>field</code> as the default field
for terms. for terms.
<p>The representation used is one that is readable by <p>The representation used is one that is readable by

View File

@ -67,7 +67,6 @@ public final class RangeQuery extends Query
private Term upperTerm; private Term upperTerm;
private boolean inclusive; private boolean inclusive;
private IndexReader reader; private IndexReader reader;
private float boost = 1.0f;
private BooleanQuery query; private BooleanQuery query;
/** Constructs a query selecting all terms greater than /** Constructs a query selecting all terms greater than
@ -91,20 +90,6 @@ public final class RangeQuery extends Query
this.inclusive = inclusive; this.inclusive = inclusive;
} }
/** Sets the boost for this term to <code>b</code>. Documents containing
this term will (in addition to the normal weightings) have their score
multiplied by <code>boost</code>. */
public void setBoost(float boost)
{
this.boost = boost;
}
/** Returns the boost for this term. */
public float getBoost()
{
return boost;
}
final void prepare(IndexReader reader) final void prepare(IndexReader reader)
{ {
this.query = null; this.query = null;

View File

@ -64,7 +64,6 @@ import org.apache.lucene.index.IndexReader;
*/ */
final public class TermQuery extends Query { final public class TermQuery extends Query {
private Term term; private Term term;
private float boost = 1.0f;
private float idf = 0.0f; private float idf = 0.0f;
private float weight = 0.0f; private float weight = 0.0f;
@ -73,15 +72,6 @@ final public class TermQuery extends Query {
term = t; term = t;
} }
/** Sets the boost for this term to <code>b</code>. Documents containing
this term will (in addition to the normal weightings) have their score
multiplied by <code>b</code>. */
public void setBoost(float b) { boost = b; }
/** Gets the boost for this term. Documents containing
this term will (in addition to the normal weightings) have their score
multiplied by <code>b</code>. The boost is 1.0 by default. */
public float getBoost() { return boost; }
final float sumOfSquaredWeights(Searcher searcher) throws IOException { final float sumOfSquaredWeights(Searcher searcher) throws IOException {
idf = Similarity.idf(term, searcher); idf = Similarity.idf(term, searcher);
weight = idf * boost; weight = idf * boost;

View File

@ -114,17 +114,20 @@ public class TestQueryParser extends TestCase {
} }
} }
public void assertQueryEquals(String query, Analyzer a, String result) public Query getQuery(String query, Analyzer a) throws Exception {
throws Exception {
if (a == null) if (a == null)
a = new SimpleAnalyzer(); a = new SimpleAnalyzer();
QueryParser qp = new QueryParser("field", a); QueryParser qp = new QueryParser("field", a);
Query q = qp.parse(query); return qp.parse(query);
}
public void assertQueryEquals(String query, Analyzer a, String result)
throws Exception {
Query q = getQuery(query, a);
String s = q.toString("field"); String s = q.toString("field");
if (!s.equals(result)) { if (!s.equals(result)) {
System.err.println("Query /" + query + "/ yielded /" + s fail("Query /" + query + "/ yielded /" + s
+ "/, expecting /" + result + "/"); + "/, expecting /" + result + "/");
assert(false);
} }
} }
@ -136,6 +139,8 @@ public class TestQueryParser extends TestCase {
assertQueryEquals("term 1.0 1 2", null, "term"); assertQueryEquals("term 1.0 1 2", null, "term");
assertQueryEquals("a AND b", null, "+a +b"); assertQueryEquals("a AND b", null, "+a +b");
assertQueryEquals("(a AND b)", null, "+a +b");
assertQueryEquals("c OR (a AND b)", null, "c (+a +b)");
assertQueryEquals("a AND NOT b", null, "+a -b"); assertQueryEquals("a AND NOT b", null, "+a -b");
assertQueryEquals("a AND -b", null, "+a -b"); assertQueryEquals("a AND -b", null, "+a -b");
assertQueryEquals("a AND !b", null, "+a -b"); assertQueryEquals("a AND !b", null, "+a -b");
@ -154,6 +159,10 @@ public class TestQueryParser extends TestCase {
"+foo:term +anotherterm"); "+foo:term +anotherterm");
assertQueryEquals("term AND \"phrase phrase\"", null, assertQueryEquals("term AND \"phrase phrase\"", null,
"+term +\"phrase phrase\""); "+term +\"phrase phrase\"");
assertQueryEquals("\"hello there\"", null, "\"hello there\"");
assert(getQuery("a AND b", null) instanceof BooleanQuery);
assert(getQuery("hello", null) instanceof TermQuery);
assert(getQuery("\"hello there\"", null) instanceof PhraseQuery);
assertQueryEquals("germ term^2.0", null, "germ term^2.0"); assertQueryEquals("germ term^2.0", null, "germ term^2.0");
assertQueryEquals("term^2.0", null, "term^2.0"); assertQueryEquals("term^2.0", null, "term^2.0");
@ -170,6 +179,21 @@ public class TestQueryParser extends TestCase {
"+(title:dog title:cat) -author:\"bob dole\""); "+(title:dog title:cat) -author:\"bob dole\"");
} }
public void testWildcard() throws Exception {
assertQueryEquals("term*", null, "term*");
assertQueryEquals("term*^2", null, "term*^2.0");
assertQueryEquals("term~", null, "term~");
assertQueryEquals("term~^2", null, "term^2.0~");
assertQueryEquals("term^2~", null, "term^2.0~");
assertQueryEquals("term*germ", null, "term*germ");
assertQueryEquals("term*germ^3", null, "term*germ^3.0");
assert(getQuery("term*", null) instanceof PrefixQuery);
assert(getQuery("term*^2", null) instanceof PrefixQuery);
assert(getQuery("term~", null) instanceof FuzzyQuery);
assert(getQuery("term*germ", null) instanceof WildcardQuery);
}
public void testQPA() throws Exception { public void testQPA() throws Exception {
assertQueryEquals("term term term", qpAnalyzer, "term term term"); assertQueryEquals("term term term", qpAnalyzer, "term term term");
assertQueryEquals("term +stop term", qpAnalyzer, "term term"); assertQueryEquals("term +stop term", qpAnalyzer, "term term");
@ -180,17 +204,21 @@ public class TestQueryParser extends TestCase {
assertQueryEquals("term AND NOT phrase term", qpAnalyzer, assertQueryEquals("term AND NOT phrase term", qpAnalyzer,
"+term -\"phrase1 phrase2\" term"); "+term -\"phrase1 phrase2\" term");
assertQueryEquals("stop", qpAnalyzer, ""); assertQueryEquals("stop", qpAnalyzer, "");
assert(getQuery("term term term", qpAnalyzer) instanceof BooleanQuery);
assert(getQuery("term +stop", qpAnalyzer) instanceof TermQuery);
} }
public void testRange() throws Exception { public void testRange() throws Exception {
assertQueryEquals("[ a z]", null, "[a-z]"); assertQueryEquals("[ a z]", null, "[a-z]");
assert(getQuery("[ a z]", null) instanceof RangeQuery);
assertQueryEquals("[ a z ]", null, "[a-z]"); assertQueryEquals("[ a z ]", null, "[a-z]");
assertQueryEquals("{ a z}", null, "{a-z}"); assertQueryEquals("{ a z}", null, "{a-z}");
assertQueryEquals("{ a z }", null, "{a-z}"); assertQueryEquals("{ a z }", null, "{a-z}");
assertQueryEquals("{ a z }^2.0", null, "{a-z}^2.0"); assertQueryEquals("{ a z }^2.0", null, "{a-z}^2.0");
assertQueryEquals("[ a z] OR bar", null, "[a-z] bar"); assertQueryEquals("[ a z] OR bar", null, "[a-z] bar");
assertQueryEquals("[ a z] AND bar", null, "+[a-z] +bar"); assertQueryEquals("[ a z] AND bar", null, "+[a-z] +bar");
assertQueryEquals("( bar blar { a z}) ", null, "(bar blar {a-z})"); assertQueryEquals("( bar blar { a z}) ", null, "bar blar {a-z}");
assertQueryEquals("gack ( bar blar { a z}) ", null, "gack (bar blar {a-z})");
} }
} }