Lucene3.6 之 Filter

1、TermRangeFilter

A Filter that restricts search results to a range of term values in a given field. 

This filter matches the documents looking for terms that fall into the supplied range according to Byte.compareTo(Byte), It is not intended for numerical ranges; use NumericRangeFilter instead. 

If you construct a large number of range filters with different ranges but on the same field, FieldCacheRangeFilter may have significantly better performance. 

示例代码

[java]   view plain copy
  1. @Test  
  2.     public void testTermRangeFilter(){  
  3.         try {  
  4.             String path = "D:\\LuceneEx\\day01";  
  5.             String keyword = "android";  
  6.             File file = new File(path);  
  7.             Directory mdDirectory = FSDirectory.open(file);  
  8.             // 使用 商业分词器  
  9.             Analyzer mAnalyzer = new IKAnalyzer();  
  10.   
  11.             IndexReader reader = IndexReader.open(mdDirectory);  
  12.   
  13.             IndexSearcher searcher = new IndexSearcher(reader);  
  14.   
  15.             String[] fields = { "title""category" }; // (在多个Filed中搜索)  
  16.             QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36,  
  17.                     fields, mAnalyzer);  
  18.             Query query = parser.parse(keyword);  
  19.               
  20.             //查询publishTime范围在2011-09 - 2012-06之间的记录  
  21.             Filter filter = new TermRangeFilter("publishTime""2011-09""2012-06"truetrue);  
  22.               
  23.             TopDocs tops = searcher.search(query, filter, 100);  
  24.   
  25.             int count = tops.totalHits;  
  26.   
  27.             System.out.println("totalHits=" + count);  
  28.   
  29.             ScoreDoc[] docs = tops.scoreDocs;  
  30.   
  31.             for (int i = 0; i < docs.length; i++) {  
  32.                   
  33.                 Document doc = searcher.doc(docs[i].doc);  
  34.   
  35.                 float score = docs[i].score;  
  36.                   
  37.                 int id = Integer.parseInt(doc.get("id"));  
  38.                 String title = doc.get("title");  
  39.                 String author = doc.get("author");  
  40.                 String publishTime = doc.get("publishTime");  
  41.                 String source = doc.get("source");  
  42.                 String category = doc.get("category");  
  43.                 float reputation = Float.parseFloat(doc.get("reputation"));  
  44.   
  45.                 System.out.println(id + "\t" + title + "\t" + author + "\t"  
  46.                         + publishTime + "\t" + source + "\t" + category + "\t"  
  47.                         + reputation+"\t"+score);  
  48.             }  
  49.   
  50.             reader.close();  
  51.             searcher.close();  
  52.   
  53.         } catch (CorruptIndexException e) {  
  54.             e.printStackTrace();  
  55.         } catch (IOException e) {  
  56.             e.printStackTrace();  
  57.         } catch (ParseException e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.     }  



2、NumericRangeFilter

A Filter that only accepts numeric values within a specified range. To use this, you must first index the numeric values using IntField, FloatField, LongField or DoubleField (expert: NumericTokenStream). 

示例代码

  1. @Test  
  2.     public void testNumericRangeFilter(){  
  3.         try {  
  4.             String path = "D:\\LuceneEx\\day02";  
  5.             String keyword = "android";  
  6.             File file = new File(path);  
  7.             Directory mdDirectory = FSDirectory.open(file);  
  8.             // 使用 商业分词器  
  9.             Analyzer mAnalyzer = new IKAnalyzer();  
  10.   
  11.             IndexReader reader = IndexReader.open(mdDirectory);  
  12.   
  13.             IndexSearcher searcher = new IndexSearcher(reader);  
  14.   
  15.             String[] fields = { "title""category" }; // (在多个Filed中搜索)  
  16.             QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36,  
  17.                     fields, mAnalyzer);  
  18.             Query query = parser.parse(keyword);  
  19.               
  20.             //过滤 reputation 在9.0f到 9.8 分之间 的记录  
  21.             Filter filter = NumericRangeFilter.newFloatRange("reputation"9.0f, 9.8f, truetrue);  
  22.               
  23.             TopDocs tops = searcher.search(query, filter, 100);  
  24.   
  25.             int count = tops.totalHits;  
  26.   
  27.             System.out.println("totalHits=" + count);  
  28.   
  29.             ScoreDoc[] docs = tops.scoreDocs;  
  30.   
  31.             for (int i = 0; i < docs.length; i++) {  
  32.                   
  33.                 Document doc = searcher.doc(docs[i].doc);  
  34.   
  35.                 float score = docs[i].score;  
  36.                   
  37.                 int id = Integer.parseInt(doc.get("id"));  
  38.                 String title = doc.get("title");  
  39.                 String author = doc.get("author");  
  40.                 String publishTime = doc.get("publishTime");  
  41.                 String source = doc.get("source");  
  42.                 String category = doc.get("category");  
  43.                 float reputation = Float.parseFloat(doc.get("reputation"));  
  44.   
  45.                 System.out.println(id + "\t" + title + "\t" + author + "\t"  
  46.                         + publishTime + "\t" + source + "\t" + category + "\t"  
  47.                         + reputation+"\t"+score);  
  48.             }  
  49.   
  50.             reader.close();  
  51.             searcher.close();  
  52.   
  53.         } catch (CorruptIndexException e) {  
  54.             e.printStackTrace();  
  55.         } catch (IOException e) {  
  56.             e.printStackTrace();  
  57.         } catch (ParseException e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.     }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值