2016-08-10 126 views
0

我是Lucene的絕對新手,在更新索引時遇到了問題。動態更新Lucene索引

目前我可以每天重建整個索引,但索引只更新到構建時間,但是如何更新索引像追加它,使其更新?目前有代碼嘗試更新索引,但它只更新段文件而不更新其他文件。

每次當我的網站添加一個條目時,它會運行RefreshFromDatabase方法並嘗試添加最新的索引,但是在搜索索引文件夾中,它會更新兩個文件segments.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(); 
        } 
       } 
     } 

回答

0

在Lucene中有沒有更新的文檔。 它實際上是刪除和添加。 當你更新文檔,在較舊的細分市場它將被標記爲刪除和新的細分市場將被創建和文件被添加到該

+0

是我實際上添加到它,但只有兩個段文件segments.gen和segments_t和.cfs文件被修改,其餘不被修改 – Xstaci

+0

是休息不會被修改,直到你做一個合併 – aravinth

+0

原諒我的無知,請告訴我怎麼做,謝謝。 或者它沒有必要合併,仍然執行「合併」? 我的情況是,我已在2/8/2016重建了索引,因此所有.prx .frq等文件都不會被修改,並且每次將記錄添加到數據庫時都有一種方法來編寫索引,所以.cfs文件不斷修改。我的問題是,索引是否實際更新? – Xstaci