[lucene]11.lucene索引的删除和更新

	public static void deleteIndex() throws Exception{
            Directory directory = FSDirectory.open(Paths.get("D:\\Freemarkfiles\\lucene01"));
	    SmartChineseAnalyzer analyzer=new SmartChineseAnalyzer();
	    IndexWriterConfig conf=new IndexWriterConfig(analyzer );
	    IndexWriter iwriter =new IndexWriter(directory, conf);
	    //term就是一个unit单元,完整的条件iwriter.deleteDocuments(new Term("filename","lu01."));
	    iwriter.deleteDocuments(new Term("filename","lu01.txt"));//filename=lu02.txt
	    //把上面生成的del文件(回收站)也删掉,不再恢复
//	    iwriter.forceMergeDeletes();
	    iwriter.close();
	    directory.close();
	}

        上面代码的功能是lucene里面索引的删除。

        注意点:lucene里面删除索引也是更新操作,更新操作都要使用IndexWriter来操作,默认使用iwriter.deleteDocuments()方法删除索引之后,lucene还有个索引回收站机制,这个回收站机制就是lucene索引目录下面的.del文件。使用iwriter.forceMergeDeletes();方法从回收站彻底删除索引。

 

ucene里面索引的更新实质上就是先删除索引里面的Document,再新增这个Document对象,只不过是提供了updateDocument()这个方法来简化了你的删除、新增操作为一步罢了,下面是简单的更新索引的java代码:

	public static void updateIndex() throws Exception{
		 Directory directory = FSDirectory.open(Paths.get("D:\\Freemarkfiles\\lucene01"));
		    SmartChineseAnalyzer analyzer=new SmartChineseAnalyzer();
	    IndexWriterConfig conf=new IndexWriterConfig(analyzer );
	    IndexWriter iwriter =new IndexWriter(directory, conf);
	    
	    Document document=new Document();
    	document.add(new StringField("id", "222", Field.Store.YES));
    	document.add(new StringField("docurl", "f:\\test\\a.txt", Field.Store.YES));
    	document.add(new StringField("filename", "a.txt", Field.Store.YES));
    	document.add(new TextField("content", new FileReader("")));
    	document.add(new StringField("zhaiyao","some brief info of its conent", Field.Store.YES));
	    iwriter.updateDocument(new Term("filename","lu01.txt"),document);
	    iwriter.close();
	    directory.close();
	}

iwriter.updateDocument(new Term("filename","lu01.txt"),document);第一个参数指定要更新的文档,第二个参数是更新后的文档内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值