项目结构大家可以先看看:lucene(全文搜索)_根据内容建立索引_源码下载
索引的删除操作:
1 /** 2 * 索引的删除 3 */ 4 public void delete() { 5 IndexWriter writer = null; 6 7 try { 8 writer = new IndexWriter(directory, new IndexWriterConfig( 9 Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); 10 // 参数是一个选项,可以是一个query,也可以是一个term,term是一个精确查找的值 11 writer.deleteDocuments(new Term("id", "1")); 12 } catch (CorruptIndexException e) { 13 e.printStackTrace(); 14 } catch (LockObtainFailedException e) { 15 e.printStackTrace(); 16 } catch (IOException e) { 17 e.printStackTrace(); 18 } finally { 19 if (writer != null) { 20 try { 21 writer.close(); 22 } catch (CorruptIndexException e) { 23 e.printStackTrace(); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 } 28 } 29 }
测试代码:
1 @Test 2 public void testDelete(){ 3 LuceneUtil util = new LuceneUtil(); 4 System.out.println("删除前 ======="); 5 util.query(); 6 util.delete(); 7 System.out.println("删除后 ======="); 8 util.query(); 9 }
运行结果:
I'm Hongten