Lucene排序
在本章中,我們將研究的排序順序默認情況下,在給出的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(); }
選擇IndexOrder排序方式
這被分揀使用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:LuceneData ecord3.txt Score: 0.790779 File: E:LuceneData ecord1.txt Score: 0.790779 File: E:LuceneData ecord2.txt Score: 0.790779 File: E:LuceneData ecord4.txt Score: 0.790779 File: E:LuceneData ecord5.txt Score: 0.790779 File: E:LuceneData ecord6.txt Score: 0.790779 File: E:LuceneData ecord7.txt Score: 0.790779 File: E:LuceneData ecord8.txt Score: 0.790779 File: E:LuceneData ecord9.txt Score: 0.2635932 File: E:LuceneData ecord10.txt 10 documents found. Time :0ms Score: 0.790779 File: E:LuceneData ecord1.txt Score: 0.2635932 File: E:LuceneData ecord10.txt Score: 0.790779 File: E:LuceneData ecord2.txt Score: 1.3179655 File: E:LuceneData ecord3.txt Score: 0.790779 File: E:LuceneData ecord4.txt Score: 0.790779 File: E:LuceneData ecord5.txt Score: 0.790779 File: E:LuceneData ecord6.txt Score: 0.790779 File: E:LuceneData ecord7.txt Score: 0.790779 File: E:LuceneData ecord8.txt Score: 0.790779 File: E:LuceneData ecord9.txt