PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / extsearch / filters / SpecialsFilterTest.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 from lia.extsearch.filters.TestSpecialsAccessor import TestSpecialsAccessor
17 from lia.extsearch.filters.SpecialsFilter import SpecialsFilter
18
19 from lucene import \
20      WildcardQuery, FilteredQuery, TermQuery, BooleanQuery, TermRangeQuery, \
21      IndexSearcher, Term, BooleanClause, MatchAllDocsQuery
22
23
24 class SpecialsFilterTest(LiaTestCase):
25
26     def setUp(self):
27
28         super(SpecialsFilterTest, self).setUp()
29
30         self.allBooks = MatchAllDocsQuery()
31         self.searcher = IndexSearcher(self.directory, True)
32
33     def testCustomFilter(self):
34
35         isbns = ["0060812451", "0465026567"]
36         accessor = TestSpecialsAccessor(isbns)
37         
38         filter = SpecialsFilter(accessor)
39         topDocs = self.searcher.search(self.allBooks, filter, 50)
40         self.assertEquals(len(isbns), topDocs.totalHits, "the specials")
41
42     def testFilteredQuery(self):
43         
44         isbns = ["0854402624"]  # Steiner
45
46         accessor = TestSpecialsAccessor(isbns)
47         filter = SpecialsFilter(accessor)
48
49         educationBooks = WildcardQuery(Term("category", "*education*"))
50         edBooksOnSpecial = FilteredQuery(educationBooks, filter)
51
52         logoBooks = TermQuery(Term("subject", "logo"))
53
54         logoOrEdBooks = BooleanQuery()
55         logoOrEdBooks.add(logoBooks, BooleanClause.Occur.SHOULD)
56         logoOrEdBooks.add(edBooksOnSpecial, BooleanClause.Occur.SHOULD)
57
58         topDocs = self.searcher.search(logoOrEdBooks, 50)
59         print logoOrEdBooks
60         self.assertEqual(2, topDocs.totalHits, "Papert and Steiner")