2016-08-10 6 views
0

私はLuceneの初心者です。インデックスを更新する際に問題が発生しました。オンザフライでLuceneインデックスを更新する

現在、インデックス全体を1日に再構築することができますが、インデックスはビルド時にのみ更新されますが、インデックスを追加して最新の状態に更新する方法はありますか?現在、索引を更新しようとするコードがありますが、セグメントファイルのみが更新され、他のファイルは更新されません。

私のウェブサイトからエントリが追加されるたびにRefreshFromDatabaseメソッドが実行され、最新のインデックスを追加しようとしますが、検索インデックスフォルダには2つのファイル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(); 
        } 
       } 
     } 

答えて

0

Luceneのでは何の更新文書はありません。 実際には削除して追加します。 ドキュメントを更新すると、古いセグメントでは削除済みとマークされ、新しいセグメントが作成され、そのドキュメントが追加されます

+0

はい実際には2つのセグメントファイルsegment.genとsegments_tおよび.cfsファイルが変更され、残りの部分は変更されません – Xstaci

+0

マージを行うまで残りは変更されません – aravinth

+0

私の無知を許してください。 また、マージする必要はなく、「マージされた」状態で実行することはできますか? 私の状況は、2016年2月8日にインデックスを再構築したので、.prx .frqなどのファイルはすべて変更されず、レコードがデータベースに追加されるたびにインデックスを書き込むメソッドがあります.cfsファイルは継続的に変更されます。私の質問は、実際にインデックスが更新されているかどうかです。 – Xstaci

関連する問題