一、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();
}

结果:

最新文章

  1. 套用GGTalk做项目的经验总结——GGTalk源码详解系列(一)
  2. WinForm------点击Control弹出MessageBox
  3. Centos7 安装codeblock( 转载)
  4. Extjs各版本的下载链接
  5. Activiti系列——如何在eclipse中安装 Activiti Designer插件
  6. 什么是 block
  7. [改善Java代码]易变业务使用脚本语言编写
  8. Linux协议栈函数调用流程
  9. Android使用bindService启动服务
  10. MFC多线程各种线程用法 .
  11. Android开发8:数据存储(二)——SQLite数据库和ContentProvider的使用
  12. php第三方类库定时任务
  13. Win10更新搜狗输入法后重启输入密码蓝屏
  14. BootStrap的布局学习
  15. 多流向算法GPU并行化
  16. KVM virsh常用命令篇
  17. ViewPager应用引导界面
  18. javascript对字符串的常见操作trim,ltrim,rtrim,isEmpty,isFloat等
  19. python list统计
  20. 查看oracle当前连接数和进程数

热门文章

  1. 神经网络模型(Backbone)
  2. shell 脚本 - 关于循环的应用
  3. Android:状态栏禁用时蓝牙多文件传输弹窗及进度显示
  4. ISO/IEC 9899:2011 条款6.3.1——算术操作数
  5. QML渐变色
  6. SpringCloud学习成长之 十一 Docker部署cloud项目
  7. Java RSA分段加密
  8. php-fpm优化参数介绍
  9. Spring Boot中mybatis insert 如何获得自增id
  10. 【Leetcode_easy】720. Longest Word in Dictionary