Apache Lucene open-source search software
Migrate setBoost/getBoost into base QUery class; don't wrap queries with BooleanQuery in query parser unless necessary; more test cases for query parser
Brian Goetzae45d392f8d3
diff --git a/src/java/org/apache/lucene/queryParser/QueryParser.jj b/src/java/org/apache/lucene/queryParser/QueryParser.jjindex 46572f982e8..3f3a140f25a 100644--- a/src/java/org/apache/lucene/queryParser/QueryParser.jj+++ b/src/java/org/apache/lucene/queryParser/QueryParser.jj@@ -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;}diff --git a/src/java/org/apache/lucene/search/MultiTermQuery.java b/src/java/org/apache/lucene/search/MultiTermQuery.javaindex 87a45a20428..fc59e597c5a 100644--- a/src/java/org/apache/lucene/search/MultiTermQuery.java+++ b/src/java/org/apache/lucene/search/MultiTermQuery.java@@ -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);}diff --git a/src/java/org/apache/lucene/search/PhraseQuery.java b/src/java/org/apache/lucene/search/PhraseQuery.javaindex 32a85111269..86ce7fde322 100644--- a/src/java/org/apache/lucene/search/PhraseQuery.java+++ b/src/java/org/apache/lucene/search/PhraseQuery.java@@ -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 workslike a <code>WITHIN</code> or <code>NEAR</code> operator.diff --git a/src/java/org/apache/lucene/search/PrefixQuery.java b/src/java/org/apache/lucene/search/PrefixQuery.javaindex d1c8bf9d7c0..29163661fae 100644--- a/src/java/org/apache/lucene/search/PrefixQuery.java+++ b/src/java/org/apache/lucene/search/PrefixQuery.java@@ -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;diff --git a/src/java/org/apache/lucene/search/Query.java b/src/java/org/apache/lucene/search/Query.javaindex 179bfe9a8ca..3bf6ba37422 100644--- a/src/java/org/apache/lucene/search/Query.java+++ b/src/java/org/apache/lucene/search/Query.java@@ -73,6 +73,9 @@ import org.apache.lucene.index.IndexReader;*/abstract public class Query {+ // query boost factor+ protected float boost = 1.0f;+// query weightingabstract 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 fieldfor terms.<p>The representation used is one that is readable bydiff --git a/src/java/org/apache/lucene/search/RangeQuery.java b/src/java/org/apache/lucene/search/RangeQuery.javaindex d84d6ef9706..083afe49c8a 100644--- a/src/java/org/apache/lucene/search/RangeQuery.java+++ b/src/java/org/apache/lucene/search/RangeQuery.java@@ -67,7 +67,6 @@ public final class RangeQuery extends Queryprivate 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 Querythis.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;diff --git a/src/java/org/apache/lucene/search/TermQuery.java b/src/java/org/apache/lucene/search/TermQuery.javaindex 8f5b0c0738b..bb55a3086cc 100644--- a/src/java/org/apache/lucene/search/TermQuery.java+++ b/src/java/org/apache/lucene/search/TermQuery.java@@ -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;diff --git a/src/test/org/apache/lucene/queryParser/TestQueryParser.java b/src/test/org/apache/lucene/queryParser/TestQueryParser.javaindex 27aeff2d4ff..1513bd3c76c 100644--- a/src/test/org/apache/lucene/queryParser/TestQueryParser.java+++ b/src/test/org/apache/lucene/queryParser/TestQueryParser.java@@ -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})");}}