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 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.
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
33 /// <version> $Id: QueryFilter.cs,v 1.2 2005/01/17 19:54:30 joeshaw Exp $
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
)
47 this.enclosingInstance
= enclosingInstance
;
49 private System
.Collections
.BitArray bits
;
50 private QueryFilter enclosingInstance
;
51 public QueryFilter Enclosing_Instance
55 return enclosingInstance
;
59 public override void Collect(int doc
, float score
)
61 bits
.Set(doc
, true); // set bit for hit
66 private System
.Collections
.Hashtable cache
= null;
68 /// <summary>Constructs a filter which only matches documents matching
69 /// <code>query</code>.
71 public QueryFilter(Query query
)
76 public override System
.Collections
.BitArray
Bits(IndexReader reader
)
81 cache
= new System
.Collections
.Hashtable();
87 System
.Collections
.BitArray cached
= (System
.Collections
.BitArray
) cache
[reader
];
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));
101 cache
[reader
] = bits
;
107 public override System
.String
ToString()
109 return "QueryFilter(" + query
+ ")";