PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / indexing / IndexTuningDemo.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 import os
16
17 from time import time
18 from datetime import timedelta
19
20 from lucene import \
21      IndexWriter, SimpleAnalyzer, Document, Field, Term, FSDirectory, System
22
23
24 class IndexTuningDemo(object):
25
26     def main(cls, argv):
27
28         if len(argv) < 5:
29             print "Usage: python IndexTuningDemo.py <numDocs> <mergeFactor> <maxMergeDocs> <maxBufferedDocs>"
30             return
31             
32         docsInIndex  = int(argv[1])
33
34         # create an index called 'index-dir' in a temp directory
35         indexDir = os.path.join(System.getProperty('java.io.tmpdir', 'tmp'),
36                                 'index-dir')
37         dir = FSDirectory.getDirectory(indexDir, True)
38         analyzer = SimpleAnalyzer()
39         writer = IndexWriter(dir, analyzer, True)
40
41         # set variables that affect speed of indexing
42         writer.setMergeFactor(int(argv[2]))
43         writer.setMaxMergeDocs(int(argv[3]))
44         writer.setMaxBufferedDocs(int(argv[4]))
45         # writer.infoStream = System.out
46
47         print "Merge factor:  ", writer.getMergeFactor()
48         print "Max merge docs:", writer.getMaxMergeDocs()
49         print "Max buffered docs:", writer.getMaxBufferedDocs()
50
51         start = time()
52         for i in xrange(docsInIndex):
53             doc = Document()
54             doc.add(Field("fieldname", "Bibamus",
55                           Field.Store.YES, Field.Index.TOKENIZED))
56             writer.addDocument(doc)
57
58         writer.close()
59         print "Time: ", timedelta(seconds=time() - start)
60
61     main = classmethod(main)