- Lucene 教程
- Lucene - 首頁
- Lucene - 概述
- Lucene - 環境設定
- Lucene - 第一個應用程式
- Lucene - 索引類
- Lucene - 搜尋類
- Lucene - 索引過程
- Lucene - 索引操作
- Lucene - 搜尋操作
- Lucene - 查詢程式設計
- Lucene - 分析
- Lucene - 排序
- Lucene 有用資源
- Lucene - 快速指南
- Lucene - 有用資源
- Lucene - 討論
Lucene - MatchAllDocsQuery
MatchAllDocsQuery顧名思義,匹配所有文件。
類宣告
以下是org.apache.lucene.search.MatchAllDocsQuery類的宣告
public class MatchAllDocsQuery extends Query
類建構函式
| 序號 | 建構函式 & 描述 |
|---|---|
| 1 | MatchAllDocsQuery() |
| 2 | MatchAllDocsQuery(String normsField) |
類方法
| 序號 | 方法 & 描述 |
|---|---|
| 1 | Weight createWeight(Searcher searcher) 專家:為該查詢構造一個合適的 Weight 實現。 |
| 2 | boolean equals(Object o) |
| 3 | void extractTerms(Set 專家:將此查詢中出現的所有術語新增到術語集中。 |
| 4 | int hashCode() |
| 5 | String toString(String field) 將查詢列印到字串,假設欄位為預設欄位並省略。 |
繼承的方法
此類繼承自以下類 -
- org.apache.lucene.search.Query
- java.lang.Object
用法
private void searchUsingMatchAllDocsQuery(String searchQuery)
throws IOException, ParseException {
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
//create the term query object
Query query = new MatchAllDocsQuery(searchQuery);
//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.print("Score: "+ scoreDoc.score + " ");
System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
}
searcher.close();
}
示例應用程式
讓我們建立一個測試 Lucene 應用程式來測試使用 MatchAllDocsQuery 進行搜尋。
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為LuceneFirstApplication的專案,放在packagecom.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.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
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.searchUsingMatchAllDocsQuery("");
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private void searchUsingMatchAllDocsQuery(String searchQuery)
throws IOException, ParseException {
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
//create the term query object
Query query = new MatchAllDocsQuery(searchQuery);
//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.print("Score: "+ scoreDoc.score + " ");
System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
}
searcher.close();
}
}
資料 & 索引目錄建立
我使用了從 record1.txt 到 record10.txt 的 10 個文字檔案,其中包含學生姓名和其他詳細資訊,並將它們放在E:\Lucene\Data目錄中。 測試資料。應建立索引目錄路徑為E:\Lucene\Index。在Lucene - 索引過程章節中執行索引程式後,您可以在該資料夾中看到建立的索引檔案列表。
執行程式
完成原始碼、原始資料、資料目錄、索引目錄和索引的建立後,您可以繼續編譯和執行程式。為此,請保持 LuceneTester.Java 檔案選項卡處於活動狀態,並使用 Eclipse IDE 中提供的執行選項或使用Ctrl + F11編譯並執行您的LuceneTester應用程式。如果您的應用程式成功執行,它將在 Eclipse IDE 的控制檯中列印以下訊息 -
10 documents found. Time :9ms Score: 1.0 File: E:\Lucene\Data\record1.txt Score: 1.0 File: E:\Lucene\Data\record10.txt Score: 1.0 File: E:\Lucene\Data\record2.txt Score: 1.0 File: E:\Lucene\Data\record3.txt Score: 1.0 File: E:\Lucene\Data\record4.txt Score: 1.0 File: E:\Lucene\Data\record5.txt Score: 1.0 File: E:\Lucene\Data\record6.txt Score: 1.0 File: E:\Lucene\Data\record7.txt Score: 1.0 File: E:\Lucene\Data\record8.txt Score: 1.0 File: E:\Lucene\Data\record9.txt