PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / extsearch / filters / SpecialsFilter.py
1 # ====================================================================
2 #   Licensed under the Apache License, Version 2.0 (the "License");
3 #   you may not use this file except in compliance with the License.
4 #   You may obtain a copy of the License at
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #   Unless required by applicable law or agreed to in writing, software
9 #   distributed under the License is distributed on an "AS IS" BASIS,
10 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 #   See the License for the specific language governing permissions and
12 #   limitations under the License.
13 # ====================================================================
14
15 from lucene import \
16     IndexReader, Term, BitSet, PythonFilter, JArray, OpenBitSet
17
18 #
19 # A Filter extension, with a TermDocs wrapper working around the lack of
20 # support for returning values in array arguments.
21 #
22 class SpecialsFilter(PythonFilter):
23
24     def __init__(self, accessor):
25         
26         super(SpecialsFilter, self).__init__()
27         self.accessor = accessor
28
29     def getDocIdSet(self, reader):
30
31         bits = OpenBitSet(long(reader.maxDoc()))
32         isbns = self.accessor.isbns()
33
34         docs = JArray(int)(1)
35         freqs = JArray(int)(1)
36
37         for isbn in isbns:
38             if isbn is not None:
39                 termDocs = reader.termDocs(Term("isbn", isbn))
40                 count = termDocs.read(docs, freqs)
41                 if count == 1:
42                     bits.set(long(docs[0]))
43
44         return bits
45
46     def __str__():
47
48         return "SpecialsFilter"