2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 IndexReader
= Lucene
.Net
.Index
.IndexReader
;
18 namespace Lucene
.Net
.Search
22 /// <summary> A query that applies a filter to the results of another query.
24 /// <p>Note: the bits are retrieved from the filter each time this
25 /// query is used in a search - use a CachingWrapperFilter to avoid
26 /// regenerating the bits every time.
28 /// <p>Created: Apr 20, 2004 8:58:29 AM
31 /// <author> Tim Jones
35 /// <version> $Id: FilteredQuery.cs,v 1.1 2005/01/17 19:54:30 joeshaw Exp $
37 /// <seealso cref="CachingWrapperFilter">
40 public class FilteredQuery
: Query
43 private class AnonymousClassWeight
: Weight
45 public AnonymousClassWeight(Lucene
.Net
.Search
.Weight weight
, Lucene
.Net
.Search
.Searcher searcher
, FilteredQuery enclosingInstance
)
47 InitBlock(weight
, searcher
, enclosingInstance
);
49 private class AnonymousClassScorer
: Scorer
51 private void InitBlock(Lucene
.Net
.Search
.Scorer scorer
, System
.Collections
.BitArray bitset
, AnonymousClassWeight enclosingInstance
)
55 this.enclosingInstance
= enclosingInstance
;
57 private Lucene
.Net
.Search
.Scorer scorer
;
58 private System
.Collections
.BitArray bitset
;
59 private AnonymousClassWeight enclosingInstance
;
60 public AnonymousClassWeight Enclosing_Instance
64 return enclosingInstance
;
68 internal AnonymousClassScorer(Lucene
.Net
.Search
.Scorer scorer
, System
.Collections
.BitArray bitset
, AnonymousClassWeight enclosingInstance
, Lucene
.Net
.Search
.Similarity Param1
):base(Param1
)
70 InitBlock(scorer
, bitset
, enclosingInstance
);
73 // pass these methods through to the enclosed scorer
74 public override bool Next()
78 public override int Doc()
82 public override bool SkipTo(int i
)
84 return scorer
.SkipTo(i
);
87 // if the document has been filtered out, set score to 0.0
88 public override float Score()
90 return (bitset
.Get(scorer
.Doc()))?scorer
.Score():0.0f
;
93 // add an explanation about whether the document was filtered
94 public override Explanation
Explain(int i
)
96 Explanation exp
= scorer
.Explain(i
);
98 exp
.SetDescription("allowed by filter: " + exp
.GetDescription());
100 exp
.SetDescription("removed by filter: " + exp
.GetDescription());
104 private void InitBlock(Lucene
.Net
.Search
.Weight weight
, Lucene
.Net
.Search
.Searcher searcher
, FilteredQuery enclosingInstance
)
106 this.weight
= weight
;
107 this.searcher
= searcher
;
108 this.enclosingInstance
= enclosingInstance
;
110 private Lucene
.Net
.Search
.Weight weight
;
111 private Lucene
.Net
.Search
.Searcher searcher
;
112 private FilteredQuery enclosingInstance
;
113 virtual public float Value
115 // pass these methods through to enclosed query's weight
123 virtual public Query Query
129 return Enclosing_Instance
;
133 public FilteredQuery Enclosing_Instance
137 return enclosingInstance
;
141 public virtual float SumOfSquaredWeights()
143 return weight
.SumOfSquaredWeights();
145 public virtual void Normalize(float v
)
149 public virtual Explanation
Explain(IndexReader ir
, int i
)
151 return weight
.Explain(ir
, i
);
154 // return a scorer that overrides the enclosed query's score if
155 // the given hit has been filtered out.
156 public virtual Scorer
Scorer(IndexReader indexReader
)
158 Scorer scorer
= weight
.Scorer(indexReader
);
159 System
.Collections
.BitArray bitset
= Enclosing_Instance
.filter
.Bits(indexReader
);
160 return new AnonymousClassScorer(scorer
, bitset
, this, Enclosing_Instance
.query
.GetSimilarity(searcher
));
164 internal Query query
;
165 internal Filter filter
;
167 /// <summary> Constructs a new query which applies a filter to the results of the original query.
168 /// Filter.bits() will be called every time this query is used in a search.
170 /// <param name="query"> Query to be filtered, cannot be <code>null</code>.
172 /// <param name="filter">Filter to apply to query results, cannot be <code>null</code>.
174 public FilteredQuery(Query query
, Filter filter
)
177 this.filter
= filter
;
180 /// <summary> Returns a Weight that applies the filter to the enclosed query's Weight.
181 /// This is accomplished by overriding the Scorer returned by the Weight.
183 protected internal override Weight
CreateWeight(Searcher searcher
)
185 Weight weight
= query
.CreateWeight(searcher
);
186 return new AnonymousClassWeight(weight
, searcher
, this);
189 /// <summary>Rewrites the wrapped query. </summary>
190 public override Query
Rewrite(IndexReader reader
)
192 Query rewritten
= query
.Rewrite(reader
);
193 if (rewritten
!= query
)
195 FilteredQuery clone
= (FilteredQuery
) this.Clone();
196 clone
.query
= rewritten
;
205 public virtual Query
GetQuery()
210 /// <summary>Prints a user-readable version of this query. </summary>
211 public override System
.String
ToString(System
.String s
)
213 return "filtered(" + query
.ToString(s
) + ")->" + filter
;
216 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
217 public override bool Equals(System
.Object o
)
219 if (o
is FilteredQuery
)
221 FilteredQuery fq
= (FilteredQuery
) o
;
222 return (query
.Equals(fq
.query
) && filter
.Equals(fq
.filter
));
227 /// <summary>Returns a hash code value for this object. </summary>
228 public override int GetHashCode()
230 return query
.GetHashCode() ^ filter
.GetHashCode();