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 # ====================================================================
17 from unittest import TestCase
20 SimpleFSDirectory, System, File, \
21 Document, Field, SimpleAnalyzer, IndexWriter, IndexReader
24 class BaseIndexingTestCase(TestCase):
27 unindexed = ["Netherlands", "Italy"]
28 unstored = ["Amsterdam has lots of bridges",
29 "Venice has lots of canals"]
30 text = ["Amsterdam", "Venice"]
34 indexDir = os.path.join(System.getProperty('java.io.tmpdir', 'tmp'),
37 self.dir = SimpleFSDirectory(File(indexDir))
38 self.addDocuments(self.dir)
42 for dir, dirnames, filenames in os.walk(dir):
43 for filename in filenames:
44 os.remove(os.path.join(dir, filename))
45 for dirname in dirnames:
46 os.rmdir(os.path.join(dir, dirname))
48 def addDocuments(self, dir):
50 writer = IndexWriter(dir, self.getAnalyzer(), True,
51 IndexWriter.MaxFieldLength.UNLIMITED)
52 writer.setUseCompoundFile(self.isCompound())
54 for i in xrange(len(self.keywords)):
56 doc.add(Field("id", self.keywords[i],
57 Field.Store.YES, Field.Index.NOT_ANALYZED))
58 doc.add(Field("country", self.unindexed[i],
59 Field.Store.YES, Field.Index.NO))
60 doc.add(Field("contents", self.unstored[i],
61 Field.Store.NO, Field.Index.ANALYZED))
62 doc.add(Field("city", self.text[i],
63 Field.Store.YES, Field.Index.ANALYZED))
64 writer.addDocument(doc)
69 def getAnalyzer(self):
71 return SimpleAnalyzer()
77 def testIndexWriter(self):
79 writer = IndexWriter(self.dir, self.getAnalyzer(), False,
80 IndexWriter.MaxFieldLength.UNLIMITED)
81 self.assertEqual(len(self.keywords), writer.numDocs())
84 def testIndexReader(self):
86 reader = IndexReader.open(self.dir, True)
87 self.assertEqual(len(self.keywords), reader.maxDoc())
88 self.assertEqual(len(self.keywords), reader.numDocs())