C# Lucene.Net IndexWriter.DeleteDocuments 不工作
C# Lucene.Net IndexWriter.DeleteDocuments not working
我正在尝试使用 Lucene.Net 构建索引操作,并且我正在逐步测试代码,
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();
}
发现request通过Delete documents方法后,document还存在,writer.deletedocuments不行,我也试过添加Flush和commit,还是不行working.What在这种情况下怎么办?
尝试用这种方式删除您的文档,您也可以将目录对象作为参数传递给 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();
}
}
希望对您有所帮助!
您的搜索方式不清晰,所以我的同样问题已通过以下方式解决:
- 使用
writer.Flush(...)
(你做到了)
- 提交所有索引更改:
writer.Commit()
(此行已在您的代码中注释)
- 调用:
_searchManager.MaybeRefreshBlocking();
(推荐)
在代码中:
writer.Flush(true, true);
writer.Commit();
然后,在新搜索中:
_searchManager.MaybeRefreshBlocking();
var searcher = _searchManager.Acquire();
try
{
var topDocs = searcher.Search(query, 10);
// ... do your search logic here ...
}
finally
{
_searchManager.Release(searcher);
searcher = null;
}
我正在尝试使用 Lucene.Net 构建索引操作,并且我正在逐步测试代码,
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();
}
发现request通过Delete documents方法后,document还存在,writer.deletedocuments不行,我也试过添加Flush和commit,还是不行working.What在这种情况下怎么办?
尝试用这种方式删除您的文档,您也可以将目录对象作为参数传递给 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();
}
}
希望对您有所帮助!
您的搜索方式不清晰,所以我的同样问题已通过以下方式解决:
- 使用
writer.Flush(...)
(你做到了) - 提交所有索引更改:
writer.Commit()
(此行已在您的代码中注释) - 调用:
_searchManager.MaybeRefreshBlocking();
(推荐)
在代码中:
writer.Flush(true, true);
writer.Commit();
然后,在新搜索中:
_searchManager.MaybeRefreshBlocking();
var searcher = _searchManager.Acquire();
try
{
var topDocs = searcher.Search(query, 10);
// ... do your search logic here ...
}
finally
{
_searchManager.Release(searcher);
searcher = null;
}