增加在入门程序创建索引中,增删改用IndexWriter.
1.获取IndexWriter的代码
// public IndexWriter getIndexWriter() throws Exception{ // 第一步:创建一个java工程,并导入jar包。 // 第二步:创建一个indexwriter对象。 Directory directory = FSDirectory.open(new File("E:\\lucene&solr\\index")); // Directory directory = new RAMDirectory();//保存索引到内存中 (内存索引库) Analyzer analyzer = new StandardAnalyzer();// 官方推荐 IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer); return new IndexWriter(directory, config); }
2.全删除:
1 //全删除 2 @Test 3 public void testAllDelete() throws Exception { 4 IndexWriter indexWriter = getIndexWriter(); 5 indexWriter.deleteAll(); 6 indexWriter.close(); 7 }
结果:
3.按条件删除
先创建索引:
删除索引
//根据条件删除 @Test public void testDelete() throws Exception { IndexWriter indexWriter = getIndexWriter(); Query query = new TermQuery(new Term("fileName","springmvc")); indexWriter.deleteDocuments(query); indexWriter.close(); }
结果:
第三个文档被删除,第五个文档被删除。
时间: 2024-10-10 04:24:08