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
18 SimpleAnalyzer, Document, TermQuery, QueryParser, IndexSearcher, Term, \
22 class BasicSearchingTest(LiaTestCase):
26 searcher = IndexSearcher(self.directory, True)
27 t = Term("subject", "ant")
29 scoreDocs = searcher.search(query, 50).scoreDocs
30 self.assertEqual(1, len(scoreDocs), "JDwA")
32 t = Term("subject", "junit")
33 scoreDocs = searcher.search(TermQuery(t), 50).scoreDocs
34 self.assertEqual(2, len(scoreDocs))
38 def testKeyword(self):
40 searcher = IndexSearcher(self.directory, True)
41 t = Term("isbn", "1930110995")
43 scoreDocs = searcher.search(query, 50).scoreDocs
44 self.assertEqual(1, len(scoreDocs), "JUnit in Action")
46 def testQueryParser(self):
48 searcher = IndexSearcher(self.directory, True)
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"))
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")