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
6 # http://www.apache.org/licenses/LICENSE-2.0
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 # ====================================================================
15 from unittest import TestCase, main
19 class PrefixFilterTestCase(TestCase):
21 Unit tests ported from Java Lucene
24 def testPrefixFilter(self):
26 directory = RAMDirectory()
28 categories = ["/Computers/Linux",
33 writer = IndexWriter(directory, WhitespaceAnalyzer(), True,
34 IndexWriter.MaxFieldLength.LIMITED)
36 for category in categories:
38 doc.add(Field("category", category,
39 Field.Store.YES, Field.Index.NOT_ANALYZED))
40 writer.addDocument(doc)
44 # PrefixFilter combined with ConstantScoreQuery
45 filter = PrefixFilter(Term("category", "/Computers"))
46 query = ConstantScoreQuery(filter)
47 searcher = IndexSearcher(directory, True)
48 topDocs = searcher.search(query, 50)
49 self.assertEqual(4, topDocs.totalHits,
50 "All documents in /Computers category and below")
52 # test middle of values
53 filter = PrefixFilter(Term("category", "/Computers/Mac"))
54 query = ConstantScoreQuery(filter)
55 topDocs = searcher.search(query, 50)
56 self.assertEqual(2, topDocs.totalHits, "Two in /Computers/Mac")
58 # test start of values
59 filter = PrefixFilter(Term("category", "/Computers/Linux"))
60 query = ConstantScoreQuery(filter)
61 topDocs = searcher.search(query, 50)
62 self.assertEqual(1, topDocs.totalHits, "One in /Computers/Linux")
65 filter = PrefixFilter(Term("category", "/Computers/Windows"))
66 query = ConstantScoreQuery(filter)
67 topDocs = searcher.search(query, 50)
68 self.assertEqual(1, topDocs.totalHits, "One in /Computers/Windows")
71 filter = PrefixFilter(Term("category", "/Computers/ObsoleteOS"))
72 query = ConstantScoreQuery(filter)
73 topDocs = searcher.search(query, 50)
74 self.assertEqual(0, topDocs.totalHits, "no documents")
76 # test non-existant, before values
77 filter = PrefixFilter(Term("category", "/Computers/AAA"))
78 query = ConstantScoreQuery(filter)
79 topDocs = searcher.search(query, 50)
80 self.assertEqual(0, topDocs.totalHits, "no documents")
82 # test non-existant, after values
83 filter = PrefixFilter(Term("category", "/Computers/ZZZ"))
84 query = ConstantScoreQuery(filter)
85 topDocs = searcher.search(query, 50)
86 self.assertEqual(0, topDocs.totalHits, "no documents")
88 # test zero-length prefix
89 filter = PrefixFilter(Term("category", ""))
90 query = ConstantScoreQuery(filter)
91 topDocs = searcher.search(query, 50)
92 self.assertEqual(4, topDocs.totalHits, "all documents")
94 # test non-existant field
95 filter = PrefixFilter(Term("nonexistantfield", "/Computers"))
96 query = ConstantScoreQuery(filter)
97 topDocs = searcher.search(query, 50)
98 self.assertEqual(0, topDocs.totalHits, "no documents")
101 if __name__ == "__main__":
104 if '-loop' in sys.argv:
105 sys.argv.remove('-loop')