@@ -47,7 +47,8 @@ public class FileSearchToolService {
4747
4848 private static IndexWriter indexWriter = null ;
4949
50- // private static RAMDirectory directory;
50+ // private static RAMDirectory directory;
51+ private static int commonIndex = 0 ;
5152
5253 static {
5354 File searchIndexDirFile = new File (searchIndexDir );
@@ -70,6 +71,14 @@ public static void getIndexWriter(Directory directory) throws IOException {
7071 IndexWriterConfig indexWriterConfig = new IndexWriterConfig (analyzer );
7172 indexWriterConfig .setRAMBufferSizeMB (Runtime .getRuntime ().totalMemory () / 1024 / 1024 / 4 );
7273// indexWriterConfig.setMaxBufferedDocs(2000);
74+ // LogMergePolicy mergePolicy = new LogByteSizeMergePolicy();
75+ // //设置segment添加文档(Document)时的合并频率 //值较小,建立索引的速度就较慢 //值较大,建立索引的速度就较快,>10适合批量建立索引
76+ // mergePolicy.setMergeFactor(50);
77+ // //设置segment最大合并文档(Document)数
78+ // //值较小有利于追加索引的速度
79+ // //值较大,适合批量建立索引和更快的搜索
80+ // mergePolicy.setMaxMergeDocs(5000);
81+ // indexWriterConfig.setMergePolicy(mergePolicy);
7382 //创建索引写入对象
7483 indexWriter = new IndexWriter (directory , indexWriterConfig );
7584 }
@@ -176,6 +185,7 @@ public void addSearchIndexFile(Path path) {
176185 log .warn ("添加索引失败:" , e );
177186 }
178187 }
188+ indexWriter .commit ();
179189 } catch (Exception e ) {
180190 log .warn ("获取失败:" , e );
181191 }
@@ -194,8 +204,11 @@ public void addIndexDocument(File file) throws Exception {
194204 doc .add (new StringField ("isHidden" , String .valueOf (file .isHidden ()), Field .Store .NO ));
195205 doc .add (new StringField ("isDirectory" , String .valueOf (file .isDirectory ()), Field .Store .NO ));
196206 indexWriter .updateDocument (new Term ("absolutePath" , file .getAbsolutePath ()), doc );
207+
197208// indexWriter.addDocument(doc);
198- indexWriter .commit ();
209+ if (commonIndex ++ % 50 == 0 ) {
210+ indexWriter .commit ();
211+ }
199212 }
200213
201214 public void autoRefreshIndexAction () {
0 commit comments