- Lucene 教程
- Lucene - 首頁
- Lucene - 概述
- Lucene - 環境搭建
- Lucene - 第一個應用程式
- Lucene - 索引類
- Lucene - 搜尋類
- Lucene - 索引過程
- Lucene - 索引操作
- Lucene - 搜尋操作
- Lucene - 查詢程式設計
- Lucene - 分析
- Lucene - 排序
- Lucene 有用資源
- Lucene - 快速指南
- Lucene - 有用資源
- Lucene - 討論
Lucene - 索引過程
索引過程是 Lucene 提供的核心功能之一。下圖說明了索引過程和類的使用。IndexWriter 是索引過程中最重要和核心的元件。
我們將包含欄位 (Field) 的文件 (Document) 新增到 IndexWriter 中,IndexWriter 使用分析器 (Analyzer) 分析文件,然後根據需要建立/開啟/編輯索引,並將它們儲存/更新到目錄 (Directory) 中。IndexWriter 用於更新或建立索引,它不用於讀取索引。
現在,我們將透過一個基本的示例,逐步引導您瞭解索引過程。
建立文件
建立一個方法,從文字檔案獲取 Lucene 文件。
建立各種型別的欄位,它們是鍵值對,鍵是名稱,值是要索引的內容。
設定欄位是否要被分析。在本例中,只有內容需要被分析,因為它可能包含諸如 a、am、are、an 等資料,這些資料在搜尋操作中不需要。
將新建立的欄位新增到文件物件中,並將其返回給呼叫方法。
private Document getDocument(File file) throws IOException {
Document document = new Document();
//index file contents
Field contentField = new Field(LuceneConstants.CONTENTS,
new FileReader(file));
//index file name
Field fileNameField = new Field(LuceneConstants.FILE_NAME,
file.getName(),
Field.Store.YES,Field.Index.NOT_ANALYZED);
//index file path
Field filePathField = new Field(LuceneConstants.FILE_PATH,
file.getCanonicalPath(),
Field.Store.YES,Field.Index.NOT_ANALYZED);
document.add(contentField);
document.add(fileNameField);
document.add(filePathField);
return document;
}
建立 IndexWriter
IndexWriter 類充當核心元件,在索引過程中建立/更新索引。請按照以下步驟建立 IndexWriter:
步驟 1 - 建立 IndexWriter 物件。
步驟 2 - 建立一個 Lucene 目錄,它應該指向儲存索引的位置。
步驟 3 - 使用索引目錄、具有版本資訊和其他必需/可選引數的標準分析器初始化建立的 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);
}
開始索引過程
以下程式顯示瞭如何啟動索引過程:
private void indexFile(File file) throws IOException {
System.out.println("Indexing "+file.getCanonicalPath());
Document document = getDocument(file);
writer.addDocument(document);
}
示例應用程式
為了測試索引過程,我們需要建立一個 Lucene 應用程式測試。
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為LuceneFirstApplication 的專案,放在com.tutorialspoint.lucene 包下,如Lucene - 第一個應用程式章節中所述。您也可以使用Lucene - 第一個應用程式章節中建立的專案,來理解索引過程。 |
| 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 Document getDocument(File file) throws IOException {
Document document = new Document();
//index file contents
Field contentField = new Field(LuceneConstants.CONTENTS,
new FileReader(file));
//index file name
Field fileNameField = new Field(LuceneConstants.FILE_NAME,
file.getName(),
Field.Store.YES,Field.Index.NOT_ANALYZED);
//index file path
Field filePathField = new Field(LuceneConstants.FILE_PATH,
file.getCanonicalPath(),
Field.Store.YES,Field.Index.NOT_ANALYZED);
document.add(contentField);
document.add(fileNameField);
document.add(filePathField);
return document;
}
private void indexFile(File file) throws IOException {
System.out.println("Indexing "+file.getCanonicalPath());
Document document = getDocument(file);
writer.addDocument(document);
}
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();
System.out.println(numIndexed+" File indexed, time taken: "
+(endTime-startTime)+" ms");
}
}
資料和索引目錄建立
我們使用了從 record1.txt 到 record10.txt 的 10 個文字檔案,包含學生姓名和其他詳細資訊,並將它們放在E:\Lucene\Data 目錄中。測試資料。應建立索引目錄路徑為E:\Lucene\Index。執行此程式後,您可以在該資料夾中看到建立的索引檔案列表。
執行程式
完成原始碼、原始資料、資料目錄和索引目錄的建立後,您可以透過編譯和執行程式繼續操作。為此,請保持 LuceneTester.Java 檔案選項卡處於活動狀態,並使用 Eclipse IDE 中提供的執行選項,或使用Ctrl + F11 來編譯和執行您的LuceneTester 應用程式。如果您的應用程式成功執行,它將在 Eclipse IDE 的控制檯中列印以下訊息:
Indexing E:\Lucene\Data\record1.txt Indexing E:\Lucene\Data\record10.txt Indexing E:\Lucene\Data\record2.txt Indexing E:\Lucene\Data\record3.txt Indexing E:\Lucene\Data\record4.txt Indexing E:\Lucene\Data\record5.txt Indexing E:\Lucene\Data\record6.txt Indexing E:\Lucene\Data\record7.txt Indexing E:\Lucene\Data\record8.txt Indexing E:\Lucene\Data\record9.txt 10 File indexed, time taken: 109 ms
成功執行程式後,您的索引目錄中將包含以下內容: