2016-08-10 124 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