搜索结果排序
3,排序
默认是相关度排序。
也可以按指定的字段排序。
1 package cn.itcast.g_sort; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.apache.lucene.document.Document; 7 import org.apache.lucene.document.Field; 8 import org.apache.lucene.document.Field.Index; 9 import org.apache.lucene.document.Field.Store; 10 import org.apache.lucene.index.IndexWriter; 11 import org.apache.lucene.index.IndexWriter.MaxFieldLength; 12 import org.apache.lucene.queryParser.MultiFieldQueryParser; 13 import org.apache.lucene.queryParser.QueryParser; 14 import org.apache.lucene.search.IndexSearcher; 15 import org.apache.lucene.search.Query; 16 import org.apache.lucene.search.Sort; 17 import org.apache.lucene.search.SortField; 18 import org.apache.lucene.search.TopDocs; 19 import org.apache.lucene.search.highlight.Formatter; 20 import org.apache.lucene.search.highlight.Highlighter; 21 import org.apache.lucene.search.highlight.QueryScorer; 22 import org.apache.lucene.search.highlight.Scorer; 23 import org.apache.lucene.search.highlight.SimpleFragmenter; 24 import org.apache.lucene.search.highlight.SimpleHTMLFormatter; 25 import org.apache.lucene.util.Version; 26 import org.junit.Test; 27 28 import cn.itcast._domain.Article; 29 import cn.itcast._util.ArticleDocumentUtils; 30 import cn.itcast._util.LuceneUtils; 31 32 public class TestApp { 33 34 // 建立索引 35 @Test 36 public void testCreateIndex() throws Exception { 37 // 准备数据 38 Article article = new Article(); 39 article.setId(30); 40 article.setTitle("准备Lucene的开发环境"); 41 article.setContent("如果信息检索系统在用户发出了检索请求后再去互联网上找答案,根本无法在有限的时间内返回结果。"); 42 43 // 放到索引库中 44 // 1, 把Article转为Document 45 Document doc = ArticleDocumentUtils.articleToDocument(article); 46 47 doc.setBoost(0.5F); // 1F表示正常得分,大于1表示高分,小于1表示低分 48 49 // 2, 把Document放到索引库中 50 LuceneUtils.getIndexWriter().addDocument(doc); 51 LuceneUtils.getIndexWriter().commit(); 52 } 53 54 // 搜索 55 @Test 56 public void testSearch() throws Exception { 57 // 准备查询条件 58 String queryString = "lucene"; 59 60 // 执行搜索 61 List<Article> list = new ArrayList<Article>(); 62 63 // 1,把查询字符串转为Query对象(从title和content中查询) 64 QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, new String[] { "title", "content" }, LuceneUtils.getAnalyzer()); 65 Query query = queryParser.parse(queryString); 66 67 // 2,执行查询,得到中间结果 68 IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.getDirectory()); // 指定所用的索引库 69 // TopDocs topDocs = indexSearcher.search(query, 100); // 最多返回前n条结果 70 71 // ========================================================================================== 【创建高亮器】 72 // indexSearcher.search(query, n); 73 // indexSearcher.search(query, filter, n); 74 // indexSearcher.search(query, filter, n, sort); 75 76 // Sort sort = new Sort( new SortField("id", SortField.INT) ); // 按id升序排列 77 Sort sort = new Sort(new SortField("id", SortField.INT, true)); // 按id降序排列(true表示降序,false表示升序) 78 79 TopDocs topDocs = indexSearcher.search(query, null, 100, sort); 80 81 // ========================================================================================== 82 83 // 3,处理结果 84 for (int i = 0; i < topDocs.scoreDocs.length; i++) { 85 float score = topDocs.scoreDocs[i].score; 86 System.out.println("---> score : " + score); 87 88 // 根据编号拿到Document数据 89 int docId = topDocs.scoreDocs[i].doc; // Document的内部编号 90 Document doc = indexSearcher.doc(docId); 91 // 把Document转为Article 92 Article article = ArticleDocumentUtils.documentToArticle(doc); 93 list.add(article); 94 } 95 indexSearcher.close(); 96 97 // 显示结果 98 System.out.println("总结果数:" + list.size()); 99 for (Article a : list) { 100 System.out.println("------------------------------"); 101 System.out.println("id = " + a.getId()); 102 System.out.println("title = " + a.getTitle()); 103 System.out.println("content = " + a.getContent()); 104 } 105 } 106 }