- Lucene 教程
- Lucene - 首頁
- Lucene - 概述
- Lucene - 環境搭建
- Lucene - 第一個應用程式
- Lucene - 索引類
- Lucene - 搜尋類
- Lucene - 索引過程
- Lucene - 索引操作
- Lucene - 搜尋操作
- Lucene - 查詢程式設計
- Lucene - 分析
- Lucene - 排序
- Lucene 有用資源
- Lucene - 快速指南
- Lucene - 有用資源
- Lucene - 討論
Lucene - 欄位選項
欄位是索引過程中最重要的單元。它是包含待索引內容的實際物件。當我們新增欄位時,Lucene 使用欄位選項提供了對欄位的大量控制,這些選項說明了欄位的可搜尋程度。
我們將包含欄位的文件新增到 IndexWriter 中,其中 IndexWriter 用於更新或建立索引。
現在,我們將向您展示一個逐步的方法,並幫助您使用一個基本示例理解各種欄位選項。
各種欄位選項
以下是各種欄位選項:
Index.ANALYZED - 在此,我們先分析,然後進行索引。這用於普通文字索引。分析器會將欄位的值分解成標記流,每個標記都是單獨可搜尋的。
Index.NOT_ANALYZED - 在此,我們不分析,但進行索引。這用於完整文字索引。例如,人名、URL 等。
Index.ANALYZED_NO_NORMS - 這是Index.ANALYZED的一個變體。分析器會將欄位的值分解成標記流,每個標記都是單獨可搜尋的。但是,NORMS 不儲存在索引中。NORMS 用於增強搜尋,這通常會消耗大量記憶體。
Index.Index.NOT_ANALYZED_NO_NORMS - 這是Index.NOT_ANALYZED的一個變體。索引已完成,但 NORMS 未儲存在索引中。
Index.NO - 欄位值不可搜尋。
欄位選項的使用
以下是欄位選項的不同使用方法:
建立一個從文字檔案獲取 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;
}
示例應用程式
為了測試索引過程,我們需要建立一個 Lucene 應用程式測試。
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為LuceneFirstApplication的專案,放在com.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 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");
}
}
資料和索引目錄建立
我們使用了 10 個文字檔案,從 record1.txt 到 record10.txt,包含學生姓名和其他詳細資訊,並將它們放在 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
成功執行程式後,您的索引目錄中將包含以下內容: