在本章中,我們將研究的排序順序預設情況下,在給出的Lucene搜尋結果或可根據需要進行操作。
這是使用Lucene的預設排序方式。 Lucene通過在頂部的最相關的命中提供的結果。
private void sortUsingRelevance(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 FuzzyQuery(term); searcher.setDefaultFieldSortScoring(true, false); //do the search TopDocs hits = searcher.search(query,Sort.RELEVANCE); 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模式,其中索引第一文件示出第一搜尋結果中。
private void sortUsingIndex(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 FuzzyQuery(term); searcher.setDefaultFieldSortScoring(true, false); //do the search TopDocs hits = searcher.search(query,Sort.INDEXORDER); 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的應用程式來測試分選過程。
步驟 | 描述 |
---|---|
1 | 建立下名稱為LuceneFirstApplication的一個專案作為解釋Lucene的應用在包packagecom.yiibai.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.Sort; 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 TopDocs search(Query query,Sort sort) throws IOException, ParseException{ return indexSearcher.search(query, LuceneConstants.MAX_SEARCH,sort); } public void setDefaultFieldSortScoring(boolean doTrackScores, boolean doMaxScores){ indexSearcher.setDefaultFieldSortScoring( doTrackScores,doMaxScores); } 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.FuzzyQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.Sort; import org.apache.lucene.search.TopDocs; public class LuceneTester { String indexDir = "E:\Lucene\Index"; String dataDir = "E:\Lucene\Data"; Indexer indexer; Searcher searcher; public static void main(String[] args) { LuceneTester tester; try { tester = new LuceneTester(); tester.sortUsingRelevance("cord3.txt"); tester.sortUsingIndex("cord3.txt"); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } private void sortUsingRelevance(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 FuzzyQuery(term); searcher.setDefaultFieldSortScoring(true, false); //do the search TopDocs hits = searcher.search(query,Sort.RELEVANCE); 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(); } private void sortUsingIndex(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 FuzzyQuery(term); searcher.setDefaultFieldSortScoring(true, false); //do the search TopDocs hits = searcher.search(query,Sort.INDEXORDER); 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 :31ms Score: 1.3179655 File: E:LuceneDataecord3.txt Score: 0.790779 File: E:LuceneDataecord1.txt Score: 0.790779 File: E:LuceneDataecord2.txt Score: 0.790779 File: E:LuceneDataecord4.txt Score: 0.790779 File: E:LuceneDataecord5.txt Score: 0.790779 File: E:LuceneDataecord6.txt Score: 0.790779 File: E:LuceneDataecord7.txt Score: 0.790779 File: E:LuceneDataecord8.txt Score: 0.790779 File: E:LuceneDataecord9.txt Score: 0.2635932 File: E:LuceneDataecord10.txt 10 documents found. Time :0ms Score: 0.790779 File: E:LuceneDataecord1.txt Score: 0.2635932 File: E:LuceneDataecord10.txt Score: 0.790779 File: E:LuceneDataecord2.txt Score: 1.3179655 File: E:LuceneDataecord3.txt Score: 0.790779 File: E:LuceneDataecord4.txt Score: 0.790779 File: E:LuceneDataecord5.txt Score: 0.790779 File: E:LuceneDataecord6.txt Score: 0.790779 File: E:LuceneDataecord7.txt Score: 0.790779 File: E:LuceneDataecord8.txt Score: 0.790779 File: E:LuceneDataecord9.txt