MatchAllDocsQuery作為顧名思義匹配的所有檔案。
以下是org.apache.lucene.search.MatchAllDocsQuery類的宣告:
public class MatchAllDocsQuery extends Query
S.N. | 建構函式和說明 |
---|---|
1 | MatchAllDocsQuery() |
2 | MatchAllDocsQuery(String normsField) |
S.N. | 方法 & 描述 |
---|---|
1 |
Weight createWeight(Searcher searcher) 構造一個適當的權重實現這個查詢。 |
2 | boolean equals(Object o) |
3 |
void extractTerms(Set<Term> terms) 新增在此查詢到設定的條款發生的所有條款。 |
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.yiibai.lucene下的Lucene用於解釋 Lucene應用程式理解搜尋過程。 |
2 | 建立LuceneConstants.java和Searcher.java作為Lucene的解釋- 第一應用程式一章。保持其它檔案不變。 |
3 | 建立LuceneTester.java如下所述。 |
4 | 清理和構建應用程式,以確保業務邏輯按要求工作。 |
LuceneConstants.java
這個類是用來提供可應用於範例應用程式中使用的各種常數。
package com.yiibai.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.yiibai.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.yiibai.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的檔案中包含簡單的名稱以及學生的其他細節,並把它們放在目錄E:LuceneData。這是測試資料。索引目錄路徑應建立為E:LuceneIndex。期間,執行 Lucene索引程式後- 索引過程中,可以看到該檔案夾中建立的索引檔案的列表。
一旦建立源,創造了原始資料,資料目錄,索引目錄和索引完成後,已經準備好這一步是編譯和執行程式。要做到這一點,請LuceneTester.Java檔案索引標籤中使用Eclipse IDE可使用Run選項,或使用Ctrl+ F11來編譯和執行應用程式LuceneTester。如果您的應用程式一切正常,這將在Eclipse IDE的控制台列印以下訊息:
10 documents found. Time :9ms Score: 1.0 File: E:LuceneDataecord1.txt Score: 1.0 File: E:LuceneDataecord10.txt Score: 1.0 File: E:LuceneDataecord2.txt Score: 1.0 File: E:LuceneDataecord3.txt Score: 1.0 File: E:LuceneDataecord4.txt Score: 1.0 File: E:LuceneDataecord5.txt Score: 1.0 File: E:LuceneDataecord6.txt Score: 1.0 File: E:LuceneDataecord7.txt Score: 1.0 File: E:LuceneDataecord8.txt Score: 1.0 File: E:LuceneDataecord9.txt