Lucene MatchAllDocsQuery類
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:LuceneData ecord1.txt Score: 1.0 File: E:LuceneData ecord10.txt Score: 1.0 File: E:LuceneData ecord2.txt Score: 1.0 File: E:LuceneData ecord3.txt Score: 1.0 File: E:LuceneData ecord4.txt Score: 1.0 File: E:LuceneData ecord5.txt Score: 1.0 File: E:LuceneData ecord6.txt Score: 1.0 File: E:LuceneData ecord7.txt Score: 1.0 File: E:LuceneData ecord8.txt Score: 1.0 File: E:LuceneData ecord9.txt