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. 

示例代码

@Test
	public void testTermRangeFilter(){
		try {
			String path = "D:\\LuceneEx\\day01";
			String keyword = "android";
			File file = new File(path);
			Directory mdDirectory = FSDirectory.open(file);
			// 使用 商业分词器
			Analyzer mAnalyzer = new IKAnalyzer();

			IndexReader reader = IndexReader.open(mdDirectory);

			IndexSearcher searcher = new IndexSearcher(reader);

			String[] fields = { "title", "category" }; // (在多个Filed中搜索)
			QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36,
					fields, mAnalyzer);
			Query query = parser.parse(keyword);
			
			//查询publishTime范围在2011-09 - 2012-06之间的记录
			Filter filter = new TermRangeFilter("publishTime", "2011-09", "2012-06", true, true);
			
			TopDocs tops = searcher.search(query, filter, 100);

			int count = tops.totalHits;

			System.out.println("totalHits=" + count);

			ScoreDoc[] docs = tops.scoreDocs;

			for (int i = 0; i < docs.length; i++) {
				
				Document doc = searcher.doc(docs[i].doc);

				float score = docs[i].score;
				
				int id = Integer.parseInt(doc.get("id"));
				String title = doc.get("title");
				String author = doc.get("author");
				String publishTime = doc.get("publishTime");
				String source = doc.get("source");
				String category = doc.get("category");
				float reputation = Float.parseFloat(doc.get("reputation"));

				System.out.println(id + "\t" + title + "\t" + author + "\t"
						+ publishTime + "\t" + source + "\t" + category + "\t"
						+ reputation+"\t"+score);
			}

			reader.close();
			searcher.close();

		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}



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). 

示例代码

@Test
	public void testNumericRangeFilter(){
		try {
			String path = "D:\\LuceneEx\\day02";
			String keyword = "android";
			File file = new File(path);
			Directory mdDirectory = FSDirectory.open(file);
			// 使用 商业分词器
			Analyzer mAnalyzer = new IKAnalyzer();

			IndexReader reader = IndexReader.open(mdDirectory);

			IndexSearcher searcher = new IndexSearcher(reader);

			String[] fields = { "title", "category" }; // (在多个Filed中搜索)
			QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36,
					fields, mAnalyzer);
			Query query = parser.parse(keyword);
			
			//过滤 reputation 在9.0f到 9.8 分之间 的记录
			Filter filter = NumericRangeFilter.newFloatRange("reputation", 9.0f, 9.8f, true, true);
			
			TopDocs tops = searcher.search(query, filter, 100);

			int count = tops.totalHits;

			System.out.println("totalHits=" + count);

			ScoreDoc[] docs = tops.scoreDocs;

			for (int i = 0; i < docs.length; i++) {
				
				Document doc = searcher.doc(docs[i].doc);

				float score = docs[i].score;
				
				int id = Integer.parseInt(doc.get("id"));
				String title = doc.get("title");
				String author = doc.get("author");
				String publishTime = doc.get("publishTime");
				String source = doc.get("source");
				String category = doc.get("category");
				float reputation = Float.parseFloat(doc.get("reputation"));

				System.out.println(id + "\t" + title + "\t" + author + "\t"
						+ publishTime + "\t" + source + "\t" + category + "\t"
						+ reputation+"\t"+score);
			}

			reader.close();
			searcher.close();

		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值