Hi,

Following up a related post I made on lucene-user yesterday, I've
tracked down what I consider to be a bug in the Query parser relating to
boosting boolean queries.

In short, I think it's reasonable for a user to want to boost all the
terms within a set of parentheses like this:

(fred jim bob)^2.0

However, this fails with the current query parser - the boost factor is
silently ignored.

Please find attached a patch that adds two assertions to the query
parser test, demonstrating the above problem. The patch also includes a
minor change to QueryParser.jj to fix the problem and a tweak to
BooleanQuery.toString() to help test it.

Regards,

-- 
Lee Mallabone.
Granta Design Ltd.
Index: src/java/org/apache/lucene/queryParser/QueryParser.jj
===================================================================
RCS file: /home/cvspublic/jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj,v
retrieving revision 1.21
diff -u -r1.21 QueryParser.jj
--- src/java/org/apache/lucene/queryParser/QueryParser.jj	18 Jul 2002 14:18:42 -0000	1.21
+++ src/java/org/apache/lucene/queryParser/QueryParser.jj	6 Sep 2002 09:40:38 -0000
@@ -424,7 +424,7 @@
 
 Query Clause(String field) : {
   Query q;
-  Token fieldToken=null;
+  Token fieldToken=null,boost=null;
 }
 {
   [
@@ -434,9 +434,16 @@
 
   (
    q=Term(field)
-   | <LPAREN> q=Query(field) <RPAREN>
+   | <LPAREN> q=Query(field) <RPAREN> (<CARAT> boost=<NUMBER>)?
   )
     {
+      if (boost != null) {
+      	float f = (float)1.0;
+	try {
+	  f = Float.valueOf(boost.image).floatValue();
+         q.setBoost(f);
+	} catch (Exception ignored) { }
+      }
       return q;
     }
 }
Index: src/java/org/apache/lucene/search/BooleanQuery.java
===================================================================
RCS file: /home/cvspublic/jakarta-lucene/src/java/org/apache/lucene/search/BooleanQuery.java,v
retrieving revision 1.4
diff -u -r1.4 BooleanQuery.java
--- src/java/org/apache/lucene/search/BooleanQuery.java	17 Jul 2002 17:38:04 -0000	1.4
+++ src/java/org/apache/lucene/search/BooleanQuery.java	6 Sep 2002 09:40:38 -0000
@@ -142,6 +142,10 @@
   /** Prints a user-readable version of this query. */
   public String toString(String field) {
     StringBuffer buffer = new StringBuffer();
+    if (boost > 1.0) {
+      buffer.append("(");
+    }
+     
     for (int i = 0 ; i < clauses.size(); i++) {
       BooleanClause c = (BooleanClause)clauses.elementAt(i);
       if (c.prohibited)
@@ -161,6 +165,12 @@
       if (i != clauses.size()-1)
 	buffer.append(" ");
     }
+    
+    if (boost > 1.0) {
+      buffer.append(")^");
+      buffer.append(boost);
+    }
+     
     return buffer.toString();
   }
 
Index: src/test/org/apache/lucene/queryParser/TestQueryParser.java
===================================================================
RCS file: /home/cvspublic/jakarta-lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java,v
retrieving revision 1.15
diff -u -r1.15 TestQueryParser.java
--- src/test/org/apache/lucene/queryParser/TestQueryParser.java	18 Jul 2002 14:17:41 -0000	1.15
+++ src/test/org/apache/lucene/queryParser/TestQueryParser.java	6 Sep 2002 09:40:39 -0000
@@ -190,6 +190,8 @@
 	assertTrue(getQuery("\"hello there\"", null) instanceof PhraseQuery);
 
 	assertQueryEquals("germ term^2.0", null, "germ term^2.0");
+	assertQueryEquals("(term)^2.0", null, "term^2.0");
+	assertQueryEquals("(germ term)^2.0", null, "(germ term)^2.0");
 	assertQueryEquals("term^2.0", null, "term^2.0");
 	assertQueryEquals("term^2", null, "term^2.0");
 	assertQueryEquals("\"germ term\"^2.0", null, "\"germ term\"^2.0");

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to