【问题标题】:What is the correct way to rebuild Lucene's index重建Lucene索引的正确方法是什么
【发布时间】:2010-07-24 07:10:21
【问题描述】:

我有一个用 Asp.net MVC 编写的类似 Web 应用程序的论坛。我正在尝试将 Lucene.net 实现为搜索引擎。当我建立我的索引时,我时不时会遇到与 Lucene 无法重命名 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();
    }
}

下面是执行这一切的代码:

    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;
            }
        }
    }

为什么 Lucene 抱怨重命名“可删除”文件?

【问题讨论】:

    标签: lucene lucene.net


    【解决方案1】:

    不确定为什么每次都重新创建索引。您可以这样追加到索引:

    Writer = new IndexWriter(IndexFileLocation, Analyzer,false);
    

    最后的 false 标志告诉 IndexWriter 以附加模式打开(即不覆盖)。 这可能会让你的问题消失。

    【讨论】:

    • IndexFileLocation.List().Length == 0 只有在不存在索引文件时才会计算为真
    【解决方案2】:

    事实证明,如果不存在索引文件,那么在 IndexWriter 之前创建 IndexReader 并不是一个好主意。我也意识到,即使 IndexWriter 的 AddDocument 方法有两个重载(一个带分析器参数,一个不带分析器参数),只有带分析器参数的一个对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-17
      • 2013-04-05
      • 2012-07-26
      • 2011-11-06
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多