KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > FilteredQuery


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.index.IndexReader;
20 import org.apache.lucene.util.ToStringUtils;
21
22 import java.io.IOException JavaDoc;
23 import java.util.BitSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26
27 /**
28  * A query that applies a filter to the results of another query.
29  *
30  * <p>Note: the bits are retrieved from the filter each time this
31  * query is used in a search - use a CachingWrapperFilter to avoid
32  * regenerating the bits every time.
33  *
34  * <p>Created: Apr 20, 2004 8:58:29 AM
35  *
36  * @author Tim Jones
37  * @since 1.4
38  * @version $Id: FilteredQuery.java 331113 2005-11-06 15:55:45Z yonik $
39  * @see CachingWrapperFilter
40  */

41 public class FilteredQuery
42 extends Query {
43
44   Query query;
45   Filter filter;
46
47   /**
48    * Constructs a new query which applies a filter to the results of the original query.
49    * Filter.bits() will be called every time this query is used in a search.
50    * @param query Query to be filtered, cannot be <code>null</code>.
51    * @param filter Filter to apply to query results, cannot be <code>null</code>.
52    */

53   public FilteredQuery (Query query, Filter filter) {
54     this.query = query;
55     this.filter = filter;
56   }
57
58
59
60   /**
61    * Returns a Weight that applies the filter to the enclosed query's Weight.
62    * This is accomplished by overriding the Scorer returned by the Weight.
63    */

64   protected Weight createWeight (final Searcher searcher) throws IOException JavaDoc {
65     final Weight weight = query.createWeight (searcher);
66     final Similarity similarity = query.getSimilarity(searcher);
67     return new Weight() {
68
69       // pass these methods through to enclosed query's weight
70
public float getValue() { return weight.getValue(); }
71       public float sumOfSquaredWeights() throws IOException JavaDoc { return weight.sumOfSquaredWeights(); }
72       public void normalize (float v) { weight.normalize(v); }
73       public Explanation explain (IndexReader ir, int i) throws IOException JavaDoc { return weight.explain (ir, i); }
74
75       // return this query
76
public Query getQuery() { return FilteredQuery.this; }
77
78       // return a scorer that overrides the enclosed query's score if
79
// the given hit has been filtered out.
80
public Scorer scorer (IndexReader indexReader) throws IOException JavaDoc {
81         final Scorer scorer = weight.scorer (indexReader);
82         final BitSet JavaDoc bitset = filter.bits (indexReader);
83         return new Scorer (similarity) {
84
85           // pass these methods through to the enclosed scorer
86
public boolean next() throws IOException JavaDoc { return scorer.next(); }
87           public int doc() { return scorer.doc(); }
88           public boolean skipTo (int i) throws IOException JavaDoc { return scorer.skipTo(i); }
89
90           // if the document has been filtered out, set score to 0.0
91
public float score() throws IOException JavaDoc {
92             return (bitset.get(scorer.doc())) ? scorer.score() : 0.0f;
93           }
94
95           // add an explanation about whether the document was filtered
96
public Explanation explain (int i) throws IOException JavaDoc {
97             Explanation exp = scorer.explain (i);
98             if (bitset.get(i))
99               exp.setDescription ("allowed by filter: "+exp.getDescription());
100             else
101               exp.setDescription ("removed by filter: "+exp.getDescription());
102             return exp;
103           }
104         };
105       }
106     };
107   }
108
109   /** Rewrites the wrapped query. */
110   public Query rewrite(IndexReader reader) throws IOException JavaDoc {
111     Query rewritten = query.rewrite(reader);
112     if (rewritten != query) {
113       FilteredQuery clone = (FilteredQuery)this.clone();
114       clone.query = rewritten;
115       return clone;
116     } else {
117       return this;
118     }
119   }
120
121   public Query getQuery() {
122     return query;
123   }
124
125   public Filter getFilter() {
126     return filter;
127   }
128
129   // inherit javadoc
130
public void extractTerms(Set JavaDoc terms) {
131       getQuery().extractTerms(terms);
132   }
133
134   /** Prints a user-readable version of this query. */
135   public String JavaDoc toString (String JavaDoc s) {
136     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
137     buffer.append("filtered(");
138     buffer.append(query.toString(s));
139     buffer.append(")->");
140     buffer.append(filter);
141     buffer.append(ToStringUtils.boost(getBoost()));
142     return buffer.toString();
143   }
144
145   /** Returns true iff <code>o</code> is equal to this. */
146   public boolean equals(Object JavaDoc o) {
147     if (o instanceof FilteredQuery) {
148       FilteredQuery fq = (FilteredQuery) o;
149       return (query.equals(fq.query) && filter.equals(fq.filter));
150     }
151     return false;
152   }
153
154   /** Returns a hash code value for this object. */
155   public int hashCode() {
156     return query.hashCode() ^ filter.hashCode();
157   }
158 }
159
Popular Tags