new StandardAnalyzer(Version.LUCENE_36)));
/*
* lucene没有提供更新,只能先删除再添加
*
*/
for(int i=0;i
document=new Document();
document.add(new Field("id", "11",
Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS ));
document.add(new Field("email",emails[0],
Field.Store.YES,Field.Index.ANALYZED));
document.add(new Field("content", contents[0],
Field.Store.NO, Field.Index.NOT_ANALYZED));
document.add(new Field("name",names[0],
Field.Store.YES,Field.Index.NOT_ANALYZED));
writer.updateDocument(new Term("id","1"), document);
}
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//搜索
public void search01()
{
IndexReader reader=null;
try {
reader = IndexReader.open(directory);
IndexSearcher searcher=new IndexSearcher(reader);
TermQuery query=new TermQuery(new Term("content","like"));
TopDocs tds =searcher.search(query, 10);
for(ScoreDoc sdc:tds.scoreDocs)
{
Document document=searcher.doc(sdc.doc);
System.out.println("("+sdc.doc+")"+document.get("name")+"["+document.get("email")+
"]-->"+document.get("id")+"..."+document.get("attach")+"..."+document.get("date"));
}
reader.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void search02()
{
//IndexReader reader=null;
try {
//reader = IndexReader.open(directory);
IndexSearcher searcher=getSearcher();
TermQuery query=new TermQuery(new Term("content","like"));
TopDocs tds =searcher.search(query, 10);
for(ScoreDoc sdc:tds.scoreDocs)
{
Document document=searcher.doc(sdc.doc);
System.out.println("("+sdc.doc+")"+document.get("name")+"["+document.get("email")+
"]-->"+document.get("id")+"..."+document.get("attach")+"..."+document.get
("date"));
}