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 FilteredQueryTestCase(TestCase):
21 Unit tests ported from Java Lucene
26 self.directory = RAMDirectory()
27 writer = IndexWriter(self.directory, WhitespaceAnalyzer(), True,
28 IndexWriter.MaxFieldLength.LIMITED)
31 doc.add(Field("field", "one two three four five",
32 Field.Store.YES, Field.Index.ANALYZED))
33 doc.add(Field("sorter", "b",
34 Field.Store.YES, Field.Index.ANALYZED))
36 writer.addDocument(doc)
39 doc.add(Field("field", "one two three four",
40 Field.Store.YES, Field.Index.ANALYZED))
41 doc.add(Field("sorter", "d",
42 Field.Store.YES, Field.Index.ANALYZED))
44 writer.addDocument(doc)
47 doc.add(Field("field", "one two three y",
48 Field.Store.YES, Field.Index.ANALYZED))
49 doc.add(Field("sorter", "a",
50 Field.Store.YES, Field.Index.ANALYZED))
52 writer.addDocument(doc)
55 doc.add(Field("field", "one two x",
56 Field.Store.YES, Field.Index.ANALYZED))
57 doc.add(Field("sorter", "c",
58 Field.Store.YES, Field.Index.ANALYZED))
60 writer.addDocument(doc)
65 self.searcher = IndexSearcher(self.directory, True)
66 self.query = TermQuery(Term("field", "three"))
68 class filter(PythonFilter):
69 def getDocIdSet(self, reader):
73 return DocIdBitSet(bitset)
75 self.filter = filter()
80 self.directory.close()
82 def testFilteredQuery(self):
84 filteredquery = FilteredQuery(self.query, self.filter)
85 topDocs = self.searcher.search(filteredquery, 50)
86 self.assertEqual(1, topDocs.totalHits)
87 self.assertEqual(1, topDocs.scoreDocs[0].doc)
89 topDocs = self.searcher.search(filteredquery, None, 50,
90 Sort(SortField("sorter",
92 self.assertEqual(1, topDocs.totalHits)
93 self.assertEqual(1, topDocs.scoreDocs[0].doc)
95 filteredquery = FilteredQuery(TermQuery(Term("field", "one")),
97 topDocs = self.searcher.search(filteredquery, 50)
98 self.assertEqual(2, topDocs.totalHits)
100 filteredquery = FilteredQuery(TermQuery(Term("field", "x")),
102 topDocs = self.searcher.search(filteredquery, 50)
103 self.assertEqual(1, topDocs.totalHits)
104 self.assertEqual(3, topDocs.scoreDocs[0].doc)
106 filteredquery = FilteredQuery(TermQuery(Term("field", "y")),
108 topDocs = self.searcher.search(filteredquery, 50)
109 self.assertEqual(0, topDocs.totalHits)
111 def testRangeQuery(self):
113 This tests FilteredQuery's rewrite correctness
116 rq = TermRangeQuery("sorter", "b", "d", True, True)
117 filteredquery = FilteredQuery(rq, self.filter)
118 scoreDocs = self.searcher.search(filteredquery, 1000).scoreDocs
119 self.assertEqual(2, len(scoreDocs))
122 if __name__ == "__main__":
125 if '-loop' in sys.argv:
126 sys.argv.remove('-loop')