QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / FilteredQuery.cs
blobe1186a605a5bdbbcd8a131fcaa409a5fa45d0cac
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
21 namespace Lucene.Net.Search
25 /// <summary> A query that applies a filter to the results of another query.
26 ///
27 /// <p>Note: the bits are retrieved from the filter each time this
28 /// query is used in a search - use a CachingWrapperFilter to avoid
29 /// regenerating the bits every time.
30 ///
31 /// <p>Created: Apr 20, 2004 8:58:29 AM
32 ///
33 /// </summary>
34 /// <author> Tim Jones
35 /// </author>
36 /// <since> 1.4
37 /// </since>
38 /// <version> $Id: FilteredQuery.cs,v 1.3 2006/10/02 17:09:04 joeshaw Exp $
39 /// </version>
40 /// <seealso cref="CachingWrapperFilter">
41 /// </seealso>
42 [Serializable]
43 public class FilteredQuery:Query
45 [Serializable]
46 private class AnonymousClassWeight : Weight
48 public AnonymousClassWeight(Lucene.Net.Search.Weight weight, Lucene.Net.Search.Similarity similarity, FilteredQuery enclosingInstance)
50 InitBlock(weight, similarity, enclosingInstance);
52 private class AnonymousClassScorer : Scorer
54 private void InitBlock(Lucene.Net.Search.Scorer scorer, System.Collections.BitArray bitset, AnonymousClassWeight enclosingInstance)
56 this.scorer = scorer;
57 this.bitset = bitset;
58 this.enclosingInstance = enclosingInstance;
60 private Lucene.Net.Search.Scorer scorer;
61 private System.Collections.BitArray bitset;
62 private AnonymousClassWeight enclosingInstance;
63 public AnonymousClassWeight Enclosing_Instance
65 get
67 return enclosingInstance;
71 internal AnonymousClassScorer(Lucene.Net.Search.Scorer scorer, System.Collections.BitArray bitset, AnonymousClassWeight enclosingInstance, Lucene.Net.Search.Similarity Param1):base(Param1)
73 InitBlock(scorer, bitset, enclosingInstance);
76 // pass these methods through to the enclosed scorer
77 public override bool Next()
79 return scorer.Next();
81 public override int Doc()
83 return scorer.Doc();
85 public override bool SkipTo(int i)
87 return scorer.SkipTo(i);
90 // if the document has been filtered out, set score to 0.0
91 public override float Score()
93 return (bitset.Get(scorer.Doc()))?scorer.Score():0.0f;
96 // add an explanation about whether the document was filtered
97 public override Explanation Explain(int i)
99 Explanation exp = scorer.Explain(i);
100 if (bitset.Get(i))
101 exp.SetDescription("allowed by filter: " + exp.GetDescription());
102 else
103 exp.SetDescription("removed by filter: " + exp.GetDescription());
104 return exp;
107 private void InitBlock(Lucene.Net.Search.Weight weight, Lucene.Net.Search.Similarity similarity, FilteredQuery enclosingInstance)
109 this.weight = weight;
110 this.similarity = similarity;
111 this.enclosingInstance = enclosingInstance;
113 private Lucene.Net.Search.Weight weight;
114 private Lucene.Net.Search.Similarity similarity;
115 private FilteredQuery enclosingInstance;
116 public FilteredQuery Enclosing_Instance
120 return enclosingInstance;
125 // pass these methods through to enclosed query's weight
126 public virtual float GetValue()
128 return weight.GetValue();
130 public virtual float SumOfSquaredWeights()
132 return weight.SumOfSquaredWeights();
134 public virtual void Normalize(float v)
136 weight.Normalize(v);
138 public virtual Explanation Explain(IndexReader ir, int i)
140 return weight.Explain(ir, i);
143 // return this query
144 public virtual Query GetQuery()
146 return Enclosing_Instance;
149 // return a scorer that overrides the enclosed query's score if
150 // the given hit has been filtered out.
151 public virtual Scorer Scorer(IndexReader indexReader)
153 Scorer scorer = weight.Scorer(indexReader);
154 System.Collections.BitArray bitset = Enclosing_Instance.filter.Bits(indexReader);
155 return new AnonymousClassScorer(scorer, bitset, this, similarity);
159 internal Query query;
160 internal Filter filter;
162 /// <summary> Constructs a new query which applies a filter to the results of the original query.
163 /// Filter.bits() will be called every time this query is used in a search.
164 /// </summary>
165 /// <param name="query"> Query to be filtered, cannot be <code>null</code>.
166 /// </param>
167 /// <param name="filter">Filter to apply to query results, cannot be <code>null</code>.
168 /// </param>
169 public FilteredQuery(Query query, Filter filter)
171 this.query = query;
172 this.filter = filter;
177 /// <summary> Returns a Weight that applies the filter to the enclosed query's Weight.
178 /// This is accomplished by overriding the Scorer returned by the Weight.
179 /// </summary>
180 protected internal override Weight CreateWeight(Searcher searcher)
182 Weight weight = query.CreateWeight(searcher);
183 Similarity similarity = query.GetSimilarity(searcher);
184 return new AnonymousClassWeight(weight, similarity, this);
187 /// <summary>Rewrites the wrapped query. </summary>
188 public override Query Rewrite(IndexReader reader)
190 Query rewritten = query.Rewrite(reader);
191 if (rewritten != query)
193 FilteredQuery clone = (FilteredQuery) this.Clone();
194 clone.query = rewritten;
195 return clone;
197 else
199 return this;
203 public virtual Query GetQuery()
205 return query;
208 public virtual Filter GetFilter()
210 return filter;
213 // inherit javadoc
214 public override void ExtractTerms(System.Collections.Hashtable terms)
216 GetQuery().ExtractTerms(terms);
219 /// <summary>Prints a user-readable version of this query. </summary>
220 public override System.String ToString(System.String s)
222 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
223 buffer.Append("filtered(");
224 buffer.Append(query.ToString(s));
225 buffer.Append(")->");
226 buffer.Append(filter);
227 buffer.Append(ToStringUtils.Boost(GetBoost()));
228 return buffer.ToString();
231 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
232 public override bool Equals(System.Object o)
234 if (o is FilteredQuery)
236 FilteredQuery fq = (FilteredQuery) o;
237 return (query.Equals(fq.query) && filter.Equals(fq.filter));
239 return false;
242 /// <summary>Returns a hash code value for this object. </summary>
243 public override int GetHashCode()
245 return query.GetHashCode() ^ filter.GetHashCode();