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 plaincopy
  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.     }  

 

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