標籤: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入門建立索引——(一)