重建Lucene的正确方法是什么;s指数
我有一个用Asp.NETMVC编写的类似论坛的web应用程序。我正试图实现Lucene.net作为搜索引擎。当我构建索引时,不时会出现与Lucene无法重命名重建Lucene的正确方法是什么;s指数,lucene,lucene.net,Lucene,Lucene.net,我有一个用Asp.NETMVC编写的类似论坛的web应用程序。我正试图实现Lucene.net作为搜索引擎。当我构建索引时,不时会出现与Lucene无法重命名deletable文件相关的异常。我想这是因为每次我想重建索引时都会清空它。以下是处理索引的代码: public class SearchService : ISearchService { Directory IndexFileLocation; IndexWriter Writer; IndexReader
deletable
文件相关的异常。我想这是因为每次我想重建索引时都会清空它。以下是处理索引的代码:
public class SearchService : ISearchService
{
Directory IndexFileLocation;
IndexWriter Writer;
IndexReader Reader;
Analyzer Analyzer;
public SearchService(String indexLocation)
{
IndexFileLocation = FSDirectory.GetDirectory(indexLocation, System.IO.Directory.Exists(indexLocation) == false);
Reader = IndexReader.Open(IndexFileLocation);
Writer = new IndexWriter(IndexFileLocation, Analyzer, IndexFileLocation.List().Length == 0);
Analyzer = new StandardAnalyzer();
}
public void ClearIndex()
{
var DocumentCount = Writer.DocCount();
if (DocumentCount == 0)
return;
for (int i = 0; i < DocumentCount; i++)
Reader.DeleteDocument(i);
}
public void AddToSearchIndex(ISearchableData Data)
{
Document Doc = new Document();
foreach (var Entry in Data)
{
Field field = new Field(Entry.Key,
Entry.Value,
Lucene.Net.Documents.Field.Store.NO,
Lucene.Net.Documents.Field.Index.TOKENIZED,
Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS);
Doc.Add(field);
}
Field KeyField = new Field(
SearchField.Key.ToString(),
Data.Key,
Lucene.Net.Documents.Field.Store.YES,
Lucene.Net.Documents.Field.Index.NO);
Doc.Add(KeyField);
Writer.AddDocument(Doc);
}
public void Dispose()
{
Writer.Optimize();
Writer.Close();
Reader.Close();
}
}
公共类搜索服务:ISearchService
{
目录索引文件位置;
索引作者;
索引阅读器;
分析仪;
公共搜索服务(字符串索引定位)
{
IndexFileLocation=FSDirectory.GetDirectory(indexLocation,System.IO.Directory.Exists(indexLocation)==false);
Reader=IndexReader.Open(IndexFileLocation);
Writer=newindexwriter(IndexFileLocation,Analyzer,IndexFileLocation.List()。长度==0);
Analyzer=新的StandardAnalyzer();
}
public void ClearIndex()
{
var DocumentCount=Writer.DocCount();
如果(DocumentCount==0)
返回;
对于(int i=0;i
下面是执行这一切的代码:
private void btnRebuildIndex_Click(object sender, EventArgs e)
{
using (var SearchService = new SearchService(Application.StartupPath + @"\indexs\"))
{
SearchService.ClearIndex();
}
using (var SearchService = new SearchService(Application.StartupPath + @"\indexs\"))
{
Int32 BatchSize = 50;
Int32 Current = 0;
var TotalQuestions = SubmissionService.GetQuestionsCount();
while (Current < TotalQuestions)
{
var Questions = SubmissionService.ListQuestions(Current, BatchSize, "Id", Qsparx.SortOrder.Asc);
foreach (var Question in Questions)
{
SearchService.AddToSearchIndex(Question.ToSearchableData());
}
Current += BatchSize;
}
}
}
private void btnRebuildIndex\u单击(对象发送方,事件参数e)
{
使用(var SearchService=newsearchservice(Application.StartupPath+@“\indexs\”)
{
SearchService.ClearIndex();
}
使用(var SearchService=newsearchservice(Application.StartupPath+@“\indexs\”)
{
Int32 BatchSize=50;
Int32电流=0;
var TotalQuestions=SubmissionService.GetQuestionsCount();
while(当前问题)
{
var Questions=SubmissionService.ListQuestions(当前,批量大小,“Id”,Qsparx.SortOrder.Asc);
foreach(问题中的var问题)
{
SearchService.AddToSearchIndex(Question.ToSearchableData());
}
当前+=批量大小;
}
}
}
为什么Lucene抱怨重命名“可删除”文件?不知道为什么每次都要重新创建索引。您可以将附加到索引,因此:
Writer = new IndexWriter(IndexFileLocation, Analyzer,false);
末尾的false标志告诉IndexWriter以追加模式打开(即不覆盖)。
这可能会解决您的问题。事实证明,如果不存在索引文件,那么在IndexWriter之前创建IndexReader不是一个好主意。我还意识到,即使IndexWriter的AddDocument方法有两个重载(一个是w/和一个是w/o Analyzer参数),但只有一个带有Analyzer参数的重载对我有效。IndexFileLocation.List()。只有在不存在索引文件时,Length==0才会计算为true