lucene入門建立索引——(一)

來源:互聯網
上載者:User

標籤:div   min   分析器   sasl   sbt   rtc   acpi   eal   ifd   

 1.程式宏觀結構圖

 

2.建立索引過程

 

 

3.代碼實現

建立索引庫:

1)  建立JavaBean對象

2)  建立Docment對象

3)  將JavaBean對象所有的屬性值,均放到Document對象中去,屬性名稱可以和JavaBean相同或不同

4)  建立IndexWriter對象

5)  將Document對象通過IndexWriter對象寫入索引庫中

6)  關閉IndexWriter對象

 

Jar包:

 

代碼:

 1 // 建立索引 2     @Test 3     public void testIndex() throws Exception { 4         // 第一步:建立一個java工程,並匯入jar包。 5         // 第二步:建立一個indexwriter對象。 6         Directory directory = FSDirectory.open(new File("E:\\lucene1\\index")); 7         // Directory directory = new RAMDirectory();//儲存索引到記憶體中 (記憶體索引庫) 8 //        Analyzer analyzer = new StandardAnalyzer();// 官方推薦 9         Analyzer analyzer = new IKAnalyzer();// 官方推薦10         IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);11         IndexWriter indexWriter = new IndexWriter(directory, config);12         // 1)指定索引庫的存放位置Directory對象13         // 2)指定一個分析器,對文檔內容進行分析。14         // 第三步:建立field對象,將field添加到document對象中。15         File f = new File("E:\\lucene1\\searchfiles");16         File[] listFiles = f.listFiles();17         for (File file : listFiles) {18             // 第三步:建立document對象。19             Document document = new Document();20             // 檔案名稱21             String file_name = file.getName();22             Field fileNameField = new TextField("fileName", file_name, Store.YES);23             // 檔案大小24             long file_size = FileUtils.sizeOf(file);25             Field fileSizeField = new LongField("fileSize", file_size, Store.YES);26             // 檔案路徑27             String file_path = file.getPath();28             Field filePathField = new StoredField("filePath", file_path);29             // 檔案內容30             String file_content = FileUtils.readFileToString(file);31             Field fileContentField = new TextField("fileContent", file_content, Store.YES);32 33             document.add(fileNameField);34             document.add(fileSizeField);35             document.add(filePathField);36             document.add(fileContentField);37             // 第四步:使用indexwriter對象將document對象寫入索引庫,此過程進行索引建立。並將索引和document對象寫入索引庫。38             indexWriter.addDocument(document);39 40         }41         // 第五步:關閉IndexWriter對象。42         indexWriter.close();43     }

結果:至此建立索引完成,以後的搜尋靠他們了。

4.luke視覺化檢視查看索引

 

 

 

lucene入門建立索引——(一)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.