On Monday 23 August 2004 19:38, Doug Cutting wrote:

> > Does this mean we're limited
> > in the changes that we can apply? So is it okay to deprecate (and
> > later remove) the current constructor that takes two booleans and add
> > one that takes the Occur object?
>
> Yes, I don't see a problem with that.

Okay, here's a new version of the patch. It adds an enumeration and 
deprecates the add(query, boolean, boolean) method. It also deprecates the 
public fields of BooleanClause and adds appropriate get/set methods 
instead. I'll commit this in the next days unless someone sees a problem 
with it.

Regards
 Daniel

-- 
http://www.danielnaber.de
Index: BooleanQuery.java
===================================================================
RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/BooleanQuery.java,v
retrieving revision 1.24
diff -u -r1.24 BooleanQuery.java
--- BooleanQuery.java	14 May 2004 12:56:29 -0000	1.24
+++ BooleanQuery.java	25 Aug 2004 15:40:13 -0000
@@ -24,7 +24,7 @@
   queries, typically [EMAIL PROTECTED] TermQuery}s or [EMAIL PROTECTED] PhraseQuery}s.
   */
 public class BooleanQuery extends Query {
-
+  
   /**
    * Default value is 1024.  Use <code>org.apache.lucene.maxClauseCount</code>
    * system property to override.
@@ -65,14 +65,29 @@
    * It is an error to specify a clause as both <code>required</code> and
    * <code>prohibited</code>.
    *
-   * @see #getMaxClauseCount()
+   * @deprecated use [EMAIL PROTECTED] #add(Query, BooleanClause.Occur)} instead:
+   * <ul>
+   *  <li>For add(query, true, false) use add(query, BooleanClause.Occur.MUST)
+   *  <li>For add(query, false, false) use add(query, BooleanClause.Occur.SHOULD)
+   *  <li>For add(query, false, true) use add(query, BooleanClause.Occur.MUST_NOT)
+   * </ul>
    */
   public void add(Query query, boolean required, boolean prohibited) {
     add(new BooleanClause(query, required, prohibited));
   }
 
   /** Adds a clause to a boolean query.
-    * @see #getMaxClauseCount()
+   *
+   * @throws TooManyClauses if the new number of clauses exceeds the maximum clause number
+   * @see #getMaxClauseCount()
+   */
+  public void add(Query query, BooleanClause.Occur occur) {
+    add(new BooleanClause(query, occur));
+  }
+
+  /** Adds a clause to a boolean query.
+   * @throws TooManyClauses if the new number of clauses exceeds the maximum clause number
+   * @see #getMaxClauseCount()
    */
   public void add(BooleanClause clause) {
     if (clauses.size() >= maxClauseCount)
@@ -94,7 +109,7 @@
       this.searcher = searcher;
       for (int i = 0 ; i < clauses.size(); i++) {
         BooleanClause c = (BooleanClause)clauses.elementAt(i);
-        weights.add(c.query.createWeight(searcher));
+        weights.add(c.getQuery().createWeight(searcher));
       }
     }
 
@@ -106,7 +121,7 @@
       for (int i = 0 ; i < weights.size(); i++) {
         BooleanClause c = (BooleanClause)clauses.elementAt(i);
         Weight w = (Weight)weights.elementAt(i);
-        if (!c.prohibited)
+        if (!c.isProhibited())
           sum += w.sumOfSquaredWeights();         // sum sub weights
       }
 
@@ -121,7 +136,7 @@
       for (int i = 0 ; i < weights.size(); i++) {
         BooleanClause c = (BooleanClause)clauses.elementAt(i);
         Weight w = (Weight)weights.elementAt(i);
-        if (!c.prohibited)
+        if (!c.isProhibited())
           w.normalize(norm);
       }
     }
@@ -137,9 +152,9 @@
       boolean noneBoolean = true;
       for (int i = 0 ; i < weights.size(); i++) {
         BooleanClause c = (BooleanClause)clauses.elementAt(i);
-        if (!c.required)
+        if (!c.isRequired())
           allRequired = false;
-        if (c.query instanceof BooleanQuery)
+        if (c.getQuery() instanceof BooleanQuery)
           noneBoolean = false;
       }
 
@@ -164,8 +179,8 @@
         Weight w = (Weight)weights.elementAt(i);
         Scorer subScorer = w.scorer(reader);
         if (subScorer != null)
-          result.add(subScorer, c.required, c.prohibited);
-        else if (c.required)
+          result.add(subScorer, c.isRequired(), c.isProhibited());
+        else if (c.isRequired())
           return null;
       }
 
@@ -183,16 +198,16 @@
         BooleanClause c = (BooleanClause)clauses.elementAt(i);
         Weight w = (Weight)weights.elementAt(i);
         Explanation e = w.explain(reader, doc);
-        if (!c.prohibited) maxCoord++;
+        if (!c.isProhibited()) maxCoord++;
         if (e.getValue() > 0) {
-          if (!c.prohibited) {
+          if (!c.isProhibited()) {
             sumExpl.addDetail(e);
             sum += e.getValue();
             coord++;
           } else {
             return new Explanation(0.0f, "match prohibited");
           }
-        } else if (c.required) {
+        } else if (c.isRequired()) {
           return new Explanation(0.0f, "match required");
         }
       }
@@ -223,12 +238,12 @@
   public Query rewrite(IndexReader reader) throws IOException {
     if (clauses.size() == 1) {                    // optimize 1-clause queries
       BooleanClause c = (BooleanClause)clauses.elementAt(0);
-      if (!c.prohibited) {			  // just return clause
+      if (!c.isProhibited()) {			  // just return clause
 
-        Query query = c.query.rewrite(reader);    // rewrite first
+        Query query = c.getQuery().rewrite(reader);    // rewrite first
 
         if (getBoost() != 1.0f) {                 // incorporate boost
-          if (query == c.query)                   // if rewrite was no-op
+          if (query == c.getQuery())                   // if rewrite was no-op
             query = (Query)query.clone();         // then clone before boost
           query.setBoost(getBoost() * query.getBoost());
         }
@@ -240,12 +255,12 @@
     BooleanQuery clone = null;                    // recursively rewrite
     for (int i = 0 ; i < clauses.size(); i++) {
       BooleanClause c = (BooleanClause)clauses.elementAt(i);
-      Query query = c.query.rewrite(reader);
-      if (query != c.query) {                     // clause rewrote: must clone
+      Query query = c.getQuery().rewrite(reader);
+      if (query != c.getQuery()) {                     // clause rewrote: must clone
         if (clone == null)
           clone = (BooleanQuery)this.clone();
         clone.clauses.setElementAt
-          (new BooleanClause(query, c.required, c.prohibited), i);
+          (new BooleanClause(query, c.getOccur()), i);
       }
     }
     if (clone != null) {
@@ -270,18 +285,18 @@
 
     for (int i = 0 ; i < clauses.size(); i++) {
       BooleanClause c = (BooleanClause)clauses.elementAt(i);
-      if (c.prohibited)
+      if (c.isProhibited())
 	buffer.append("-");
-      else if (c.required)
+      else if (c.isRequired())
 	buffer.append("+");
 
-      Query subQuery = c.query;
+      Query subQuery = c.getQuery();
       if (subQuery instanceof BooleanQuery) {	  // wrap sub-bools in parens
 	buffer.append("(");
-	buffer.append(c.query.toString(field));
+	buffer.append(c.getQuery().toString(field));
 	buffer.append(")");
       } else
-	buffer.append(c.query.toString(field));
+	buffer.append(c.getQuery().toString(field));
 
       if (i != clauses.size()-1)
 	buffer.append(" ");
Index: BooleanClause.java
===================================================================
RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/BooleanClause.java,v
retrieving revision 1.6
diff -u -r1.6 BooleanClause.java
--- BooleanClause.java	29 Mar 2004 22:48:03 -0000	1.6
+++ BooleanClause.java	25 Aug 2004 15:40:00 -0000
@@ -18,21 +18,115 @@
 
 /** A clause in a BooleanQuery. */
 public class BooleanClause implements java.io.Serializable {
-  /** The query whose matching documents are combined by the boolean query. */
-  public Query query;
+  
+  public static final class Occur {
+    
+    private String name;
+    
+    private Occur() {
+      // typesafe enum pattern, no public constructor
+    }
+    
+    private Occur(String name) {
+      // typesafe enum pattern, no public constructor
+      this.name = name;
+    }
+    
+    public String toString() {
+      return name;
+    }
+
+    /** Use this operator for terms that <i>must</i> appear in the matching documents. */
+    public static final Occur MUST = new Occur("MUST");
+    /** Use this operator for terms of which <i>should</i> appear in the 
+     * matching documents. For a BooleanQuery with two <code>SHOULD</code> 
+     * subqueries, at least one of the queries must appear in the matching documents. */
+    public static final Occur SHOULD = new Occur("SHOULD");
+    /** Use this operator for terms that <i>must not</i> appear in the matching documents.
+     * Note that it is not possible to search for queries that only consist
+     * of a <code>MUST_NOT</code> query. */
+    public static final Occur MUST_NOT = new Occur("MUST_NOT");
+    
+  }
+
+  /** The query whose matching documents are combined by the boolean query.
+   *     @deprecated use [EMAIL PROTECTED] #setQuery(Query)} instead */
+  public Query query;    // TODO: decrease visibility for Lucene 2.0
+
   /** If true, documents documents which <i>do not</i>
-    match this sub-query will <i>not</i> match the boolean query. */
-  public boolean required = false;
-  /** If true, documents documents which <i>do</i>
-    match this sub-query will <i>not</i> match the boolean query. */
-  public boolean prohibited = false;
+    match this sub-query will <i>not</i> match the boolean query.
+    @deprecated use [EMAIL PROTECTED] #setOccur(Occur)} instead */
+  public boolean required = false;  // TODO: decrease visibility for Lucene 2.0
   
+  /** If true, documents documents which <i>do</i>
+    match this sub-query will <i>not</i> match the boolean query.
+    @deprecated use [EMAIL PROTECTED] #setOccur(Occur)} instead */
+  public boolean prohibited = false;  // TODO: decrease visibility for Lucene 2.0
+
+  private Occur occur;
+
   /** Constructs a BooleanClause with query <code>q</code>, required
-    <code>r</code> and prohibited <code>p</code>. */ 
+   * <code>r</code> and prohibited <code>p</code>.
+   * @deprecated use BooleanClause(Query, Occur) instead
+   * <ul>
+   *  <li>For BooleanClause(query, true, false) use BooleanClause(query, BooleanClause.Occur.MUST)
+   *  <li>For BooleanClause(query, false, false) use BooleanClause(query, BooleanClause.Occur.SHOULD)
+   *  <li>For BooleanClause(query, false, true) use BooleanClause(query, BooleanClause.Occur.MUST_NOT)
+   * </ul>
+   */ 
   public BooleanClause(Query q, boolean r, boolean p) {
+    // TODO: remove for Lucene 2.0
     query = q;
     required = r;
     prohibited = p;
+  }
+
+  /** Constructs a BooleanClause.
+  */ 
+  public BooleanClause(Query query, Occur occur) {
+    this.query = query;
+    this.occur = occur;
+    setFields(occur);
+  }
+
+  public Occur getOccur() {
+    return occur;
+  }
+
+  public void setOccur(Occur occur) {
+    this.occur = occur;
+    setFields(occur);
+  }
+
+  public Query getQuery() {
+    return query;
+  }
+
+  public void setQuery(Query query) {
+    this.query = query;
+  }
+  
+  public boolean isProhibited() {
+    return prohibited;
+  }
+
+  public boolean isRequired() {
+    return required;
+  }
+
+  private void setFields(Occur occur) {
+    if (occur == Occur.MUST) {
+      required = true;
+      prohibited = false;
+    } else if (occur == Occur.SHOULD) {
+      required = false;
+      prohibited = false;
+    } else if (occur == Occur.MUST_NOT) {
+      required = false;
+      prohibited = true;
+    } else {
+      throw new IllegalArgumentException("Unknown operator " + occur);
+    }
   }
 
   /** Returns true iff <code>o</code> is equal to this. */

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

Reply via email to