lucene搜索中filter的使用

  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.Filter; 
  18. import org.apache.lucene.search.FuzzyQuery; 
  19. import org.apache.lucene.search.IndexSearcher; 
  20. import org.apache.lucene.search.NumericRangeQuery; 
  21. import org.apache.lucene.search.PhraseQuery; 
  22. import org.apache.lucene.search.PrefixQuery; 
  23. import org.apache.lucene.search.Query; 
  24. import org.apache.lucene.search.ScoreDoc; 
  25. import org.apache.lucene.search.Sort; 
  26. import org.apache.lucene.search.TermQuery; 
  27. import org.apache.lucene.search.TermRangeQuery; 
  28. import org.apache.lucene.search.TopDocs; 
  29. import org.apache.lucene.search.WildcardQuery; 
  30. import org.apache.lucene.search.BooleanClause.Occur; 
  31. import org.apache.lucene.store.Directory; 
  32. import org.apache.lucene.store.FSDirectory; 
  33. import org.apache.lucene.store.LockObtainFailedException; 
  34. import org.apache.lucene.util.Version; 
  35.  
  36.  
  37. public class SearchUtil { 
  38.     private String[] ids = {"1","2","3","4","5","6"}; 
  39.     private String[] emails = {"[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"}; 
  40.     private String[] content ={ 
  41.             "Welcome to Kenan my home"
  42.             "Hello Kenan "
  43.             "Good morning"
  44.             "Are you OK?"
  45.             "Yeah hahahahahahaha Kenan"
  46.             "I like foot ball" 
  47.     }; 
  48.     private int[] attachs = {1,4,6,2,3,8}; 
  49.     private Date[] dates = null
  50.     private String[] names = {"5555","333","44","111","222","666"}; 
  51.     //詞典  
  52.     private Directory directory = null
  53.     //寫入筆 
  54.     private IndexWriter writer = null
  55.     //文檔對象 
  56.     private Document doc = null
  57.     //讀取對象 
  58.     private IndexReader reader = null
  59.     private IndexSearcher searcher = null
  60.     private void datesInit() { 
  61.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
  62.         dates = new Date[6]; 
  63.         try { 
  64.             dates[0] = sdf.parse("2010-12-01"); 
  65.             dates[1] = sdf.parse("2011-12-01"); 
  66.             dates[2] = sdf.parse("2001-12-01"); 
  67.             dates[3] = sdf.parse("2013-12-01"); 
  68.             dates[4] = sdf.parse("2003-12-01"); 
  69.             dates[5] = sdf.parse("2014-12-01"); 
  70.         } catch (ParseException e) { 
  71.             // TODO Auto-generated catch block 
  72.             e.printStackTrace(); 
  73.         } 
  74.     } 
  75.     public SearchUtil(){ 
  76.         datesInit(); 
  77.         try { 
  78.             directory = FSDirectory.open(new File("d:/lucene/index01")); 
  79.              
  80.             this.getSearcher(); 
  81.         } catch (IOException e) { 
  82.             // TODO Auto-generated catch block 
  83.             e.printStackTrace(); 
  84.         } 
  85.     } 
  86.     /** 
  87.      * 構建索引 
  88.      */ 
  89.     public void buildIndex(){ 
  90.         try { 
  91.             writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); 
  92.             for(int i=0;i<6;i++){ 
  93.                 doc = new Document(); 
  94.                 doc.add(new Field ("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  95.                 doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED)); 
  96.                 doc.add(new Field("content",this.content[i],Field.Store.NO,Field.Index.ANALYZED)); 
  97.                 doc.add(new Field("name",this.names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  98.                 //給數字加索引 
  99.                 doc.add(new NumericField("attach", Field.Store.YES,true).setIntValue(attachs[i])); 
  100.                 //給日期加索引 
  101.                 doc.add(new NumericField("date",Field.Store.YES,true).setLongValue(dates[i].getTime())); 
  102.                 writer.addDocument(doc); 
  103.             } 
  104.         } catch (CorruptIndexException e) { 
  105.             // TODO Auto-generated catch block 
  106.             e.printStackTrace(); 
  107.         } catch (LockObtainFailedException e) { 
  108.             // TODO Auto-generated catch block 
  109.             e.printStackTrace(); 
  110.         } catch (IOException e) { 
  111.             // TODO Auto-generated catch block 
  112.             e.printStackTrace(); 
  113.         }finally
  114.             if(writer != null){ 
  115.                 try { 
  116.                     writer.close(); 
  117.                 } catch (CorruptIndexException e) { 
  118.                     // TODO Auto-generated catch block 
  119.                     e.printStackTrace(); 
  120.                 } catch (IOException e) { 
  121.                     // TODO Auto-generated catch block 
  122.                     e.printStackTrace(); 
  123.                 } 
  124.             } 
  125.         } 
  126.     } 
  127.     public IndexSearcher getSearcher(){ 
  128.         try { 
  129.             if(reader == null){ 
  130.                 reader = IndexReader.open(directory); 
  131.             }else
  132.                 IndexReader ir = IndexReader.openIfChanged(reader); 
  133.                 if(ir!=null){ 
  134.                     reader.close(); 
  135.                     reader = ir; 
  136.                 } 
  137.             } 
  138.             searcher = new IndexSearcher(reader); 
  139.             return searcher; 
  140.         } catch (CorruptIndexException e) { 
  141.             // TODO Auto-generated catch block 
  142.             e.printStackTrace(); 
  143.         } catch (IOException e) { 
  144.             // TODO Auto-generated catch block 
  145.             e.printStackTrace(); 
  146.         } 
  147.         return null
  148.     } 
  149.     public void queryParse(Query query , int num ,Sort sort){ 
  150.         try { 
  151.             TopDocs  tds = null
  152.             if(sort!=null){ 
  153.                 tds = this.searcher.search(query, num,sort); 
  154.             }else
  155.                 tds = this.searcher.search(query, num); 
  156.             } 
  157.             System.out.println("查詢到的結果數:"+tds.totalHits); 
  158.             for(ScoreDoc sd : tds.scoreDocs){ 
  159.                 doc = this.searcher.doc(sd.doc); 
  160.                 System.out.println("id:"+doc.get("id")+"---"+"評分:"+sd.score+"--"+"name:"+doc.get("name")+"---"+"attachs:"+doc.get("attach")+"---"+"email:"+doc.get("email")); 
  161.             } 
  162.         } catch (IOException e) { 
  163.             // TODO Auto-generated catch block 
  164.             e.printStackTrace(); 
  165.         } finally { 
  166.             try { 
  167.                 this.searcher.close(); 
  168.             } catch (IOException e) { 
  169.                 // TODO Auto-generated catch block 
  170.                 e.printStackTrace(); 
  171.             } 
  172.         } 
  173.     } 
  174.     public void queryByFilter(Query query , int num ,Filter filter){ 
  175.         try { 
  176.             TopDocs  tds = null
  177.             if(filter!=null){ 
  178.                 tds = this.searcher.search(query, filter, num); 
  179.             }else
  180.                 tds = this.searcher.search(query, num); 
  181.             } 
  182.             System.out.println("查詢到的結果數:"+tds.totalHits); 
  183.             for(ScoreDoc sd : tds.scoreDocs){ 
  184.                 doc = this.searcher.doc(sd.doc); 
  185.                 System.out.println("id:"+doc.get("id")+"---"+"評分:"+sd.score+"--"+"name:"+doc.get("name")+"---"+"attachs:"+doc.get("attach")+"---"+"email:"+doc.get("email")); 
  186.             } 
  187.         } catch (IOException e) { 
  188.             // TODO Auto-generated catch block 
  189.             e.printStackTrace(); 
  190.         } finally { 
  191.             try { 
  192.                 this.searcher.close(); 
  193.             } catch (IOException e) { 
  194.                 // TODO Auto-generated catch block 
  195.                 e.printStackTrace(); 
  196.             } 
  197.         } 
  198.     } 
  199. import org.apache.lucene.analysis.standard.StandardAnalyzer; 
  200. import org.apache.lucene.index.Term; 
  201. import org.apache.lucene.queryParser.ParseException; 
  202. import org.apache.lucene.queryParser.QueryParser; 
  203. import org.apache.lucene.queryParser.QueryParser.Operator; 
  204. import org.apache.lucene.search.Filter; 
  205. import org.apache.lucene.search.NumericRangeFilter; 
  206. import org.apache.lucene.search.Query; 
  207. import org.apache.lucene.search.QueryWrapperFilter; 
  208. import org.apache.lucene.search.Sort; 
  209. import org.apache.lucene.search.SortField; 
  210. import org.apache.lucene.search.TermRangeFilter; 
  211. import org.apache.lucene.search.WildcardQuery; 
  212. import org.apache.lucene.util.Version; 
  213. import org.junit.BeforeClass; 
  214. import org.junit.Test; 
  215.  
  216.  
  217. public class TestSearchUtil { 
  218.      
  219.     static SearchUtil su ; 
  220.     @BeforeClass 
  221.     public static void setUpBeforeClass() throws Exception { 
  222.         su = new SearchUtil(); 
  223.     } 
  224.     @Test 
  225.     public void testBuildIndex(){ 
  226.         su.buildIndex(); 
  227.     } 
  228.  
  229.      
  230.     @Test 
  231.     public void testSort() throws Exception{ 
  232.         //創建QueryParser對象 默認的搜索域爲content 
  233.         QueryParser parser = new QueryParser(Version.LUCENE_35, "content"new StandardAnalyzer(Version.LUCENE_35)); 
  234.         //設置 空格的默認操作符爲 AND  默認爲OR 
  235. //      parser.setDefaultOperator(Operator.AND); 
  236.         //開啓第一個字符的通配符配置 
  237.         parser.setAllowLeadingWildcard(true); 
  238.         //搜索content中包含kenan的 
  239.         Query query = parser.parse("kenan"); 
  240.          
  241.         //默認是按照評分排序 
  242.         su.queryParse(query, 10,null); 
  243.          
  244.         //按照評分排序 
  245.         su.queryParse(query, 10, Sort.RELEVANCE); 
  246.          
  247.         //按照索引排序 默認id來排序 
  248.         su.queryParse(query, 10, Sort.INDEXORDER); 
  249.          
  250.         //按照attach的大小排序 升序 
  251.         su.queryParse(query, 10new Sort(new SortField("attach", SortField.INT))); 
  252.          
  253.         //按照attach的大小排序 降序 
  254.         su.queryParse(query, 10new Sort(new SortField("attach", SortField.INT,true))); 
  255.                  
  256.         //按照name排序 升序 
  257.         su.queryParse(query, 10new Sort(new SortField("name", SortField.STRING))); 
  258.          
  259.         //按照name排序 降序 
  260.         su.queryParse(query, 10new Sort(new SortField("name", SortField.STRING,true))); 
  261.          
  262.         //按照評分排序 
  263.         su.queryParse(query, 10, Sort.RELEVANCE); 
  264.          
  265.         //先按照評分排序 評分相同的,按照attach排序 降序 
  266.         su.queryParse(query, 10new Sort(SortField.FIELD_SCORE,new SortField("attach", SortField.INT,true))); 
  267.          
  268.          
  269.          
  270.     } 
  271.     @Test 
  272.     public void testQueryByFilter() throws ParseException{ 
  273.         //創建QueryParser對象 默認的搜索域爲content 
  274.                 QueryParser parser = new QueryParser(Version.LUCENE_35, "content"new StandardAnalyzer(Version.LUCENE_35)); 
  275.                 //設置 空格的默認操作符爲 AND  默認爲OR 
  276. //              parser.setDefaultOperator(Operator.AND); 
  277.                 //開啓第一個字符的通配符配置 
  278.                 parser.setAllowLeadingWildcard(true); 
  279.                 //搜索content中包含kenan的 
  280.                 Query query = parser.parse("kenan"); 
  281.                  
  282.         Filter filter = null
  283.         //過濾name從1111到4444的 包括邊界 
  284.         filter = new TermRangeFilter("name""1111""4444"truetrue); 
  285.         //過濾attach大小從 1到3的 包括邊界  
  286.         filter = NumericRangeFilter.newIntRange("attach"13truetrue); 
  287.         //通過一個Query進行過濾 
  288.         filter = new QueryWrapperFilter(new WildcardQuery(new Term("email""*.com"))); 
  289.         su.queryByFilter(query ,10,null); 
  290.         su.queryByFilter(query ,10,filter); 
  291.     } 

 

本文出自 “Kenan_ITBlog” 博客,請務必保留此出處http://soukenan.blog.51cto.com/5130995/1122938

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章