lucene_indexWriter说明、索引库优化
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                lucene_indexWriter说明、索引库优化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                IndexWriter
Hibernate的SessionFactory在Hibernate中。一般保持一個數據庫就僅僅有一個SessionFactory。由于在SessionFactory中維護二級緩存,而SessionFactory又是線程安全的。
所以SessionFactory是共享的。
lucene的IndexWriter假設同一時候在一個索引庫中同一時候建立兩個IndexWriter,比如: IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);IndexWriter indexWriter2 = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
這種代碼會出現異常
而lucene的文件夾結構:
.會出現write.lock這個文件。
由于當一個IndexWriter在進行讀索引庫操作的時候,lucene會為索引庫。以防止其它IndexWriter訪問索引庫而導致數據不一致,直到IndexWriter關閉為止。
public class LuceneIndexWriter {private static IndexWriter indexWriter = null;public static IndexWriter getIndexWriter() {if (indexWriter == null) {try {indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer, MaxFieldLength.LIMITED);} catch (CorruptIndexException e) {e.printStackTrace();} catch (LockObtainFailedException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}return indexWriter;}public static void close() {if (indexWriter != null) {try {indexWriter.close();} catch (CorruptIndexException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}} else {System.out.println("indexWriter為空。不能關閉!
"); } } }
注:這里用單例模式做比較好。 /*** 1、索引庫的增、刪、改是由indexWriter來操作的* 2、同一個時刻內。同一個索引庫。僅僅能同意一個indexWriter操作* 3、當IndexWriter創建完畢以后。indexwriter所指向的索引庫就被占據了,僅僅有當indexWriter.close時。才干釋放鎖的資源* 4、當一個新的indexWriter想擁有索引庫時。原來的indexWriter必須釋放鎖* 5、僅僅要索引庫中存在write.lock文件。說明上鎖了* 6、indexWriter.close有兩層含義:* * 關閉IO資源* * 釋放鎖* @author Administrator**/ public class IndexWriterTest {@Testpublic void testIndexWriter() throws Exception{IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);indexWriter.close();IndexWriter indexWriter2 = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);} }索引庫的優化
當運行創建索引多次時。索引庫的文件如圖所看到的:(索引里內容是一樣的) 從圖中能夠看出來,每運行一次就生成一個cfs文件。當運行delete操作時,會生成如圖所看到的的結構:從圖中能夠看出來。lucene在運行刪除的時候,是先把要刪除的元素形成了一個文件del文件,然后再和cfs文件進行整合得出最后結果。
結論:假設添加、刪除重復操作非常多次,就會造成文件大量添加。這樣檢索的速度也會下降。所以我們有必要去優化索引結構。使文件的結構發生改變從而提高效率。
手動合并文件
合并多個小文件為一個大文件,降低IO操作:在indexWriter.close();前加上
indexWriter.optimize();
就可以
在運行完上述代碼后,索引庫的結構為: 能夠看出把該合并的項都合并了。把del文件徹底所有刪除掉了。
自己主動合并文件
indexWriter.setMergeFactor(3);當cfs文件的數量為3個時,這是會自己主動合并成一個文件。
假設沒有設置數量,默認情況下為10;
轉載于:https://www.cnblogs.com/blfshiye/p/5272222.html
總結
以上是生活随笔為你收集整理的lucene_indexWriter说明、索引库优化的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: UVALive 6525 Attacki
- 下一篇: 写博客和生产api的工具
