old python needs __main__ to call a module
[pylucene.git] / samples / LuceneInAction / lia / searching / BooleanQueryTest.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     Term, BooleanQuery, IndexSearcher, \
19     NumericRangeQuery, TermQuery, BooleanClause
20
21
22 class BooleanQueryTest(LiaTestCase):
23
24     def testAnd(self):
25
26         searchingBooks = TermQuery(Term("subject", "search"))
27         books2004 = NumericRangeQuery.newIntRange("pubmonth",
28                                                   Integer(200401),
29                                                   Integer(200412),
30                                                   True, True)
31
32         searchingBooks2004 = BooleanQuery()
33         searchingBooks2004.add(searchingBooks, BooleanClause.Occur.MUST)
34         searchingBooks2004.add(books2004, BooleanClause.Occur.MUST)
35
36         searcher = IndexSearcher(self.directory, True)
37         scoreDocs = searcher.search(searchingBooks2004, 50).scoreDocs
38
39         self.assertHitsIncludeTitle(searcher, scoreDocs, "Lucene in Action")
40
41     def testOr(self):
42
43         methodologyBooks = TermQuery(Term("category",
44                                           "/technology/computers/programming/methodology"))
45         easternPhilosophyBooks = TermQuery(Term("category",
46                                                 "/philosophy/eastern"))
47
48         enlightenmentBooks = BooleanQuery()
49         enlightenmentBooks.add(methodologyBooks, BooleanClause.Occur.SHOULD)
50         enlightenmentBooks.add(easternPhilosophyBooks, BooleanClause.Occur.SHOULD)
51
52         searcher = IndexSearcher(self.directory, True)
53         scoreDocs = searcher.search(enlightenmentBooks, 50).scoreDocs
54         print "or =", enlightenmentBooks
55
56         self.assertHitsIncludeTitle(searcher, scoreDocs,
57                                     "Extreme Programming Explained")
58         self.assertHitsIncludeTitle(searcher, scoreDocs,
59                                     u"Tao Te Ching \u9053\u5FB7\u7D93")