FileDocCategorySizeDatePackage
BooleanQuery.javaAPI DocApache Lucene 1.4.310683Fri May 14 14:56:30 BST 2004org.apache.lucene.search

BooleanQuery

public class BooleanQuery extends Query
A Query that matches documents matching boolean combinations of other queries, typically {@link TermQuery}s or {@link PhraseQuery}s.

Fields Summary
public static int
maxClauseCount
Default value is 1024. Use org.apache.lucene.maxClauseCount system property to override.
private Vector
clauses
Constructors Summary
public BooleanQuery()
Constructs an empty boolean query.


        
    
Methods Summary
public voidadd(org.apache.lucene.search.Query query, boolean required, boolean prohibited)
Adds a clause to a boolean query. Clauses may be:
  • required which means that documents which do not match this sub-query will not match the boolean query;
  • prohibited which means that documents which do match this sub-query will not match the boolean query; or
  • neither, in which case matched documents are neither prohibited from nor required to match the sub-query. However, a document must match at least 1 sub-query to match the boolean query.
It is an error to specify a clause as both required and prohibited.

see
#getMaxClauseCount()

    add(new BooleanClause(query, required, prohibited));
  
public voidadd(org.apache.lucene.search.BooleanClause clause)
Adds a clause to a boolean query.

see
#getMaxClauseCount()

    if (clauses.size() >= maxClauseCount)
      throw new TooManyClauses();

    clauses.addElement(clause);
  
public java.lang.Objectclone()

    BooleanQuery clone = (BooleanQuery)super.clone();
    clone.clauses = (Vector)this.clauses.clone();
    return clone;
  
protected org.apache.lucene.search.WeightcreateWeight(org.apache.lucene.search.Searcher searcher)

    return new BooleanWeight(searcher);
  
public booleanequals(java.lang.Object o)
Returns true iff o is equal to this.

    if (!(o instanceof BooleanQuery))
      return false;
    BooleanQuery other = (BooleanQuery)o;
    return (this.getBoost() == other.getBoost())
      &&  this.clauses.equals(other.clauses);
  
public org.apache.lucene.search.BooleanClause[]getClauses()
Returns the set of clauses in this query.

    return (BooleanClause[])clauses.toArray(new BooleanClause[0]);
  
public static intgetMaxClauseCount()
Return the maximum number of clauses permitted, 1024 by default. Attempts to add more than the permitted number of clauses cause {@link TooManyClauses} to be thrown.


                
        

                            
       return maxClauseCount; 
public inthashCode()
Returns a hash code value for this object.

    return Float.floatToIntBits(getBoost()) ^ clauses.hashCode();
  
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.index.IndexReader reader)

    if (clauses.size() == 1) {                    // optimize 1-clause queries
      BooleanClause c = (BooleanClause)clauses.elementAt(0);
      if (!c.prohibited) {			  // just return clause

        Query query = c.query.rewrite(reader);    // rewrite first

        if (getBoost() != 1.0f) {                 // incorporate boost
          if (query == c.query)                   // if rewrite was no-op
            query = (Query)query.clone();         // then clone before boost
          query.setBoost(getBoost() * query.getBoost());
        }

        return query;
      }
    }

    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
        if (clone == null)
          clone = (BooleanQuery)this.clone();
        clone.clauses.setElementAt
          (new BooleanClause(query, c.required, c.prohibited), i);
      }
    }
    if (clone != null) {
      return clone;                               // some clauses rewrote
    } else
      return this;                                // no clauses rewrote
  
public static voidsetMaxClauseCount(int maxClauseCount)
Set the maximum number of clauses permitted.

    BooleanQuery.maxClauseCount = maxClauseCount;
  
public java.lang.StringtoString(java.lang.String field)
Prints a user-readable version of this query.

    StringBuffer buffer = new StringBuffer();
    if (getBoost() != 1.0) {
      buffer.append("(");
    }

    for (int i = 0 ; i < clauses.size(); i++) {
      BooleanClause c = (BooleanClause)clauses.elementAt(i);
      if (c.prohibited)
	buffer.append("-");
      else if (c.required)
	buffer.append("+");

      Query subQuery = c.query;
      if (subQuery instanceof BooleanQuery) {	  // wrap sub-bools in parens
	buffer.append("(");
	buffer.append(c.query.toString(field));
	buffer.append(")");
      } else
	buffer.append(c.query.toString(field));

      if (i != clauses.size()-1)
	buffer.append(" ");
    }

    if (getBoost() != 1.0) {
      buffer.append(")^");
      buffer.append(getBoost());
    }

    return buffer.toString();