mirror of https://github.com/apache/lucene.git
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:
parent
9f805c7d1d
commit
ae45d392f8
|
@ -320,21 +320,29 @@ int Modifiers() : {
|
|||
Query Query(String field) :
|
||||
{
|
||||
Vector clauses = new Vector();
|
||||
Query q;
|
||||
Query q, firstQuery=null;
|
||||
int conj, mods;
|
||||
}
|
||||
{
|
||||
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)
|
||||
{ addClause(clauses, conj, mods, q); }
|
||||
)*
|
||||
{
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
for (int i = 0; i < clauses.size(); i++)
|
||||
query.add((BooleanClause)clauses.elementAt(i));
|
||||
return query;
|
||||
if (clauses.size() == 1 && firstQuery != null)
|
||||
return firstQuery;
|
||||
else {
|
||||
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>
|
||||
)
|
||||
[ <FUZZY> { fuzzy=true; } ]
|
||||
[ <CARAT> boost=<NUMBER> ]
|
||||
[ <CARAT> boost=<NUMBER> [ <FUZZY> { fuzzy=true; } ] ]
|
||||
{
|
||||
if (wildcard)
|
||||
q = new WildcardQuery(new Term(field, term.image));
|
||||
|
@ -409,14 +417,7 @@ Query Term(String field) : {
|
|||
}
|
||||
catch (Exception ignored) { }
|
||||
|
||||
if (q instanceof TermQuery)
|
||||
((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);
|
||||
q.setBoost(f);
|
||||
}
|
||||
return q;
|
||||
}
|
||||
|
|
|
@ -70,7 +70,6 @@ public class MultiTermQuery extends Query {
|
|||
private Term term;
|
||||
private FilteredTermEnum enum;
|
||||
private IndexReader reader;
|
||||
private float boost = 1.0f;
|
||||
private BooleanQuery query;
|
||||
|
||||
/** Enable or disable lucene style toString(field) format */
|
||||
|
@ -87,18 +86,6 @@ public class MultiTermQuery extends Query {
|
|||
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 {
|
||||
return getQuery().sumOfSquaredWeights(searcher);
|
||||
}
|
||||
|
|
|
@ -71,7 +71,6 @@ final public class PhraseQuery extends Query {
|
|||
private float idf = 0.0f;
|
||||
private float weight = 0.0f;
|
||||
|
||||
private float boost = 1.0f;
|
||||
private int slop = 0;
|
||||
|
||||
|
||||
|
@ -79,15 +78,6 @@ final public class PhraseQuery extends Query {
|
|||
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.
|
||||
If zero, then this is an exact phrase search. For larger values this works
|
||||
like a <code>WITHIN</code> or <code>NEAR</code> operator.
|
||||
|
|
|
@ -64,7 +64,6 @@ import org.apache.lucene.index.IndexReader;
|
|||
final public class PrefixQuery extends Query {
|
||||
private Term prefix;
|
||||
private IndexReader reader;
|
||||
private float boost = 1.0f;
|
||||
private BooleanQuery query;
|
||||
|
||||
/** Constructs a query for terms starting with <code>prefix</code>. */
|
||||
|
@ -73,18 +72,6 @@ final public class PrefixQuery extends Query {
|
|||
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) {
|
||||
this.query = null;
|
||||
this.reader = reader;
|
||||
|
|
|
@ -73,6 +73,9 @@ import org.apache.lucene.index.IndexReader;
|
|||
*/
|
||||
abstract public class Query {
|
||||
|
||||
// query boost factor
|
||||
protected float boost = 1.0f;
|
||||
|
||||
// query weighting
|
||||
abstract float sumOfSquaredWeights(Searcher searcher) throws IOException;
|
||||
abstract void normalize(float norm);
|
||||
|
@ -91,6 +94,16 @@ abstract public class Query {
|
|||
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
|
||||
for terms.
|
||||
<p>The representation used is one that is readable by
|
||||
|
|
|
@ -67,7 +67,6 @@ public final class RangeQuery extends Query
|
|||
private Term upperTerm;
|
||||
private boolean inclusive;
|
||||
private IndexReader reader;
|
||||
private float boost = 1.0f;
|
||||
private BooleanQuery query;
|
||||
|
||||
/** Constructs a query selecting all terms greater than
|
||||
|
@ -91,20 +90,6 @@ public final class RangeQuery extends Query
|
|||
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)
|
||||
{
|
||||
this.query = null;
|
||||
|
|
|
@ -64,7 +64,6 @@ import org.apache.lucene.index.IndexReader;
|
|||
*/
|
||||
final public class TermQuery extends Query {
|
||||
private Term term;
|
||||
private float boost = 1.0f;
|
||||
private float idf = 0.0f;
|
||||
private float weight = 0.0f;
|
||||
|
||||
|
@ -73,15 +72,6 @@ final public class TermQuery extends Query {
|
|||
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 {
|
||||
idf = Similarity.idf(term, searcher);
|
||||
weight = idf * boost;
|
||||
|
|
|
@ -114,17 +114,20 @@ public class TestQueryParser extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void assertQueryEquals(String query, Analyzer a, String result)
|
||||
throws Exception {
|
||||
public Query getQuery(String query, Analyzer a) throws Exception {
|
||||
if (a == null)
|
||||
a = new SimpleAnalyzer();
|
||||
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");
|
||||
if (!s.equals(result)) {
|
||||
System.err.println("Query /" + query + "/ yielded /" + s
|
||||
+ "/, expecting /" + result + "/");
|
||||
assert(false);
|
||||
fail("Query /" + query + "/ yielded /" + s
|
||||
+ "/, expecting /" + result + "/");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -136,6 +139,8 @@ public class TestQueryParser extends TestCase {
|
|||
assertQueryEquals("term 1.0 1 2", null, "term");
|
||||
|
||||
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 -b", null, "+a -b");
|
||||
assertQueryEquals("a AND !b", null, "+a -b");
|
||||
|
@ -154,6 +159,10 @@ public class TestQueryParser extends TestCase {
|
|||
"+foo:term +anotherterm");
|
||||
assertQueryEquals("term AND \"phrase phrase\"", null,
|
||||
"+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("term^2.0", null, "term^2.0");
|
||||
|
@ -170,6 +179,21 @@ public class TestQueryParser extends TestCase {
|
|||
"+(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 {
|
||||
assertQueryEquals("term term term", qpAnalyzer, "term 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,
|
||||
"+term -\"phrase1 phrase2\" term");
|
||||
assertQueryEquals("stop", qpAnalyzer, "");
|
||||
assert(getQuery("term term term", qpAnalyzer) instanceof BooleanQuery);
|
||||
assert(getQuery("term +stop", qpAnalyzer) instanceof TermQuery);
|
||||
}
|
||||
|
||||
public void testRange() throws Exception {
|
||||
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 }^2.0", null, "{a-z}^2.0");
|
||||
assertQueryEquals("[ a z] OR 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})");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue