lucene排序---相关度与其他组合排序

原先一直有这个概念,就是在lucene中相关度排序和其他排序是不能组合使用;但是一直怀疑这个观念,我记得是书上看到的(网上也有人这么说),但是有不能确定。

理论上来说,lucene是可以做到这样组合排序的;所以我就试一下了;


[java]  view plain copy
  1. package com.search5173.lucene.demo.sort;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import org.apache.lucene.analysis.Analyzer;  
  8. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  9. import org.apache.lucene.document.Document;  
  10. import org.apache.lucene.document.Field;  
  11. import org.apache.lucene.index.CorruptIndexException;  
  12. import org.apache.lucene.index.IndexWriter;  
  13. import org.apache.lucene.index.Term;  
  14. import org.apache.lucene.queryParser.MultiFieldQueryParser;  
  15. import org.apache.lucene.queryParser.ParseException;  
  16. import org.apache.lucene.queryParser.QueryParser;  
  17. import org.apache.lucene.search.IndexSearcher;  
  18. import org.apache.lucene.search.Query;  
  19. import org.apache.lucene.search.ScoreDoc;  
  20. import org.apache.lucene.search.Sort;  
  21. import org.apache.lucene.search.SortField;  
  22. import org.apache.lucene.search.TermQuery;  
  23. import org.apache.lucene.search.TopDocs;  
  24. import org.apache.lucene.store.Directory;  
  25. import org.apache.lucene.store.LockObtainFailedException;  
  26. import org.apache.lucene.store.RAMDirectory;  
  27. import org.apache.lucene.util.Version;  
  28.   
  29. public class TestLuceneSort {  
  30.   
  31.     public static void main(String args[]) throws CorruptIndexException, LockObtainFailedException, IOException, ParseException {  
  32.                 try {  
  33.                     TestLuceneSort.doSearch("天使"1100);  
  34.                 } catch(Exception ex) {  
  35.                 // TODO  
  36.                 }  
  37.         }  
  38.       
  39.         public static void doSearch(String keyword, int page, int pageSize) throws CorruptIndexException, LockObtainFailedException, IOException, ParseException   
  40.         {  
  41.             Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);// Store the index in memory:  
  42.             Directory directory = new RAMDirectory();  
  43.             // To store an index on disk, use this instead:  
  44.             //Directory directory = FSDirectory.open("/tmp/testindex");  
  45.             IndexWriter iwriter = new IndexWriter(directory, analyzer, true,  
  46.             new IndexWriter.MaxFieldLength(25000));Post post = new Post();  
  47.               
  48.             post.setPostId(10001);  
  49.             post.setTitle("校内八大俗天使");  
  50.             post.setContent(" 有网友总结校内网类网站的八大俗: 生活常识化妆技,十二星座小秘密; 不看后悔成功录,论文大全雅思题。 恋爱金句传送门,男默女泪蛋疼文; 读到哪句 心痛了?不顶不是中国人。 相关日志推荐不成熟男人的十个标志中国大学排行榜2010版金融危机十项注意2008十大网络公敌将从我们生活里 ");  
  51.             iwriter.addDocument(TestLuceneSort.buildDocument(post));  
  52.               
  53.             post.setPostId(10002);  
  54.             post.setTitle("冷组天使天使天使天使总是能出这么伟大的冷笑话天使");  
  55.             post.setContent(" 鹅李卡|蘑菇蘑菇分享 天使我的某位友邻说:据说大地震前有三个明显征兆: 1.井水异常;2.牲畜反应异常;3.专家出来辟谣。 但是细心网友指出,第二条 和第三条重复了。 然后底下有人回应说:可能是喝了异常的井水。。。 其实专家的嘴就像屁股一样,有图有真相!!!!");  
  56.             //post.setContent(" 校内:之前已经有很多关于“天使”的文章了,这些小家伙一举一动往往最能打动我们。今天又收集了一些“天使的眼神”与大家分享,那清澈的眼神是否会直达你 的心底,让你的心也一片清澈?   另外,由于图片数量较多,就作两期发布,希望大家喜欢……");  
  57.             iwriter.addDocument(TestLuceneSort.buildDocument(post));  
  58.               
  59.             post.setPostId(10003);  
  60.             post.setTitle("冷组天使天使天使天使天使天使天使天使天使天使天使天使天使总是能出这么伟大的冷笑话天使");  
  61.             post.setContent(" 鹅李卡|蘑菇蘑菇分享 天使我的某位友邻说:据说大地震前有三个明显征兆: 1.井水异常;2.牲畜反应异常;3.专家出来辟谣。 但是细心网友指出,第二条 和第三条重复了。 然后底下有人回应说:可能是喝了异常的井水。。。 其实专家的嘴就像屁股一样,有图有真相!!!!");  
  62.             iwriter.addDocument(TestLuceneSort.buildDocument(post));// Now search the index:  
  63.               
  64.               
  65.             post.setPostId(10000);  
  66.             post.setTitle("冷组天使天使天使天使总是能出这么伟大的冷笑话天使");  
  67.             post.setContent(" 鹅李卡|蘑菇蘑菇分享 天使我的某位友邻说:据说大地震前有三个明显征兆: 1.井水异常;2.牲畜反应异常;3.专家出来辟谣。 但是细心网友指出,第二条 和第三条重复了。 然后底下有人回应说:可能是喝了异常的井水。。。 其实专家的嘴就像屁股一样,有图有真相!!!!");  
  68.             iwriter.addDocument(TestLuceneSort.buildDocument(post));  
  69.               
  70.             iwriter.close();  
  71.             IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true  
  72.           
  73.             Map<String, Float> boosts = new HashMap<String, Float>();  
  74.             boosts.put("title"3.0f);  
  75.             boosts.put("content"1.1f);  
  76.             QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_CURRENT, new String[]{"title","content"}, analyzer,null);  
  77.               
  78.             Query query = parser.parse(keyword);  
  79.               
  80.             Sort sort1 = new Sort(new SortField[]  
  81.                                                         {  
  82.                                                                //如果两个都没有注释,那么就是按照先按照相关度,然后按照id排序(相关度相同的情况下)  
  83.                                                                 SortField.FIELD_SCORE,//注释掉这个就是按照id排序  
  84.                                                                 new SortField("postId", SortField.INT, false)//注释掉这个就是按照相关度排序  
  85.                                                         }  
  86.                                                 );  
  87.               
  88.               
  89.               
  90.             ScoreDoc[] hits = isearcher.search(query, null1000, sort1).scoreDocs;  
  91.             System.out.println(hits.length);  
  92.             // Iterate through the results:  
  93.           
  94.              for(ScoreDoc match :  hits)  
  95.                 {  
  96.                     Document doc = isearcher.doc(match.doc);  
  97.                     System.out.println(""+doc.get("postId") +" : "+doc.get("title") + " sorc:"+match.score);  
  98.                 }  
  99.                   
  100.             isearcher.close();  
  101.             directory.close();  
  102.         }  
  103.           
  104.         private static Document buildDocument(Post post) {  
  105.             Document doc = new Document();  
  106.             doc.add(new Field("postId", String.valueOf(post.getPostId()), Field.Store.YES, Field.Index.NOT_ANALYZED));  
  107.             doc.add(new Field("title", String.valueOf(post.getTitle()), Field.Store.YES,Field.Index.ANALYZED ));  
  108.             doc.add(new Field("content", String.valueOf(post.getContent()), Field.Store.YES,Field.Index.ANALYZED ));  
  109.             return doc;  
  110.         }  
  111.           
  112.           
  113.         /** 
  114.          * 輸出結果结果 
  115. 相关度 
  116. 10003 : 冷组天使天使天使天使天使天使天使天使天使天使天使天使天使总是能出这么伟大的冷笑话天使 sorc:NaN 
  117. 10002 : 冷组天使天使天使天使总是能出这么伟大的冷笑话天使 sorc:NaN 
  118. 10000 : 冷组天使天使天使天使总是能出这么伟大的冷笑话天使 sorc:NaN 
  119. 10001 : 校内八大俗天使 sorc:NaN 
  120.  
  121.  
  122. id 
  123. 10000 : 冷组天使天使天使天使总是能出这么伟大的冷笑话天使 sorc:NaN 
  124. 10001 : 校内八大俗天使 sorc:NaN 
  125. 10002 : 冷组天使天使天使天使总是能出这么伟大的冷笑话天使 sorc:NaN 
  126. 10003 : 冷组天使天使天使天使天使天使天使天使天使天使天使天使天使总是能出这么伟大的冷笑话天使 sorc:NaN 
  127.  
  128. id+相关度 
  129. 10003 : 冷组天使天使天使天使天使天使天使天使天使天使天使天使天使总是能出这么伟大的冷笑话天使 sorc:NaN 
  130. 10000 : 冷组天使天使天使天使总是能出这么伟大的冷笑话天使 sorc:NaN 
  131. 10002 : 冷组天使天使天使天使总是能出这么伟大的冷笑话天使 sorc:NaN 
  132. 10001 : 校内八大俗天使 sorc:NaN 
  133.          */  
  134. }  


这里的结论就是在使用相关度排序和其他排序是可以组合使用的,但是我要申明的是一定要把相关度排序的SortField.FIELD_SCORE放前面,否则就是不能组合查询了

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值