第一步:添加相关的依赖
<!-- https://mvnrepository.com/artifact/junit/junit -->(新增 junit依赖)
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
第二步:准备luke插件
找到个luke所有版本的下载:https://github.com/DmitryKey/luke/releases
Luke是一个用于Lucene搜索引擎的,方便开发和诊断的第三方工具,它可以访问现有Lucene的索引,并允许您显示和修改。
第三步:新建一个类 IndexingTest
/**
* 对索引的文档进行添加 修改 删除
* @author 王晨
*
*/
public class IndexingTest {
private String[] ids = {"1","2","3"};
private String[] citys = {"beijing","shanghai","guangzhou"};
private String[] descs = {
"beijing is a beautiful city",
"shanghai is a city of culture",
"guangzhou is a bulsting city"
};
private Directory dir;
/**
* 每次执行时都会执行一次写索引
* @throws Exception
*/
@Before
public void setUp() throws Exception {
//获取到索引输出的目录
dir = FSDirectory.open(Paths.get("D:\\lucene2"));
IndexWriter writer = getWriter();
//新建document对象 将数据加入
for(int i = 0; i < ids.length; i++){
Document document = new Document();
document.add(new StringField("id", ids[i], Field.Store.YES));
document.add(new StringField("city", citys[i], Field.Store.YES));
document.add(new TextField("desc",descs[i], Field.Store.NO));
writer.addDocument(document);
}
writer.close();
}
/**
* 获取到IndexWriter的实例
* @return
*/
private IndexWriter getWriter() throws Exception{
// TODO Auto-generated method stub
Analyzer analyzer = new StandardAnalyzer(); //标准分词器
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter writer = new IndexWriter(dir, conf);
return writer;
}
/**
* 测试一共写了多少个文档
* @throws Exception
*/
@Test
public void testIndexWriter() throws Exception{
IndexWriter writer = getWriter();
System.out.println("一共写入了"+writer.numDocs()+"个文档");
writer.close();
}
/**
* 测试读取文档
* @throws Exception
*/
@Test
public void testIndexReader() throws Exception{
IndexReader reader = DirectoryReader.open(dir);
System.out.println("最大文档数:"+reader.maxDoc());
System.out.println("实际文档数:"+reader.numDocs());
reader.close();
}
/**
* 测试删除文档 在合并前
* @throws Exception
*/
@Test
public void testDeleteBeforeMerge() throws Exception{
IndexWriter writer = getWriter();
System.out.println("再删除前一个存在的文档数"+writer.numDocs());
writer.deleteDocuments(new Term("id","1"));
writer.commit();
System.out.println("删除后最大存在的文档数:"+writer.maxDoc());
System.out.println("删除后实际存在的文档数:"+writer.numDocs());
writer.close();
}
/**
* 测试删除文档 在合并后
* @throws Exception
*/
@Test
public void testDeleteAfterMerge() throws Exception{
IndexWriter writer = getWriter();
System.out.println("在删除前一个存在的文档数"+writer.numDocs());
writer.deleteDocuments(new Term("id","1"));
writer.forceMergeDeletes(); //强制合并索引 数据量大时不建议进行此操作
writer.commit();
System.out.println("删除后最大存在的文档数:"+writer.maxDoc());
System.out.println("删除后实际存在的文档数:"+writer.numDocs());
writer.close();
}
/**
* 测试更新
* @throws Exception
*/
@Test
public void testUpdate() throws Exception{
IndexWriter writer = getWriter();
Document doc = new Document();
doc.add(new StringField("id", "beijing", Field.Store.YES));
doc.add(new StringField("city", "beijing", Field.Store.YES));
doc.add(new TextField("desc", "bbbjjj is city", Field.Store.NO));
writer.updateDocument(new Term("id","1"), doc); //底层是先删除 在添加
writer.close();
}
}