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 lia.common.LiaTestCase import LiaTestCase
17 from lucene import Integer, \
18 IndexSearcher, Term, TermQuery, MatchAllDocsQuery, \
19 BooleanQuery, BooleanClause, CachingWrapperFilter, \
20 TermRangeFilter, NumericRangeFilter, FieldCacheRangeFilter, \
21 FieldCacheTermsFilter, QueryWrapperFilter, PrefixFilter
24 class FilterTest(LiaTestCase):
28 super(FilterTest, self).setUp()
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)
35 def testTermRangeFilter(self):
37 filter = TermRangeFilter("title2", "d", "j", True, True)
38 scoreDocs = self.searcher.search(self.allBooks, filter, 50).scoreDocs
39 self.assertEqual(3, len(scoreDocs))
41 def testNumericDateFilter(self):
43 filter = NumericRangeFilter.newIntRange("pubmonth",
47 scoreDocs = self.searcher.search(self.allBooks, filter, 50).scoreDocs
48 self.assertEqual(1, len(scoreDocs))
50 def testFieldCacheRangeFilter(self):
52 filter = FieldCacheRangeFilter.newStringRange("title2", "d", "j",
54 scoreDocs = self.searcher.search(self.allBooks, filter, 50).scoreDocs
55 self.assertEqual(3, len(scoreDocs))
57 filter = FieldCacheRangeFilter.newIntRange("pubmonth",
61 scoreDocs = self.searcher.search(self.allBooks, filter, 50).scoreDocs
62 self.assertEqual(1, len(scoreDocs))
64 def testFieldCacheTermsFilter(self):
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")
73 def testQueryWrapperFilter(self):
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")
80 def testSpanQueryFilter(self):
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")
87 def testFilterAlternative(self):
89 categoryQuery = TermQuery(Term("category", "/philosophy/eastern"))
91 constrainedQuery = BooleanQuery()
92 constrainedQuery.add(self.allBooks, BooleanClause.Occur.MUST)
93 constrainedQuery.add(categoryQuery, BooleanClause.Occur.MUST)
95 scoreDocs = self.searcher.search(constrainedQuery, 50).scoreDocs
96 self.assertEqual(1, len(scoreDocs), "only tao te ching")
98 def testPrefixFilter(self):
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")
105 def testCachingWrapper(self):
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))