作者:Wang嘉伟 | 来源:互联网 | 2023-09-10 19:00
增加在入门程序创建索引中,增删改用IndexWriter.1.获取IndexWriter的代码publicIndexWritergetIndexWriter()th
增加在入门程序创建索引中,增删改用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.按条件删除
先创建索引:
删除索引
//根据条件删除@Testpublic void testDelete() throws Exception {IndexWriter indexWriter = getIndexWriter();Query query = new TermQuery(new Term("fileName","springmvc"));indexWriter.deleteDocuments(query);indexWriter.close();}
结果:
第三个文档被删除,第五个文档被删除。