自定义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