Lucene筆記21-Lucene的自定義排序
阿新 • • 發佈:2018-11-04
一、排序介紹
Lucene對文件搜尋完成後,顯示的結果是有一個順序的,如果沒有設定排序規則,那麼這個順序就是按照文件的評分降序排列,至於評分的計算,是一個比較複雜的公式,這裡不先研究了。可是有時候,我們需要根據需求,改變預設的排序規則,這時候就要用到自定義排序啦,下面來看一下自定義排序是怎麼使用的吧。
二、程式碼演示
package com.wsy; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.NumericField; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.*; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.junit.Test; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class CustomSortTest { private static Directory directory; private static IndexReader indexReader; static { try { directory = FSDirectory.open(new File("E:\\Lucene\\IndexLibrary")); indexReader = IndexReader.open(directory); } catch (IOException e) { e.printStackTrace(); } } public void index(boolean update) { IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); if (update) { indexWriter.deleteAll(); } File[] files = new File("E:\\Lucene\\SearchSource").listFiles(); for (File file : files) { Document document = new Document(); document.add(new Field("content", new FileReader(file))); document.add(new Field("fileName", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.add(new NumericField("date", Field.Store.YES, true).setLongValue(file.lastModified())); document.add(new NumericField("size", Field.Store.YES, true).setIntValue((int) (file.length()))); indexWriter.addDocument(document); } } catch (IOException e) { e.printStackTrace(); } finally { if (indexWriter != null) { try { indexWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } public IndexSearcher getIndexSearcher() { try { if (indexReader == null) { indexReader = IndexReader.open(directory); } else { IndexReader temp = IndexReader.openIfChanged(indexReader); if (temp != null) { indexReader.close(); indexReader = temp; } } return new IndexSearcher(indexReader); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public void searcher(String queryString, Sort sort) { try { IndexSearcher indexSearcher = getIndexSearcher(); QueryParser queryParser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35)); Query query = queryParser.parse(queryString); TopDocs topDocs; if (sort != null) { topDocs = indexSearcher.search(query, 100, sort); } else { topDocs = indexSearcher.search(query, 100); } ScoreDoc[] scoreDocs = topDocs.scoreDocs; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (ScoreDoc scoreDoc : scoreDocs) { Document document = indexSearcher.doc(scoreDoc.doc); // 依次輸出文件id,文件得分,文件名字,文件路徑,文件大小,文件修改時間 System.out.println(scoreDoc.doc + "-->" + scoreDoc.score + "-->" + document.get("fileName") + "-->" + document.get("path") + "-->" + document.get("size") + "-->" + simpleDateFormat.format(new Date(Long.valueOf(document.get("date"))))); } indexSearcher.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { CustomSortTest customSortTest = new CustomSortTest(); customSortTest.index(true); // 預設排序(按照評分從高到底降序排列) System.out.println("預設排序(按照評分從高到底降序排列)"); customSortTest.searcher("java", null); // Sort.INDEXORDER是通過doc的id進行升序排序 System.out.println("Sort.INDEXORDER是通過doc的id進行升序排序"); customSortTest.searcher("java", Sort.INDEXORDER); // Sort.RELEVANCE是按照關聯性來排序 System.out.println("Sort.RELEVANCE是按照關聯性來排序"); customSortTest.searcher("java", Sort.RELEVANCE); // 自定義排序,預設是升序,如果需要降序,根據SortField(String field, int type, boolean reverse)構造方法,多傳一個true即可 System.out.println("自定義排序,這裡是按照檔案大小升序排序"); customSortTest.searcher("java", new Sort(new SortField("size", SortField.INT))); System.out.println("自定義排序,這裡是按照檔案日期升序排序"); customSortTest.searcher("java", new Sort(new SortField("date", SortField.LONG))); System.out.println("自定義排序,這裡是按照檔名稱升序排序"); customSortTest.searcher("java", new Sort(new SortField("fileName", SortField.STRING))); // 多欄位排序 System.out.println("自定義多欄位排序,這裡是先按照檔案日期升序排序,再按照大小降序"); customSortTest.searcher("java", new Sort(new SortField("date", SortField.LONG), new SortField("size", SortField.INT, true))); } }