pylucene 3.5.0-3
[pylucene.git] / samples / LuceneInAction / lia / advsearching / FilterTest.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 lia.common.LiaTestCase import LiaTestCase
16
17 from lucene import Integer, \
18      IndexSearcher, Term, TermQuery, MatchAllDocsQuery, \
19      BooleanQuery, BooleanClause, CachingWrapperFilter, \
20      TermRangeFilter, NumericRangeFilter, FieldCacheRangeFilter, \
21      FieldCacheTermsFilter, QueryWrapperFilter, PrefixFilter
22      
23
24 class FilterTest(LiaTestCase):
25
26     def setUp(self):
27
28         super(FilterTest, self).setUp()
29
30         self.allBooks = MatchAllDocsQuery()
31         self.searcher = IndexSearcher(self.directory, True)
32         scoreDocs = self.searcher.search(self.allBooks, 50).scoreDocs
33         self.numAllBooks = len(scoreDocs)
34
35     def testTermRangeFilter(self):
36
37         filter = TermRangeFilter("title2", "d", "j", True, True)
38         scoreDocs = self.searcher.search(self.allBooks, filter, 50).scoreDocs
39         self.assertEqual(3, len(scoreDocs))
40
41     def testNumericDateFilter(self):
42
43         filter = NumericRangeFilter.newIntRange("pubmonth",
44                                                 Integer(198805),
45                                                 Integer(198810),
46                                                 True, True)
47         scoreDocs = self.searcher.search(self.allBooks, filter, 50).scoreDocs
48         self.assertEqual(1, len(scoreDocs))
49
50     def testFieldCacheRangeFilter(self):
51
52         filter = FieldCacheRangeFilter.newStringRange("title2", "d", "j",
53                                                       True, True)
54         scoreDocs = self.searcher.search(self.allBooks, filter, 50).scoreDocs
55         self.assertEqual(3, len(scoreDocs))
56
57         filter = FieldCacheRangeFilter.newIntRange("pubmonth",
58                                                    Integer(198805),
59                                                    Integer(198810),
60                                                    True, True)
61         scoreDocs = self.searcher.search(self.allBooks, filter, 50).scoreDocs
62         self.assertEqual(1, len(scoreDocs))
63
64     def testFieldCacheTermsFilter(self):
65
66         filter = FieldCacheTermsFilter("category",
67                                        ["/health/alternative/chinese",
68                                         "/technology/computers/ai",
69                                         "/technology/computers/programming"])
70         scoreDocs = self.searcher.search(self.allBooks, filter, 50).scoreDocs
71         self.assertEqual(7, len(scoreDocs), "expected 7 hits")
72
73     def testQueryWrapperFilter(self):
74
75         categoryQuery = TermQuery(Term("category", "/philosophy/eastern"))
76         categoryFilter = QueryWrapperFilter(categoryQuery)
77         scoreDocs = self.searcher.search(self.allBooks, categoryFilter, 50).scoreDocs
78         self.assertEqual(1, len(scoreDocs), "only tao te ching")
79
80     def testSpanQueryFilter(self):
81         
82         categoryQuery = TermQuery(Term("category", "/philosophy/eastern"))
83         categoryFilter = QueryWrapperFilter(categoryQuery)
84         scoreDocs = self.searcher.search(self.allBooks, categoryFilter, 50).scoreDocs
85         self.assertEqual(1, len(scoreDocs), "only tao te ching")
86
87     def testFilterAlternative(self):
88
89         categoryQuery = TermQuery(Term("category", "/philosophy/eastern"))
90
91         constrainedQuery = BooleanQuery()
92         constrainedQuery.add(self.allBooks, BooleanClause.Occur.MUST)
93         constrainedQuery.add(categoryQuery, BooleanClause.Occur.MUST)
94
95         scoreDocs = self.searcher.search(constrainedQuery, 50).scoreDocs
96         self.assertEqual(1, len(scoreDocs), "only tao te ching")
97
98     def testPrefixFilter(self):
99
100         prefixFilter = PrefixFilter(Term("category", "/technology/computers"))
101         scoreDocs = self.searcher.search(self.allBooks, prefixFilter, 50).scoreDocs
102         self.assertEqual(8, len(scoreDocs),
103                          "only /technology/computers/* books")
104
105     def testCachingWrapper(self):
106
107         filter = TermRangeFilter("title2", "d", "j", True, True)
108         cachingFilter = CachingWrapperFilter(filter)
109         scoreDocs = self.searcher.search(self.allBooks, cachingFilter, 50).scoreDocs
110         self.assertEqual(3, len(scoreDocs))