Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / QueryFilter.cs
blobc0d7fd8b4a3b08f4916c3ba694e3e223639e0f81
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.
16 using System;
17 using System.Runtime.InteropServices;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 namespace Lucene.Net.Search
22 /// <summary>Constrains search results to only match those which also match a provided
23 /// query. Results are cached, so that searches after the first on the same
24 /// index using this filter are much faster.
25 ///
26 /// <p> This could be used, for example, with a {@link RangeQuery} on a suitably
27 /// formatted date Field to implement date filtering. One could re-use a single
28 /// QueryFilter that matches, e.g., only documents modified within the last
29 /// week. The QueryFilter and RangeQuery would only need to be reconstructed
30 /// once per day.
31 ///
32 /// </summary>
33 /// <version> $Id: QueryFilter.cs,v 1.2 2005/01/17 19:54:30 joeshaw Exp $
34 /// </version>
35 [Serializable]
36 public class QueryFilter:Filter
38 private class AnonymousClassHitCollector:HitCollector
40 public AnonymousClassHitCollector(System.Collections.BitArray bits, QueryFilter enclosingInstance)
42 InitBlock(bits, enclosingInstance);
44 private void InitBlock(System.Collections.BitArray bits, QueryFilter enclosingInstance)
46 this.bits = bits;
47 this.enclosingInstance = enclosingInstance;
49 private System.Collections.BitArray bits;
50 private QueryFilter enclosingInstance;
51 public QueryFilter Enclosing_Instance
53 get
55 return enclosingInstance;
59 public override void Collect(int doc, float score)
61 bits.Set(doc, true); // set bit for hit
64 private Query query;
65 [NonSerialized]
66 private System.Collections.Hashtable cache = null;
68 /// <summary>Constructs a filter which only matches documents matching
69 /// <code>query</code>.
70 /// </summary>
71 public QueryFilter(Query query)
73 this.query = query;
76 public override System.Collections.BitArray Bits(IndexReader reader)
79 if (cache == null)
81 cache = new System.Collections.Hashtable();
84 lock (cache.SyncRoot)
86 // check cache
87 System.Collections.BitArray cached = (System.Collections.BitArray) cache[reader];
88 if (cached != null)
90 return cached;
94 System.Collections.BitArray bits = new System.Collections.BitArray((reader.MaxDoc() % 64 == 0?reader.MaxDoc() / 64:reader.MaxDoc() / 64 + 1) * 64);
96 new IndexSearcher(reader).Search(query, new AnonymousClassHitCollector(bits, this));
98 lock (cache.SyncRoot)
100 // update cache
101 cache[reader] = bits;
104 return bits;
107 public override System.String ToString()
109 return "QueryFilter(" + query + ")";