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.
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.
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
35 /// <version> $Id: QueryFilter.cs,v 1.4 2006/10/02 17:09:06 joeshaw Exp $
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
)
49 this.enclosingInstance
= enclosingInstance
;
51 private System
.Collections
.BitArray bits
;
52 private QueryFilter enclosingInstance
;
53 public QueryFilter Enclosing_Instance
57 return enclosingInstance
;
61 public override void Collect(int doc
, float score
)
63 bits
.Set(doc
, true); // set bit for hit
68 private System
.Collections
.Hashtable cache
= null;
70 /// <summary>Constructs a filter which only matches documents matching
71 /// <code>query</code>.
73 public QueryFilter(Query query
)
78 public override System
.Collections
.BitArray
Bits(IndexReader reader
)
83 cache
= new System
.Collections
.Hashtable();
89 System
.Collections
.BitArray cached
= (System
.Collections
.BitArray
) cache
[reader
];
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
)
103 cache
[reader
] = bits
;
109 public override System
.String
ToString()
111 return "QueryFilter(" + query
+ ")";
114 public override bool Equals(System
.Object o
)
116 if (!(o
is QueryFilter
))
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?!