lucene4.5.0----自定义filter

自定义filter:

public class MyFilter extends Filter {

	private String ids[]={"1","4","6","8"};//待删除文档id集合
	@Override
	public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs)
			throws IOException {
		int length=context.reader().maxDoc();
		OpenBitSet openBitSet=new OpenBitSet(length);
		openBitSet.set(0, length);
		for(String string:ids)
		{
			System.out.println(string);
			TermQuery query=new TermQuery(new Term("Id",string));
			IndexSearcher indexSearcher=new IndexSearcher(context.reader());
			TopDocs docs=indexSearcher.search(query,length);
			ScoreDoc[] scoreDocs=docs.scoreDocs;
			if (scoreDocs.length==1) {
				openBitSet.clear(scoreDocs[0].doc);
			}
		}
		return openBitSet;
	}

}


要区别scoreDoc[].doc(目前的搜索结果中该文档在scoreDoc中的位置)和id(文档的索引id)的区别。

搜索:

public class MyFilterUtil {
	
	private  Directory directory=null;
	private IndexReader reader=null;
	private IndexSearcher searcher=null;
	public void search(String string)
	{
		try {
			directory=FSDirectory.open(new File("D:/lucene/index04"));
			reader= DirectoryReader.open(directory);
			searcher=new IndexSearcher(reader);
			QueryParser queryParser=new QueryParser(Version.LUCENE_45, "contents",new StandardAnalyzer(Version.LUCENE_45));
			Query query=queryParser.parse(string);
			ScoreDoc[] sds = searcher.search(query,new MyFilter(),100).scoreDocs;
			System.out.println("找到了"+sds.length+"篇文章");
			for(ScoreDoc doc:sds){
				Document document=searcher.doc(doc.doc);
				System.out.println(document.get("score")+" | "+document.get("filename")+
						" | "+doc.doc+" | "+document.get("Id") );
			}
		
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  finally
		{
			if(reader!=null)
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}

 

测试:

@Test
	public void filterTest()
	{
		MyFilterUtil myFilterUtil=new MyFilterUtil();
		myFilterUtil.search("lucene");
	}


普通搜索结果:

找到了7篇文章
73 | shuxue.txt | 7 | 1
75 | action.txt | 5 | 6
67 | struts2.txt | 2 | 9
14 | english.txt | 3 | 5
14 | lucene.txt | 4 | 4
22 | jsp.txt | 11 | 11
73 | cpp.txt | 1 | 7

private String ids[]={"1","4","6","8"};

自定义搜索结果:

6 | struts2.txt | 2 | 9
54 | english.txt | 3 | 5
26 | jsp.txt | 11 | 11
65 | cpp.txt | 1 | 7


 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值