一、代码分析

/**
* Lucene入门
* 操作索引
*/
public class ManageIndex { public IndexWriter getIndexWriter() throws Exception {
//设置索引库的位置
Directory directory = FSDirectory.open(new File("E:\\zhanghaoBF\\luceneSolr\\indexLibrary").toPath());
Analyzer analyzer = new StandardAnalyzer();//创建分词器对象(官方推荐标准分词器)
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);//设置使用的分词器
return new IndexWriter(directory, indexWriterConfig);//索引对象
} /**
* 全删除(PS:索引里面的文档也会删掉)
*
* @throws Exception
*/
@Test
public void deleteAllIndex() throws Exception {
IndexWriter indexWriter = getIndexWriter();//获取索引的流对象
indexWriter.deleteAll();//删除全部索引
indexWriter.close();//关流
} /**
* 按条件删除(PS:索引里面的文档也会删掉)
*
* @throws Exception
*/
@Test
public void deleteIndex() throws Exception {
IndexWriter indexWriter = getIndexWriter();//获取索引的流对象
Query query = new TermQuery(new Term("fileContent", "lucene"));//PS:TermQuery为精准匹配
indexWriter.deleteDocuments(query);//按条件删除索引
indexWriter.close();//关流
} /**
* 修改(PS:先删除后新增,与数据库里面的修改不一样,注意区分)
*
* @throws Exception
*/
@Test
public void updateIndex() throws Exception {
//获取索引的流对象
IndexWriter indexWriter = getIndexWriter(); //构建一个文档对象
Document doc = new Document();
doc.add(new TextField("fileName", "新的文件名称", Field.Store.YES));
doc.add(new TextField("fileContent", "新的文件内容", Field.Store.YES)); //调用更新操作,先把符合条件的索引删掉(包括term和存在索引库的文档),然后把刚才新增的文档对象加入到索引库中
indexWriter.updateDocument(new Term("fileName", "是"), doc);//按条件删除索引,再加入新的索引
//PS:删掉的文档还是会占用对应的文档ID,新增的文档排在最后(和数据库ID自增时的删除再添加一个道理) //关流
indexWriter.close();
}
}

二、注意事项

1、删除的时候,索引库里对应ID下的term和文档都会删除

2、修改操作其实是先把符合条件的term和文档都会删掉,然后再加入新的文档。

3、删掉的文档,文档ID不会释放,还是被占用的。

4、流用完一定要记得关。

最新文章

  1. BeautifulSoup
  2. Java 把 InputStream 转换成 String 的几种方法
  3. Centos 7 安装jdk 配置环境变量
  4. 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract
  5. Oracle DBA从小白到入职实战应用
  6. C++数据结构之Stack(栈)
  7. vim制作c的IDE
  8. 在linux服务器上装svn版本管理,自动部署代码到项目
  9. php 格式化数字 位数不足前面加0补足
  10. FlowLayoutPanel autowrapping doesn't work with autosize
  11. HDU_1230——火星A+B,加法进制问题
  12. GTD:是一种态度
  13. Oracle中的日期和字符串互相转换
  14. Hdu 1301 Jungle Roads (最小生成树)
  15. AOP学习笔记二
  16. 【English】十六、时间相关
  17. h5网页跳转到app,若未安装app,则跳转app下载页面
  18. Kudu-java数据库简单操作
  19. error: navicat 连接debian系列系统mysql 10038问题解决方案
  20. [转]8款实用的jQuery/CSS3最新插件应用

热门文章

  1. MongoDB联表查询
  2. CentOS 阿里源
  3. Ubuntu 磁盘满了处理方法。
  4. LG P4161 [SCOI2009]游戏/LG P6280 [USACO20OPEN]Exercise G
  5. Selenium的WebDriver API元素定位中的XPath和CSS
  6. Shell编程—sed和gawk
  7. CF1270B Interesting Subarray 题解
  8. win10下MinGW的安装与配置(详细步骤)
  9. Oracle-Over()函数高级用法
  10. e3mall商城总结11之sso系统的分析、应用以及解决ajax跨域问题