即时更新Lucene索引

时间:2016-08-10 03:29:49

标签: lucene

我是Lucene的绝对新手,并且在更新索引时遇到了问题。

目前我可以每天重建整个索引,但索引只更新到构建的时间,但是如何更新索引,如附加它以使它是最新的?目前有代码尝试更新索引但它只更新段文件而不更新其他文件。

每当从我的网站添加一个条目时,它将运行RefreshFromDatabase方法并尝试添加最新的索引,但是在搜索索引文件夹中,它将更新两个文件的segment.gen和segments_t,但是所有其他文件(.fdt .fdx .fnm .frq .nrm .prx .tii .tis .del .cfs)未更新。 以下是屏幕截图:folder screenshot

代码:

using (ISiteScope scope = _scopeFactory.GetSiteScope(site)){
        scope.Get<ISearchIndexUpdater>().RefreshFromDatabase(primaryId, secondaryId);
        scope.Commit();
}

public void RefreshFromDatabase(long primaryId, int? secondaryId){
    Process process = _processRepo.GetById(primaryId);
    IList<Decision> allDecisions = _decisionRepo.GetByProcess(process);
    IList<Link> allLinks = _linkRepo.GetActiveByProcess(process);
    Decision current = allDecisions.OrderByDescending(x => x.DTG).FirstOrDefault();
    _luceneRepository.Add(process, allDecisions, allLinks);
}

public void Add(Process process, IList<Decision> decisions, IList<Link> links){
    if (null == decisions)
    decisions = new List<Decision>();

    using (LuceneWriter writer = BeginWriter(false)) {
        Add(writer.Writer,
            new SearchIndexProcess {
                // properties
            },
            decisions.Select(x => new SearchIndexDecision {
                // params
            }).ToArray(),
            (links ?? new List<Link>()).Select(x => new SearchIndexLink {
                // properties
            }).ToArray()
        );
        writer.Commit();
    }
}

和LuceneWriter类:

public class LuceneWriter : IDisposable
       {
              Directory _directory;
              Analyzer _analyzer;
              IndexWriter _indexWriter;

              bool _commit;
              bool _optimise;

              /// <summary>
              /// Constructor for LuceneWriter.
              /// </summary>
              /// <param name="fileSystem">An IFileSystem.</param>
              /// <param name="luceneDir">The directory that contains the Lucene index. Need not exist.</param>
              public LuceneWriter(IFileSystem fileSystem, string luceneDir)
                     : this(fileSystem, luceneDir, false)
              {
              }

              /// <summary>
              /// Constructor for LuceneWriter.
              /// </summary>
              /// <param name="fileSystem">An IFileSystem.</param>
              /// <param name="luceneDir">The directory that contains the Lucene index. Need not exist.</param>
              /// <param name="optimiseWhenDone">Optimse the index on Dispose(). This is an expensive operation.</param>
              public LuceneWriter(IFileSystem fileSystem, string luceneDir, bool optimiseWhenDone)
              {
                     Init(fileSystem, luceneDir, optimiseWhenDone);
              }

              //init has its own single use method for mocking reasons.
              /// <summary>
              /// Initialise the LuceneWriter.
              /// </summary>
              /// <param name="fileSystem">An IFileSystem.</param>
              /// <param name="luceneDir">The directory containing the Lucene index.</param>
              /// <param name="optimiseWhenDone">Whether or not to optimise the Lucene index upon Dispose().</param>
              protected virtual void Init(IFileSystem fileSystem, string luceneDir, bool optimiseWhenDone)
              {
                     _optimise = optimiseWhenDone;

                     bool exists = true;
                     if (!fileSystem.DirectoryExists(luceneDir)) {
                           fileSystem.CreateDirectory(luceneDir);
                           exists = false;
                     }

                     _directory = FSDirectory.Open(new DirectoryInfo(luceneDir));
                     _analyzer = new StandardAnalyzer(Version.LUCENE_30);
                     _indexWriter = new IndexWriter(_directory, _analyzer, !exists, IndexWriter.MaxFieldLength.UNLIMITED);
              }

              /// <summary>
              /// Flags writer to commit and optimise. Does not commit until Dispose() is called.
              /// </summary>
              public void Commit()
              {
                     _commit = true;
              }

              /// <summary>
              /// The IndexWriter.
              /// </summary>
              public IndexWriter Writer { get { return _indexWriter; } }

              /// <summary>
              /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
              /// </summary>
              /// <filterpriority>2</filterpriority>
              public void Dispose()
              {
                     if ((null != _indexWriter) && (_commit)) {
                           if (_optimise)
                                  _indexWriter.Optimize(true);
                           _indexWriter.Commit();
                           _indexWriter.Close(true);
                     }

                     if (null != _indexWriter)
                           _indexWriter.Dispose();
                     if (null != _analyzer)
                           _analyzer.Dispose();
                     if (null != _directory) {
                           _directory.Close();
                           _directory.Dispose();
                     }
              }
       }

1 个答案:

答案 0 :(得分:0)

在Lucene中没有更新文件。 它实际上是删除和添加。 当您更新文档时,在较旧的细分中,它将被标记为已删除,并且将创建新细分并将文档添加到该文档中