Lucene使用Filter搜索过滤

 

 1     /**
 2      * 排序查询
 3      * 
 4      * @param querystr
 5      *            查找匹配的字符串
 6      * @param domain
 7      *            查找内容的域
 8      * @param filter
 9      *            过滤器
10      */
11     public void SearchByFilter(String querystr, String domain, Filter filter) {
12         TopDocs docs = null;
13         IndexSearcher searcher = this.getSearcher();
14         try {
15             QueryParser parser = new QueryParser(Version.LUCENE_35, domain,
16                     new StandardAnalyzer(Version.LUCENE_35));
17             Query query = parser.parse(querystr);
18             if (filter == null) {
19                 docs = searcher.search(query, 150);
20             } else {
21                 docs = searcher.search(query,filter,150);
22             }
23 
24             // 输出信息
25             ScoreDoc[] sds = docs.scoreDocs;
26             Document d = null;
27             SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
28             for (ScoreDoc s : sds) {
29                 d = searcher.doc(s.doc);
30                 System.out.println(s.doc+"->"
31                                     +s.score+"->"
32                                     +d.get("filename")+"->"
33                                     +d.get("size")+"->"
34                                     +sdf.format(new Date(Long.valueOf(d.get("date")))));
35             }
36 
37         } catch (ParseException e) {
38             e.printStackTrace();
39         } catch (IOException e) {
40             e.printStackTrace();
41         }
42     }
43     
 1     @Test
 2     public void testFilterSearch(){
 3         
 4         Filter filter=null;
 5         //使用TermRangeFilter进行范围过滤搜索
 6         //1.域   2.起始位置  3.结束位置   4.是否包含起始位置    5.是否包含结束位置   
 7         filter=new TermRangeFilter("filename","chs.a","chs.txt",true,true);
 8         //使用NumericRangeFilter进行过滤
 9         filter=NumericRangeFilter.newIntRange("size", 10, 5000, true, true);
10         //使用QueryWrapperFilter进行过滤
11         filter=new QueryWrapperFilter(new WildcardQuery(new Term("filename", "*.ini")));
12         su.SearchByFilter("中国", "content", filter);
13     }

 

posted @ 2012-04-22 14:30  Paul.Lau  阅读(7494)  评论(1编辑  收藏  举报