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 Term, BooleanQuery, IndexSearcher, \
19 NumericRangeQuery, TermQuery, BooleanClause
22 class BooleanQueryTest(LiaTestCase):
26 searchingBooks = TermQuery(Term("subject", "search"))
27 books2004 = NumericRangeQuery.newIntRange("pubmonth",
32 searchingBooks2004 = BooleanQuery()
33 searchingBooks2004.add(searchingBooks, BooleanClause.Occur.MUST)
34 searchingBooks2004.add(books2004, BooleanClause.Occur.MUST)
36 searcher = IndexSearcher(self.directory, True)
37 scoreDocs = searcher.search(searchingBooks2004, 50).scoreDocs
39 self.assertHitsIncludeTitle(searcher, scoreDocs, "Lucene in Action")
43 methodologyBooks = TermQuery(Term("category",
44 "/technology/computers/programming/methodology"))
45 easternPhilosophyBooks = TermQuery(Term("category",
46 "/philosophy/eastern"))
48 enlightenmentBooks = BooleanQuery()
49 enlightenmentBooks.add(methodologyBooks, BooleanClause.Occur.SHOULD)
50 enlightenmentBooks.add(easternPhilosophyBooks, BooleanClause.Occur.SHOULD)
52 searcher = IndexSearcher(self.directory, True)
53 scoreDocs = searcher.search(enlightenmentBooks, 50).scoreDocs
54 print "or =", enlightenmentBooks
56 self.assertHitsIncludeTitle(searcher, scoreDocs,
57 "Extreme Programming Explained")
58 self.assertHitsIncludeTitle(searcher, scoreDocs,
59 u"Tao Te Ching \u9053\u5FB7\u7D93")