简体   繁体   English

C#Lucene.Net IndexWriter.DeleteDocuments无法正常工作

[英]C# Lucene.Net IndexWriter.DeleteDocuments not working

I'm trying to build an operation of Index by using Lucene.Net, and I was testing the code step by step, 我正在尝试使用Lucene.Net构建Index的操作,并且我正在逐步测试代码,

private void CRUDIndex()
    {


        FSDirectory directory = FSDirectory.Open(new DirectoryInfo(Path), new NativeFSLockFactory());


        bool isExist = IndexReader.IndexExists(directory);
        if (isExist)
        {
            if (IndexWriter.IsLocked(directory))
            {
                IndexWriter.Unlock(directory);
            }
        }
        IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isExist, IndexWriter.MaxFieldLength.UNLIMITED);
        while (bookQueue.Count > 0)
        {
if (book.IT == IndexType.Insert)
            {
                document.Add(new Field("id", book.ID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.Add(new Field("title", book.Title, Field.Store.YES, Field.Index.ANALYZED,
                                       Field.TermVector.WITH_POSITIONS_OFFSETS));
                document.Add(new Field("content", book.Starring, Field.Store.YES, Field.Index.ANALYZED,
                                       Field.TermVector.WITH_POSITIONS_OFFSETS));
                writer.AddDocument(document);
            }
            else if (book.IT == IndexType.Delete)
            {


                writer.DeleteDocuments(new Term("id", book.ID.ToString()));
                writer.Optimize();
                //writer.Commit();
                writer.Flush(true, true, true);
                //writer.Dispose();
            }
}
        writer.Dispose();
        directory.Dispose();
    }

I realized that after the request passed over Delete documents method, the document still exists, writer.deletedocuments is not working, I also tried to add Flush and commit, it's still not working.What should I do in this case? 我意识到在请求传递到Deletedocument方法之后,该文档仍然存在,writer.deletedocuments无法正常工作,我还尝试添加Flush并提交,但仍无法正常工作,在这种情况下我该怎么办?

Try this way to delete your documents you can also pass your directory object to the ClearLuceneIndexRecord method as parameter. 尝试通过这种方式删除文档,您也可以将目录对象作为参数传递给ClearLuceneIndexRecord方法。

public static void ClearLuceneIndexRecord(int record_id)
 {
   // init lucene
   var analyzer = new PanGuAnalyzer();
   using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
   {
     // remove older index entry
     var DocIdToDelete= new TermQuery(new Term("id", record_id.ToString()));
     writer.DeleteDocuments(DocIdToDelete);
     // close handles
     analyzer.Close();
     writer.Dispose();
   }
}

Hope it will help you! 希望对您有帮助!

It's not clean how you do Search, so my same issue was solved by: 搜索的方式不干净,因此我的相同问题已通过以下方式解决:

  1. using writer.Flush(...) (you did) 使用writer.Flush(...) (您做了)
  2. commit all Index changes: writer.Commit() (this line is commented in your code) 提交所有索引更改: writer.Commit() (此行在您的代码中已注释)
  3. call: _searchManager.MaybeRefreshBlocking(); 呼叫: _searchManager.MaybeRefreshBlocking(); (recommended) (推荐的)

In code: 在代码中:

    writer.Flush(true, true);
    writer.Commit();

and then, in new search: 然后,在新搜索中:

    _searchManager.MaybeRefreshBlocking();

    var searcher = _searchManager.Acquire();
    try
    {
        var topDocs = searcher.Search(query, 10);

        // ... do your search logic here ...
    }
    finally
    {
        _searchManager.Release(searcher);
        searcher = null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM