1。数据库中添加数据
增加新纪录:
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), false);
false:在原先基础上添加,不覆盖之前,增量索引
true:重新创建
2。数据库中更新数据
判断是否有
删除陈旧记录:
public void updateIndex(){
String INDEX_DIR = "d:\\index_update";
Directory dir;
try {
dir = FSDirectory.getDirectory(INDEX_DIR);
IndexReader reader = IndexReader.open(dir);
Term term = new Term("ID","1"); //---精确查找,删除此纪录
Query query = new TermQuery(term);
IndexSearcher is = new IndexSearcher(dir);
Hits hits = is.search(query);
System.out.println( "删除前"+hits.length() + " 个结果! ");
//删除-----使用deleteDocument只能对term进行删除
reader.deleteDocuments(term);
reader.close();
dir = FSDirectory.getDirectory(INDEX_DIR);
IndexReader reader1 = IndexReader.open(dir);
Term term1 = new Term("ID","1");
Query query1 = new TermQuery(term);
IndexSearcher is1 = new IndexSearcher(dir);
Hits hits1 = is1.search(query1);
System.out.println("删除后"+ hits1.length() + " 个结果! ");
// IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), false);
// Document doc = new Document();
// doc.add(new Field("TicketType", "演唱会111", Field.Store.YES, Field.Index.TOKENIZED));
// writer.addDocument(doc);
// writer.optimize();
// System.out.println("更改索引:"+doc.getField("TicketType").stringValue());
// writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}