PyLucene 3.4.0-1 import
[pylucene.git] / test / test_PrefixFilter.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 unittest import TestCase, main
16 from lucene import *
17
18
19 class PrefixFilterTestCase(TestCase):
20     """
21     Unit tests ported from Java Lucene
22     """
23
24     def testPrefixFilter(self):
25
26         directory = RAMDirectory()
27
28         categories = ["/Computers/Linux",
29                       "/Computers/Mac/One",
30                       "/Computers/Mac/Two",
31                       "/Computers/Windows"]
32
33         writer = IndexWriter(directory, WhitespaceAnalyzer(), True,
34                              IndexWriter.MaxFieldLength.LIMITED)
35
36         for category in categories:
37             doc = Document()
38             doc.add(Field("category", category,
39                           Field.Store.YES, Field.Index.NOT_ANALYZED))
40             writer.addDocument(doc)
41
42         writer.close()
43
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")
51
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")
57
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")
63
64         # test end of values
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")
69
70         # test non-existant
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")
75
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")
81
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")
87
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")
93
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")
99
100
101 if __name__ == "__main__":
102     import sys, lucene
103     lucene.initVM()
104     if '-loop' in sys.argv:
105         sys.argv.remove('-loop')
106         while True:
107             try:
108                 main()
109             except:
110                 pass
111     else:
112          main()