先上代码,注释还算清楚
为了减少代码量,写一个公用的增删改公用的获取IndexWriter对象的方法
/**
* 增删改公用的获取IndexWriter对象
* @return
* @throws Exception
*/
public IndexWriter getIndexWriter() throws Exception{
//获得索引存放的位置
Directory directory = FSDirectory.open(new File("./index"));
//获得分词器
Analyzer analyzer=new IKAnalyzer();
//获得IndexWriterConfig对象
IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_4_10_2,analyzer);
//获得indexWeiter对象
IndexWriter indexWriter =new IndexWriter(directory,indexWriterConfig);
return indexWriter;
}
删除,修改索引
/**
* 删除索引——全部删除
* @throws Exception
*/
@Test
public void deleteAll() throws Exce