我按照本文http://leoncullens.nl/post/2012/11/18/Full-Text-Search-on-Azure-with-LuceneNET.aspx设置了Lucene Indexing。
它在大多数情况下运行顺畅,如何编辑它,以便如果现有文档绑定到id以删除它然后重新插入它?
编辑以下方法:
public void CreateIndex() {
IndexWriter indexWriter = new IndexWriter(_directory, new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED); // Create the IndexWriter
foreach (Book book in _bookService.List()) // Use whatever data source you like
{
// NEED TO INSERT A CHECK HERE TO SEE IF A DOC EXISTS
Document document = CreateBookDocument(book); // Create a 'Document' for each row of your data
indexWriter.AddDocument(document);
}
try
{
indexWriter.Optimize(); // Call optimize once to improve performance
indexWriter.Dispose(); // Commit and dispose the object
Thread.Sleep(60 * 10 * 1000); // Sleep 10 minutes when the index is created successfully, otherwise immediately restart the process
}
catch (Exception)
{
indexWriter.Rollback();
indexWriter.Dispose();
}
}
private Document CreateBookDocument(Book book)
{
Document document = new Document();
document.Add(new Field("Id", book.Id.ToString(), Field.Store.YES, Field.Index.NO, Field.TermVector.NO)); // We want to store the ID so we can retrieve data for the ID, but we don't want to index it because it is useless searching through the IDs
document.Add(new Field("Title", book.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
document.Add(new Field("Publisher", book.Publisher, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
document.Add(new Field("Isbn10", book.Isbn10, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
return document;
}
答案 0 :(得分:1)
据我所知,实际上没有更新Lucene中的Document实例。它总是作为删除/插入完成。
答案 1 :(得分:1)
我们使用Lucene.Net 2.0.0.4,而对于3.0我认为它们是相同的。
首先您需要将ID添加为 Field.Index.TOKENIZED ,然后您可以修改(先删除然后插入)索引。
document.Add(new Field("Id", book.Id.ToString(), Field.Store.YES, Field.Index.TOKENIZED));
然后打开索引而不是创建新的
IndexWriter indexWriter = new IndexWriter(_directory, new StandardAnalyzer(Version.LUCENE_30), false, IndexWriter.MaxFieldLength.UNLIMITED);
有删除示例代码:
IndexReader reader = IndexReader.Open(_directory);
reader.DeleteDocuments(new Term("Id", book_id_delete));
reader.Close();