(三)Lucene之删除更新文档以及luke的基本使用
2024-09-01 15:13:46
一、demo
- 本例中采用单元测试,故在pom.xml中引入junit jar包
- 1.1 前提:
public class IndexTest {
/**
*数据准备
*/
private String ids[] = { "1", "2", "3" };
private String citys[] = { "qingdao", "nanjing", "shanghai" };
private String descs[] = { "Qingdao is a beautiful city.", "Nanjing is a city of culture.",
"Shanghai is a bustling city." }; @Before
public void setUp() throws IOException {
IndexWriter indexWriter = getIndexWiter(); 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 StringField("desc", descs[i], Field.Store.NO));
indexWriter.addDocument(document);
} indexWriter.close();
} /**
* 实例化IndexWiter
*
* @return
* @throws IOException
*/
private IndexWriter getIndexWiter() throws IOException {
Directory dir = FSDirectory.open(Paths.get("E:\\lucene2"));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(dir, conf); return indexWriter;
} }
1.2 测试写入
/**
* 测试写了几个文档
*
* @throws IOException
*/
@Test
public void tesWritert() throws IOException {
IndexWriter indexWriter = getIndexWiter(); System.out.println("一共写了" + indexWriter.numDocs() + "个文档");
indexWriter.close();
}
结果:
- 使用luke查看索引文件
1.3 测试读取
- 由于进行了1.2 测试写入,所以要把索引文件清理一下,因为此步骤也会产生索引文件
/**
* 测试读取了多少文档
* @throws IOException
*/
@Test
public void testReader() throws IOException {
IndexWriter indexWriter=getIndexWiter();
System.out.println("最大文档数为:"+indexWriter.maxDoc());
System.out.println("当前文档数为:"+indexWriter.numDocs());
indexWriter.close();
}
结果:
1.4 测试删除 在合并前
- 由于进行了1.3 测试写入,所以要把索引文件清理一下,因为此步骤也会产生索引文件
/**
* 测试删除 在合并前
* @throws IOException
*/
@Test
public void testDeleteBeforeMerge() throws IOException {
IndexWriter indexWriter=getIndexWiter();
indexWriter.deleteDocuments(new Term("id","1"));
System.out.println("删除前。。。。。"+indexWriter.numDocs()+"个文件");
indexWriter.commit();
System.out.println("writer.maxDoc():"+indexWriter.maxDoc());
System.out.println("writer.numDocs():"+indexWriter.numDocs());
indexWriter.close();
}
结果:
- 上图可知,虽然indexWriter.deleteDocuments(new Term("id","1")); 删除了document,但是索引文件中不会立即删除。
1.5 测试删除 在合并后
- 由于进行了1.4 测试写入,所以要把索引文件清理一下,因为此步骤也会产生索引文件
- 测试删除 在合并后,用强制删除的方法会立即在索引表删除文档,
这种方法比较耗cpu,建议数据量不大的系统使用,数据量大的系统建议不写indexWriter.forceMergeDeletes(); 就不会立即删除文档
/**
* 测试删除 在合并后,用强制删除的方法会立即在索引表删除文档,
* 这种方法比较耗cpu,建议数据量不大的系统使用,数据量大的系统建议不写indexWriter.forceMergeDeletes(); 就不会立即删除文档
* @throws IOException
*/
@Test
public void testDeleteAfterMerge() throws IOException {
IndexWriter indexWriter=getIndexWiter();
indexWriter.deleteDocuments(new Term("id","1"));
System.out.println("删除前。。。。。"+indexWriter.numDocs()+"个文件");
indexWriter.forceMergeDeletes(); // 强制删除
indexWriter.commit();
System.out.println("writer.maxDoc():"+indexWriter.maxDoc());
System.out.println("writer.numDocs():"+indexWriter.numDocs());
indexWriter.close();
}
结果:
- 如图可知,立即在索引表删除文档,这种方式比较耗cpu,建议数据量不大的系统使用,数据量大的系统建议不写indexWriter.forceMergeDeletes(); 就不会立即删除文档。
1.6 测试更新
- 由于进行了1.5 测试写入,所以要把索引文件清理一下,因为此步骤也会产生索引文件
/**
* 测试更新
* @throws Exception
*/
@Test
public void testUpdate()throws Exception{
IndexWriter writer=getIndexWiter();
Document doc=new Document();
doc.add(new StringField("id", "1", Field.Store.YES));
doc.add(new StringField("city","qingdao",Field.Store.YES));
doc.add(new TextField("desc", "dsss is a city.", Field.Store.NO));
writer.updateDocument(new Term("id","1"), doc);
writer.close();
}
结果:
最新文章
- 套用GGTalk做项目的经验总结——GGTalk源码详解系列(一)
- WinForm------点击Control弹出MessageBox
- Centos7 安装codeblock( 转载)
- Extjs各版本的下载链接
- Activiti系列——如何在eclipse中安装 Activiti Designer插件
- 什么是 block
- [改善Java代码]易变业务使用脚本语言编写
- Linux协议栈函数调用流程
- Android使用bindService启动服务
- MFC多线程各种线程用法 .
- Android开发8:数据存储(二)——SQLite数据库和ContentProvider的使用
- php第三方类库定时任务
- Win10更新搜狗输入法后重启输入密码蓝屏
- BootStrap的布局学习
- 多流向算法GPU并行化
- KVM virsh常用命令篇
- ViewPager应用引导界面
- javascript对字符串的常见操作trim,ltrim,rtrim,isEmpty,isFloat等
- python list统计
- 查看oracle当前连接数和进程数