1、要删除的记录的依据应该具有唯一性,这样删除才有意义,并且这个字段在lucene存储时需要是ANALYZED,即可以被检索到
2、删除时的条件最好使用Query,而不要使用Term,我做过很多测试,结果证明Term条件总是不要使。
对于删除的代码如下:
- [WebMethod]
- public int DeleteLuceneDocument(string primaryKey, string module, string passKey)
- {
- int flag = 0;
- try
- {
- dirInfo = Directory.CreateDirectory(this.GetIndexPath(ConfigurationManager.AppSettings[module]));
- directory = LuceneIO.FSDirectory.Open(dirInfo);
- IndexWriter writer = new IndexWriter(directory, standardAnalyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
- QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "PrimaryKey", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
- Query query = parser.Parse(primaryKey);
- writer.DeleteDocuments(query);
- writer.Commit();
- writer.Optimize();
- writer.Close();
- flag = 1;
- }
- catch (Exception)
- {
- throw;
- }
- return flag;
- }
而