Lucene3.6總結篇

來源:互聯網
上載者:User

1、合并索引庫片段檔案
IndexWriter的optimize()方法已經過時,因為這個方法的效率很低。合并檔案主要是使用IndexWriter的setMergeFactor(int)方法,但是在Lucene3.6版本中,該方法已淘汰,直接使用LogMergePolicy.setMergeFactor(int)方法代替。
當setMergeFactor(int)的參數值較小的時候,建立索引的速度較慢。當參數值較大的時候,建立索引的速度就比較快。大於10適合大量建立索引。


範例程式碼

//合并索引檔案@Testpublic void testMergeFactor() {try {String path = "D:\\LuceneEx\\day03";File file = new File(path);//在當前路徑下建立一個目錄叫indexDir//File indexDir = new File("./indexDir");Directory mDirectory = FSDirectory.open(file);Analyzer mAnalyzer = new IKAnalyzer();IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,mAnalyzer);LogByteSizeMergePolicy mergePolicy = new LogByteSizeMergePolicy();// mergePolicy.setMaxMergeDocs(maxMergeDocs);// mergePolicy.setMaxMergeMB(mb);mergePolicy.setMergeFactor(3); // 達到3個檔案時就和合并config.setMergePolicy(mergePolicy);IndexWriter mIndexWriter = new IndexWriter(mDirectory, config);Book book1 = createBook("Android核心揭秘", "ABC", "2010-07","android 移動開發", 8.9f);Document doc1 = createDocument(book1);Book book2 = createBook("Android多媒體開發", "BCD", "2011-07","android 多媒體", 8.5f);Document doc2 = createDocument(book2);Book book3 = createBook("Android公司專屬應用程式開發", "QAB", "2012-05","android 公司專屬應用程式", 8.2f);Document doc3 = createDocument(book3);doc3.setBoost(1.5F); // boost:設定得分,2F在當前得分的基礎上*2,使得分增高Book book4 = createBook("Android核心剖析", "WPS", "2012-09","android 驅動開發", 9.8f);Document doc4 = createDocument(book4);mIndexWriter.addDocument(doc1);mIndexWriter.addDocument(doc2);mIndexWriter.addDocument(doc3);mIndexWriter.addDocument(doc4);mIndexWriter.close();} catch (CorruptIndexException e) {e.printStackTrace();} catch (LockObtainFailedException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/** * 建立文檔對象的工具方法 *  * @param book * @return */public Document createDocument(Book book) {Document doc = new Document();Field id = new Field("id", book.getId() + "", Store.YES, Index.ANALYZED);Field title = new Field("title", book.getTitle(), Store.YES,Index.ANALYZED);Field author = new Field("author", book.getAuthor(), Store.YES,Index.ANALYZED);Field publishTime = new Field("publishTime", book.getPublishTime(),Store.YES, Index.ANALYZED);Field source = new Field("source", book.getSource(), Store.YES,Index.ANALYZED);Field category = new Field("category", book.getCategory(), Store.YES,Index.ANALYZED);Field reputation = new Field("reputation", book.getReputation() + "",Store.YES, Index.ANALYZED);doc.add(id);doc.add(title);doc.add(author);doc.add(publishTime);doc.add(source);doc.add(category);doc.add(reputation);return doc;}/** * 建立Book對象 *  * @param title * @param author * @param publishTime * @param category * @param reputation * @return */public Book createBook(String title, String author, String publishTime,String category, float reputation) {Random r = new Random();int id = r.nextInt(10000);Book book = new Book();book.setId(id);book.setAuthor(author);book.setTitle(title);book.setCategory(category);book.setPublishTime(publishTime);book.setReputation(reputation);book.setSource("清華大學出版社");return book;}



2、記憶體索引目錄和檔案系統索引目錄結合使用
記憶體索引目錄的操作速度非常快,所以我們在操作索引的時候可以把索引庫從檔案系統載入到記憶體中,操作完成後再寫迴文件系統。
記憶體中的索引檔案寫回到文建系統中的時候,我們需要對索引目錄進行重建。比如原來檔案系統中的索引目錄有10個檔案,載入到記憶體目錄的時候是把10個檔案拷貝一份到記憶體,然後我們添加了一個索引檔案,記憶體中的索引目錄檔案數就變成11個,寫會到檔案系統的時候,記憶體索引目錄檔案數(11個)加上原來檔案系統索引目錄的檔案數(10)就變成21個了,有10個檔案是重複了,所以我們需要刪除原來檔案系統中的索引目錄重新建立。
但是如果索引庫是巨大的,不建議使用,因為所需的記憶體很大。


範例程式碼

@Testpublic void testRAMFSCombination() {try {String path = "D:\\LuceneEx\\day03";File file = new File(path);Directory mDirectory = FSDirectory.open(file);Analyzer mAnalyzer = new IKAnalyzer();// 建立記憶體索引目錄,把檔案系統中的索引庫載入進來RAMDirectory mRamDirectory = new RAMDirectory(mDirectory);IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, mAnalyzer);IndexWriter ramIndexWriter = new IndexWriter(mRamDirectory, config);Book book1 = createBook("Spring企業開發", "QWA", "2012-07","spring web開發", 8.5f);Document doc1 = createDocument(book1);Book book2 = createBook("精通SSH三大架構", "SSH", "2012-11","web開發  SSH", 9.1f);Document doc2 = createDocument(book2);ramIndexWriter.addDocument(doc1);ramIndexWriter.addDocument(doc2);ramIndexWriter.close();//寫回到檔案中IndexWriterConfig fsIndexWriterConfig = new IndexWriterConfig(Version.LUCENE_36,mAnalyzer);//建立新的索引目錄或者覆蓋原來的索引目錄fsIndexWriterConfig.setOpenMode(OpenMode.CREATE);IndexWriter fsIndexWriter = new IndexWriter(mDirectory, fsIndexWriterConfig);//把記憶體中的索引庫寫到檔案系統中fsIndexWriter.addIndexes(mRamDirectory);fsIndexWriter.close();} catch (IOException e) {e.printStackTrace();}}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.