Methods Summary |
---|
protected org.apache.lucene.search.Weight | createWeight(org.apache.lucene.search.Searcher searcher)Returns a Weight that applies the filter to the enclosed query's Weight.
This is accomplished by overriding the Scorer returned by the Weight.
final Weight weight = query.createWeight (searcher);
final Similarity similarity = query.getSimilarity(searcher);
return new Weight() {
private float value;
// pass these methods through to enclosed query's weight
public float getValue() { return value; }
public float sumOfSquaredWeights() throws IOException {
return weight.sumOfSquaredWeights() * getBoost() * getBoost();
}
public void normalize (float v) {
weight.normalize(v);
value = weight.getValue() * getBoost();
}
public Explanation explain (IndexReader ir, int i) throws IOException {
Explanation inner = weight.explain (ir, i);
if (getBoost()!=1) {
Explanation preBoost = inner;
inner = new Explanation(inner.getValue()*getBoost(),"product of:");
inner.addDetail(new Explanation(getBoost(),"boost"));
inner.addDetail(preBoost);
}
Filter f = FilteredQuery.this.filter;
BitSet matches = f.bits(ir);
if (matches.get(i))
return inner;
Explanation result = new Explanation
(0.0f, "failure to match filter: " + f.toString());
result.addDetail(inner);
return result;
}
// return this query
public Query getQuery() { return FilteredQuery.this; }
// return a filtering scorer
public Scorer scorer (IndexReader indexReader) throws IOException {
final Scorer scorer = weight.scorer (indexReader);
final BitSet bitset = filter.bits (indexReader);
return new Scorer (similarity) {
public boolean next() throws IOException {
do {
if (! scorer.next()) {
return false;
}
} while (! bitset.get(scorer.doc()));
/* When skipTo() is allowed on scorer it should be used here
* in combination with bitset.nextSetBit(...)
* See the while loop in skipTo() below.
*/
return true;
}
public int doc() { return scorer.doc(); }
public boolean skipTo(int i) throws IOException {
if (! scorer.skipTo(i)) {
return false;
}
while (! bitset.get(scorer.doc())) {
int nextFiltered = bitset.nextSetBit(scorer.doc() + 1);
if (nextFiltered == -1) {
return false;
} else if (! scorer.skipTo(nextFiltered)) {
return false;
}
}
return true;
}
public float score() throws IOException { return getBoost() * scorer.score(); }
// add an explanation about whether the document was filtered
public Explanation explain (int i) throws IOException {
Explanation exp = scorer.explain (i);
exp.setValue(getBoost() * exp.getValue());
if (bitset.get(i))
exp.setDescription ("allowed by filter: "+exp.getDescription());
else
exp.setDescription ("removed by filter: "+exp.getDescription());
return exp;
}
};
}
};
|
public boolean | equals(java.lang.Object o)Returns true iff o is equal to this.
if (o instanceof FilteredQuery) {
FilteredQuery fq = (FilteredQuery) o;
return (query.equals(fq.query) && filter.equals(fq.filter) && getBoost()==fq.getBoost());
}
return false;
|
public void | extractTerms(java.util.Set terms)
getQuery().extractTerms(terms);
|
public org.apache.lucene.search.Filter | getFilter()
return filter;
|
public org.apache.lucene.search.Query | getQuery()
return query;
|
public int | hashCode()Returns a hash code value for this object.
return query.hashCode() ^ filter.hashCode() + Float.floatToRawIntBits(getBoost());
|
public org.apache.lucene.search.Query | rewrite(org.apache.lucene.index.IndexReader reader)Rewrites the wrapped query.
Query rewritten = query.rewrite(reader);
if (rewritten != query) {
FilteredQuery clone = (FilteredQuery)this.clone();
clone.query = rewritten;
return clone;
} else {
return this;
}
|
public java.lang.String | toString(java.lang.String s)Prints a user-readable version of this query.
StringBuffer buffer = new StringBuffer();
buffer.append("filtered(");
buffer.append(query.toString(s));
buffer.append(")->");
buffer.append(filter);
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
|