lucene学习之搜索排序

来源:互联网 发布:离散数学 视频 知乎 编辑:程序博客网 时间:2024/06/06 03:49
  1. import java.io.File; 
  2. import java.io.IOException; 
  3. import java.text.ParseException; 
  4. import java.text.SimpleDateFormat; 
  5. import java.util.Date; 
  6.  
  7. import org.apache.lucene.analysis.standard.StandardAnalyzer; 
  8. import org.apache.lucene.document.Document; 
  9. import org.apache.lucene.document.Field; 
  10. import org.apache.lucene.document.NumericField; 
  11. import org.apache.lucene.index.CorruptIndexException; 
  12. import org.apache.lucene.index.IndexReader; 
  13. import org.apache.lucene.index.IndexWriter; 
  14. import org.apache.lucene.index.IndexWriterConfig; 
  15. import org.apache.lucene.index.Term; 
  16. import org.apache.lucene.search.BooleanQuery; 
  17. import org.apache.lucene.search.FuzzyQuery; 
  18. import org.apache.lucene.search.IndexSearcher; 
  19. import org.apache.lucene.search.NumericRangeQuery; 
  20. import org.apache.lucene.search.PhraseQuery; 
  21. import org.apache.lucene.search.PrefixQuery; 
  22. import org.apache.lucene.search.Query; 
  23. import org.apache.lucene.search.ScoreDoc; 
  24. import org.apache.lucene.search.Sort; 
  25. import org.apache.lucene.search.TermQuery; 
  26. import org.apache.lucene.search.TermRangeQuery; 
  27. import org.apache.lucene.search.TopDocs; 
  28. import org.apache.lucene.search.WildcardQuery; 
  29. import org.apache.lucene.search.BooleanClause.Occur; 
  30. import org.apache.lucene.store.Directory; 
  31. import org.apache.lucene.store.FSDirectory; 
  32. import org.apache.lucene.store.LockObtainFailedException; 
  33. import org.apache.lucene.util.Version; 
  34.  
  35.  
  36. public class SearchUtil { 
  37.     private String[] ids = {"1","2","3","4","5","6"}; 
  38.     private String[] emails = {"soukenan@qq.com","li@soukenan.com","804632564@qq.com","admin@qq.com","soukenan@kenan.org","123@df.com"}; 
  39.     private String[] content ={ 
  40.             "Welcome to Kenan my home"
  41.             "Hello Kenan "
  42.             "Good morning"
  43.             "Are you OK?"
  44.             "Yeah hahahahahahaha Kenan"
  45.             "I like foot ball" 
  46.     }; 
  47.     private int[] attachs = {1,4,6,2,3,8}; 
  48.     private Date[] dates = null
  49.     private String[] names = {"5555","333","44","111","222","666"}; 
  50.     //词典  
  51.     private Directory directory = null
  52.     //写入笔 
  53.     private IndexWriter writer = null
  54.     //文档对象 
  55.     private Document doc = null
  56.     //读取对象 
  57.     private IndexReader reader = null
  58.     private IndexSearcher searcher = null
  59.     private void datesInit() { 
  60.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
  61.         dates = new Date[6]; 
  62.         try { 
  63.             dates[0] = sdf.parse("2010-12-01"); 
  64.             dates[1] = sdf.parse("2011-12-01"); 
  65.             dates[2] = sdf.parse("2001-12-01"); 
  66.             dates[3] = sdf.parse("2013-12-01"); 
  67.             dates[4] = sdf.parse("2003-12-01"); 
  68.             dates[5] = sdf.parse("2014-12-01"); 
  69.         } catch (ParseException e) { 
  70.             // TODO Auto-generated catch block 
  71.             e.printStackTrace(); 
  72.         } 
  73.     } 
  74.     public SearchUtil(){ 
  75.         datesInit(); 
  76.         try { 
  77.             directory = FSDirectory.open(new File("d:/lucene/index01")); 
  78.              
  79.             this.getSearcher(); 
  80.         } catch (IOException e) { 
  81.             // TODO Auto-generated catch block 
  82.             e.printStackTrace(); 
  83.         } 
  84.     } 
  85.     /** 
  86.      * 构建索引 
  87.      */ 
  88.     public void buildIndex(){ 
  89.         try { 
  90.             writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); 
  91.             for(int i=0;i<6;i++){ 
  92.                 doc = new Document(); 
  93.                 doc.add(new Field ("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  94.                 doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED)); 
  95.                 doc.add(new Field("content",this.content[i],Field.Store.NO,Field.Index.ANALYZED)); 
  96.                 doc.add(new Field("name",this.names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  97.                 //给数字加索引 
  98.                 doc.add(new NumericField("attach", Field.Store.YES,true).setIntValue(attachs[i])); 
  99.                 //给日期加索引 
  100.                 doc.add(new NumericField("date",Field.Store.YES,true).setLongValue(dates[i].getTime())); 
  101.                 writer.addDocument(doc); 
  102.             } 
  103.         } catch (CorruptIndexException e) { 
  104.             // TODO Auto-generated catch block 
  105.             e.printStackTrace(); 
  106.         } catch (LockObtainFailedException e) { 
  107.             // TODO Auto-generated catch block 
  108.             e.printStackTrace(); 
  109.         } catch (IOException e) { 
  110.             // TODO Auto-generated catch block 
  111.             e.printStackTrace(); 
  112.         }finally
  113.             if(writer != null){ 
  114.                 try { 
  115.                     writer.close(); 
  116.                 } catch (CorruptIndexException e) { 
  117.                     // TODO Auto-generated catch block 
  118.                     e.printStackTrace(); 
  119.                 } catch (IOException e) { 
  120.                     // TODO Auto-generated catch block 
  121.                     e.printStackTrace(); 
  122.                 } 
  123.             } 
  124.         } 
  125.     } 
  126.     public IndexSearcher getSearcher(){ 
  127.         try { 
  128.             if(reader == null){ 
  129.                 reader = IndexReader.open(directory); 
  130.             }else
  131.                 IndexReader ir = IndexReader.openIfChanged(reader); 
  132.                 if(ir!=null){ 
  133.                     reader.close(); 
  134.                     reader = ir; 
  135.                 } 
  136.             } 
  137.             searcher = new IndexSearcher(reader); 
  138.             return searcher; 
  139.         } catch (CorruptIndexException e) { 
  140.             // TODO Auto-generated catch block 
  141.             e.printStackTrace(); 
  142.         } catch (IOException e) { 
  143.             // TODO Auto-generated catch block 
  144.             e.printStackTrace(); 
  145.         } 
  146.         return null
  147.     } 
  148.     public void queryParse(Query query , int num ,Sort sort){ 
  149.         try { 
  150.             TopDocs  tds = null
  151.             if(sort!=null){ 
  152.                 tds = this.searcher.search(query, num,sort); 
  153.             }else
  154.                 tds = this.searcher.search(query, num); 
  155.             } 
  156.             System.out.println("查询到的结果数:"+tds.totalHits); 
  157.             for(ScoreDoc sd : tds.scoreDocs){ 
  158.                 doc = this.searcher.doc(sd.doc); 
  159.                 System.out.println("id:"+doc.get("id")+"---"+"评分:"+sd.score+"--"+"name:"+doc.get("name")+"---"+"attachs:"+doc.get("attach")+"---"+"email:"+doc.get("email")); 
  160.             } 
  161.         } catch (IOException e) { 
  162.             // TODO Auto-generated catch block 
  163.             e.printStackTrace(); 
  164.         } finally { 
  165.             try { 
  166.                 this.searcher.close(); 
  167.             } catch (IOException e) { 
  168.                 // TODO Auto-generated catch block 
  169.                 e.printStackTrace(); 
  170.             } 
  171.         } 
  172.     } 
  173.  
  174. import org.apache.lucene.analysis.standard.StandardAnalyzer; 
  175. import org.apache.lucene.queryParser.ParseException; 
  176. import org.apache.lucene.queryParser.QueryParser; 
  177. import org.apache.lucene.queryParser.QueryParser.Operator; 
  178. import org.apache.lucene.search.Query; 
  179. import org.apache.lucene.search.Sort; 
  180. import org.apache.lucene.search.SortField; 
  181. import org.apache.lucene.util.Version; 
  182. import org.junit.BeforeClass; 
  183. import org.junit.Test; 
  184.  
  185.  
  186. public class TestSearchUtil { 
  187.      
  188.     static SearchUtil su ; 
  189.     @BeforeClass 
  190.     public static void setUpBeforeClass() throws Exception { 
  191.         su = new SearchUtil(); 
  192.     } 
  193.     @Test 
  194.     public void testBuildIndex(){ 
  195.         su.buildIndex(); 
  196.     } 
  197.  
  198.      
  199.     @Test 
  200.     public void testSort() throws Exception{ 
  201.         //创建QueryParser对象 默认的搜索域为content 
  202.         QueryParser parser = new QueryParser(Version.LUCENE_35, "content"new StandardAnalyzer(Version.LUCENE_35)); 
  203.         //设置 空格的默认操作符为 AND  默认为OR 
  204. //      parser.setDefaultOperator(Operator.AND); 
  205.         //开启第一个字符的通配符配置 
  206.         parser.setAllowLeadingWildcard(true); 
  207.         //搜索content中包含kenan的 
  208.         Query query = parser.parse("kenan"); 
  209.          
  210.         //默认是按照评分排序 
  211.         su.queryParse(query, 10,null); 
  212.          
  213.         //按照评分排序 
  214.         su.queryParse(query, 10, Sort.RELEVANCE); 
  215.          
  216.         //按照索引排序 默认id来排序 
  217.         su.queryParse(query, 10, Sort.INDEXORDER); 
  218.          
  219.         //按照attach的大小排序 升序 
  220.         su.queryParse(query, 10new Sort(new SortField("attach", SortField.INT))); 
  221.          
  222.         //按照attach的大小排序 降序 
  223.         su.queryParse(query, 10new Sort(new SortField("attach", SortField.INT,true))); 
  224.                  
  225.         //按照name排序 升序 
  226.         su.queryParse(query, 10new Sort(new SortField("name", SortField.STRING))); 
  227.          
  228.         //按照name排序 降序 
  229.         su.queryParse(query, 10new Sort(new SortField("name", SortField.STRING,true))); 
  230.          
  231.         //按照评分排序 
  232.         su.queryParse(query, 10, Sort.RELEVANCE); 
  233.          
  234.         //先按照评分排序 评分相同的,按照attach排序 降序 
  235.         su.queryParse(query, 10new Sort(SortField.FIELD_SCORE,new SortField("attach", SortField.INT,true))); 
  236.          
  237.          
  238.          
  239.     } 

 

本文出自 “Kenan_ITBlog” 博客,请务必保留此出处http://soukenan.blog.51cto.com/5130995/1122899

原创粉丝点击