- Lucene 教程
- Lucene - 首頁
- Lucene - 概述
- Lucene - 環境搭建
- Lucene - 第一個應用程式
- Lucene - 索引類
- Lucene - 搜尋類
- Lucene - 索引過程
- Lucene - 索引操作
- Lucene - 搜尋操作
- Lucene - 查詢程式設計
- Lucene - 分析
- Lucene - 排序
- Lucene 有用資源
- Lucene - 快速指南
- Lucene - 有用資源
- Lucene - 討論
Lucene - 更新文件操作
更新文件是索引過程中另一個重要的操作。當已索引的內容被更新且索引失效時,可以使用此操作。此操作也稱為重新索引。
我們將包含欄位的文件更新到IndexWriter,其中IndexWriter用於更新索引。
現在,我們將向您展示一個分步方法,並幫助您理解如何使用基本示例更新文件。
將文件更新到索引
按照以下步驟將文件更新到索引:
步驟 1 - 建立一個方法,從更新的文字檔案更新 Lucene 文件。
private void updateDocument(File file) throws IOException {
Document document = new Document();
//update indexes for file contents
writer.updateDocument(new Term
(LuceneConstants.CONTENTS,
new FileReader(file)),document);
writer.close();
}
建立 IndexWriter
按照以下步驟建立 IndexWriter:
步驟 1 - IndexWriter 類作為核心元件,在索引過程中建立/更新索引。
步驟 2 - 建立 IndexWriter 物件。
步驟 3 - 建立一個 Lucene 目錄,該目錄應指向要儲存索引的位置。
步驟 4 - 使用索引目錄、具有版本資訊和其他必需/可選引數的標準分析器初始化建立的 IndexWriter 物件。
private IndexWriter writer;
public Indexer(String indexDirectoryPath) throws IOException {
//this directory will contain the indexes
Directory indexDirectory =
FSDirectory.open(new File(indexDirectoryPath));
//create the indexer
writer = new IndexWriter(indexDirectory,
new StandardAnalyzer(Version.LUCENE_36),true,
IndexWriter.MaxFieldLength.UNLIMITED);
}
更新文件並開始重新索引過程
以下是更新文件的兩種方法。
updateDocument(Term, Document) - 刪除包含該術語的文件,並使用預設分析器(在建立索引寫入器時指定)新增文件。
updateDocument(Term, Document, Analyzer) - 刪除包含該術語的文件,並使用提供的分析器新增文件。
private void indexFile(File file) throws IOException {
System.out.println("Updating index for "+file.getCanonicalPath());
updateDocument(file);
}
示例應用程式
為了測試索引過程,讓我們建立一個 Lucene 應用程式測試。
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為LuceneFirstApplication的專案,放在packagecom.tutorialspoint.lucene下,如Lucene - 第一個應用程式章節中所述。您也可以使用EJB - 第一個應用程式章節中建立的專案,以便理解索引過程。 |
| 2 | 建立LuceneConstants.java,TextFileFilter.java和Indexer.java,如Lucene - 第一個應用程式章節中所述。保持其餘檔案不變。 |
| 3 | 如下所述建立LuceneTester.java。 |
| 4 | 清理並構建應用程式,以確保業務邏輯按要求工作。 |
LuceneConstants.java
此類用於提供可在示例應用程式中使用的各種常量。
package com.tutorialspoint.lucene;
public class LuceneConstants {
public static final String CONTENTS = "contents";
public static final String FILE_NAME = "filename";
public static final String FILE_PATH = "filepath";
public static final int MAX_SEARCH = 10;
}
TextFileFilter.java
此類用作.txt檔案過濾器。
package com.tutorialspoint.lucene;
import java.io.File;
import java.io.FileFilter;
public class TextFileFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
return pathname.getName().toLowerCase().endsWith(".txt");
}
}
Indexer.java
此類用於索引原始資料,以便我們可以使用 Lucene 庫對其進行搜尋。
package com.tutorialspoint.lucene;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class Indexer {
private IndexWriter writer;
public Indexer(String indexDirectoryPath) throws IOException {
//this directory will contain the indexes
Directory indexDirectory =
FSDirectory.open(new File(indexDirectoryPath));
//create the indexer
writer = new IndexWriter(indexDirectory,
new StandardAnalyzer(Version.LUCENE_36),true,
IndexWriter.MaxFieldLength.UNLIMITED);
}
public void close() throws CorruptIndexException, IOException {
writer.close();
}
private void updateDocument(File file) throws IOException {
Document document = new Document();
//update indexes for file contents
writer.updateDocument(
new Term(LuceneConstants.FILE_NAME,
file.getName()),document);
writer.close();
}
private void indexFile(File file) throws IOException {
System.out.println("Updating index: "+file.getCanonicalPath());
updateDocument(file);
}
public int createIndex(String dataDirPath, FileFilter filter)
throws IOException {
//get all files in the data directory
File[] files = new File(dataDirPath).listFiles();
for (File file : files) {
if(!file.isDirectory()
&& !file.isHidden()
&& file.exists()
&& file.canRead()
&& filter.accept(file)
){
indexFile(file);
}
}
return writer.numDocs();
}
}
LuceneTester.java
此類用於測試 Lucene 庫的索引能力。
package com.tutorialspoint.lucene;
import java.io.IOException;
public class LuceneTester {
String indexDir = "E:\\Lucene\\Index";
String dataDir = "E:\\Lucene\\Data";
Indexer indexer;
public static void main(String[] args) {
LuceneTester tester;
try {
tester = new LuceneTester();
tester.createIndex();
} catch (IOException e) {
e.printStackTrace();
}
}
private void createIndex() throws IOException {
indexer = new Indexer(indexDir);
int numIndexed;
long startTime = System.currentTimeMillis();
numIndexed = indexer.createIndex(dataDir, new TextFileFilter());
long endTime = System.currentTimeMillis();
indexer.close();
}
}
資料和索引目錄建立
在這裡,我們使用了從 record1.txt 到 record10.txt 的 10 個文字檔案,其中包含學生姓名和其他詳細資訊,並將它們放在 E:\Lucene\Data 目錄中。 測試資料。應建立索引目錄路徑為E:\Lucene\Index。執行此程式後,您可以在該資料夾中看到建立的索引檔案列表。
執行程式
完成原始碼、原始資料、資料目錄和索引目錄的建立後,您可以繼續編譯和執行程式。為此,請保持LuceneTester.Java檔案選項卡處於活動狀態,並使用 Eclipse IDE 中提供的“執行”選項,或使用Ctrl + F11編譯和執行LuceneTester應用程式。如果您的應用程式成功執行,它將在 Eclipse IDE 的控制檯中列印以下訊息:
Updating index for E:\Lucene\Data\record1.txt Updating index for E:\Lucene\Data\record10.txt Updating index for E:\Lucene\Data\record2.txt Updating index for E:\Lucene\Data\record3.txt Updating index for E:\Lucene\Data\record4.txt Updating index for E:\Lucene\Data\record5.txt Updating index for E:\Lucene\Data\record6.txt Updating index for E:\Lucene\Data\record7.txt Updating index for E:\Lucene\Data\record8.txt Updating index for E:\Lucene\Data\record9.txt 10 File indexed, time taken: 109 ms
成功執行上述程式後,您的索引目錄中將包含以下內容: