QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / QueryFilter.cs
blob25f083876c67c4156adcec6cc40f12eaf3e0de2f
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 System.Runtime.InteropServices;
19 using IndexReader = Lucene.Net.Index.IndexReader;
21 namespace Lucene.Net.Search
24 /// <summary>Constrains search results to only match those which also match a provided
25 /// query. Results are cached, so that searches after the first on the same
26 /// index using this filter are much faster.
27 ///
28 /// <p> This could be used, for example, with a {@link RangeQuery} on a suitably
29 /// formatted date field to implement date filtering. One could re-use a single
30 /// QueryFilter that matches, e.g., only documents modified within the last
31 /// week. The QueryFilter and RangeQuery would only need to be reconstructed
32 /// once per day.
33 ///
34 /// </summary>
35 /// <version> $Id: QueryFilter.cs,v 1.4 2006/10/02 17:09:06 joeshaw Exp $
36 /// </version>
37 [Serializable]
38 public class QueryFilter : Filter
40 private class AnonymousClassHitCollector : HitCollector
42 public AnonymousClassHitCollector(System.Collections.BitArray bits, QueryFilter enclosingInstance)
44 InitBlock(bits, enclosingInstance);
46 private void InitBlock(System.Collections.BitArray bits, QueryFilter enclosingInstance)
48 this.bits = bits;
49 this.enclosingInstance = enclosingInstance;
51 private System.Collections.BitArray bits;
52 private QueryFilter enclosingInstance;
53 public QueryFilter Enclosing_Instance
55 get
57 return enclosingInstance;
61 public override void Collect(int doc, float score)
63 bits.Set(doc, true); // set bit for hit
66 private Query query;
67 [NonSerialized]
68 private System.Collections.Hashtable cache = null;
70 /// <summary>Constructs a filter which only matches documents matching
71 /// <code>query</code>.
72 /// </summary>
73 public QueryFilter(Query query)
75 this.query = query;
78 public override System.Collections.BitArray Bits(IndexReader reader)
81 if (cache == null)
83 cache = new System.Collections.Hashtable();
86 lock (cache.SyncRoot)
88 // check cache
89 System.Collections.BitArray cached = (System.Collections.BitArray) cache[reader];
90 if (cached != null)
92 return cached;
96 System.Collections.BitArray bits = new System.Collections.BitArray((reader.MaxDoc() % 64 == 0?reader.MaxDoc() / 64:reader.MaxDoc() / 64 + 1) * 64);
98 new IndexSearcher(reader).Search(query, new AnonymousClassHitCollector(bits, this));
100 lock (cache.SyncRoot)
102 // update cache
103 cache[reader] = bits;
106 return bits;
109 public override System.String ToString()
111 return "QueryFilter(" + query + ")";
114 public override bool Equals(System.Object o)
116 if (!(o is QueryFilter))
117 return false;
118 return this.query.Equals(((QueryFilter) o).query);
121 public override int GetHashCode()
123 return query.GetHashCode() ^ (unchecked((int) 0x923F64B9L)); // {{Aroush-1.9}} Is this OK?!