- Lucene 教程
- Lucene - 首頁
- Lucene - 概述
- Lucene - 環境搭建
- Lucene - 第一個應用程式
- Lucene - 索引類
- Lucene - 搜尋類
- Lucene - 索引過程
- Lucene - 索引操作
- Lucene - 搜尋操作
- Lucene - 查詢程式設計
- Lucene - 分析
- Lucene - 排序
- Lucene 有用資源
- Lucene - 快速指南
- Lucene - 有用資源
- Lucene - 討論
Lucene - 字首查詢 (PrefixQuery)
PrefixQuery 用於匹配索引以指定字串開頭的文件。
類宣告
以下是org.apache.lucene.search.PrefixQuery類的宣告:
public class PrefixQuery extends MultiTermQuery
類建構函式
下表顯示了不同的類建構函式:
| 序號 | 建構函式及描述 |
|---|---|
| 1 | PrefixQuery(Term prefix) 構造一個以prefix開頭的術語的查詢。 |
類方法
下表顯示了不同的類方法:
| 序號 | 方法及描述 |
|---|---|
| 1 | boolean equals(Object o) 如果物件o等於this,則返回true。 |
| 2 | protected FilteredTermEnum getEnum(IndexReader reader) 構造要使用的列舉,擴充套件模式術語。 |
| 3 | Term getPrefix() 返回此查詢的字首。 |
| 4 | int hashCode() 返回此物件的雜湊碼值。 |
| 5 | String toString(String field) 列印此查詢的使用者可讀版本。 |
繼承的方法
此類繼承自以下類的方法:
- org.apache.lucene.search.MultiTermQuery
- org.apache.lucene.search.Query
- java.lang.Object
用法
private void searchUsingPrefixQuery(String searchQuery)
throws IOException, ParseException {
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
//create a term to search file name
Term term = new Term(LuceneConstants.FILE_NAME, searchQuery);
//create the term query object
Query query = new PrefixQuery(term);
//do the search
TopDocs hits = searcher.search(query);
long endTime = System.currentTimeMillis();
System.out.println(hits.totalHits +
" documents found. Time :" + (endTime - startTime) + "ms");
for(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = searcher.getDocument(scoreDoc);
System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
}
searcher.close();
}
示例應用程式
讓我們建立一個測試Lucene應用程式來測試使用PrefixQuery進行搜尋。
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為LuceneFirstApplication的專案,放在com.tutorialspoint.lucene包下,如Lucene - 第一個應用程式章節中所述。您也可以使用Lucene - 第一個應用程式章節中建立的專案,以便理解搜尋過程。 |
| 2 | 建立LuceneConstants.java和Searcher.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;
}
Searcher.java
此類用於讀取對原始資料進行的索引,並使用Lucene庫搜尋資料。
package com.tutorialspoint.lucene;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class Searcher {
IndexSearcher indexSearcher;
QueryParser queryParser;
Query query;
public Searcher(String indexDirectoryPath) throws IOException {
Directory indexDirectory =
FSDirectory.open(new File(indexDirectoryPath));
indexSearcher = new IndexSearcher(indexDirectory);
queryParser = new QueryParser(Version.LUCENE_36,
LuceneConstants.CONTENTS,
new StandardAnalyzer(Version.LUCENE_36));
}
public TopDocs search( String searchQuery)
throws IOException, ParseException {
query = queryParser.parse(searchQuery);
return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
}
public TopDocs search(Query query) throws IOException, ParseException {
return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
}
public Document getDocument(ScoreDoc scoreDoc)
throws CorruptIndexException, IOException {
return indexSearcher.doc(scoreDoc.doc);
}
public void close() throws IOException {
indexSearcher.close();
}
}
LuceneTester.java
此類用於測試Lucene庫的搜尋能力。
package com.tutorialspoint.lucene;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.TopDocs;
public class LuceneTester {
String indexDir = "E:\\Lucene\\Index";
String dataDir = "E:\\Lucene\\Data";
Searcher searcher;
public static void main(String[] args) {
LuceneTester tester;
try {
tester = new LuceneTester();
tester.searchUsingPrefixQuery("record1");
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private void searchUsingPrefixQuery(String searchQuery)
throws IOException, ParseException {
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
//create a term to search file name
Term term = new Term(LuceneConstants.FILE_NAME, searchQuery);
//create the term query object
Query query = new PrefixQuery(term);
//do the search
TopDocs hits = searcher.search(query);
long endTime = System.currentTimeMillis();
System.out.println(hits.totalHits +
" documents found. Time :" + (endTime - startTime) + "ms");
for(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = searcher.getDocument(scoreDoc);
System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
}
searcher.close();
}
}
資料和索引目錄建立
我們使用了10個文字檔案,從record1.txt到record10.txt,包含學生姓名和其他詳細資訊,並將它們放在E:\Lucene\Data目錄中。測試資料。應建立索引目錄路徑E:\Lucene\Index。在Lucene - 索引過程章節中執行索引程式後,您可以在該資料夾中看到建立的索引檔案列表。
執行程式
完成原始碼、原始資料、資料目錄、索引目錄和索引的建立後,您可以透過編譯和執行程式繼續操作。為此,請保持LuceneTester.Java檔案選項卡處於活動狀態,並使用Eclipse IDE中提供的“執行”選項,或使用Ctrl + F11編譯並執行您的LuceneTester應用程式。如果您的應用程式成功執行,它將在Eclipse IDE的控制檯中列印以下訊息:
2 documents found. Time :20ms File: E:\Lucene\Data\record1.txt File: E:\Lucene\Data\record10.txt