PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / searching / BasicSearchingTest.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 \
18     SimpleAnalyzer, Document, TermQuery, QueryParser, IndexSearcher, Term, \
19     Version
20
21
22 class BasicSearchingTest(LiaTestCase):
23
24     def testTerm(self):
25
26         searcher = IndexSearcher(self.directory, True)
27         t = Term("subject", "ant")
28         query = TermQuery(t)
29         scoreDocs = searcher.search(query, 50).scoreDocs
30         self.assertEqual(1, len(scoreDocs), "JDwA")
31
32         t = Term("subject", "junit")
33         scoreDocs = searcher.search(TermQuery(t), 50).scoreDocs
34         self.assertEqual(2, len(scoreDocs))
35
36         searcher.close()
37
38     def testKeyword(self):
39
40         searcher = IndexSearcher(self.directory, True)
41         t = Term("isbn", "1930110995")
42         query = TermQuery(t)
43         scoreDocs = searcher.search(query, 50).scoreDocs
44         self.assertEqual(1, len(scoreDocs), "JUnit in Action")
45
46     def testQueryParser(self):
47
48         searcher = IndexSearcher(self.directory, True)
49
50         query = QueryParser(Version.LUCENE_CURRENT, "contents",
51                             SimpleAnalyzer()).parse("+JUNIT +ANT -MOCK")
52         scoreDocs = searcher.search(query, 50).scoreDocs
53         self.assertEqual(1, len(scoreDocs))
54         d = searcher.doc(scoreDocs[0].doc)
55         self.assertEqual("Java Development with Ant", d.get("title"))
56
57         query = QueryParser(Version.LUCENE_CURRENT, "contents",
58                             SimpleAnalyzer()).parse("mock OR junit")
59         scoreDocs = searcher.search(query, 50).scoreDocs
60         self.assertEqual(2, len(scoreDocs), "JDwA and JIA")