Methods Summary |
---|
public void | add(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 .
add(new BooleanClause(query, required, prohibited));
|
public void | add(org.apache.lucene.search.BooleanClause clause)Adds a clause to a boolean query.
if (clauses.size() >= maxClauseCount)
throw new TooManyClauses();
clauses.addElement(clause);
|
public java.lang.Object | clone()
BooleanQuery clone = (BooleanQuery)super.clone();
clone.clauses = (Vector)this.clauses.clone();
return clone;
|
protected org.apache.lucene.search.Weight | createWeight(org.apache.lucene.search.Searcher searcher)
return new BooleanWeight(searcher);
|
public boolean | equals(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 int | getMaxClauseCount()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 int | hashCode()Returns a hash code value for this object.
return Float.floatToIntBits(getBoost()) ^ clauses.hashCode();
|
public org.apache.lucene.search.Query | rewrite(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 void | setMaxClauseCount(int maxClauseCount)Set the maximum number of clauses permitted.
BooleanQuery.maxClauseCount = maxClauseCount;
|
public java.lang.String | toString(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();
|