Beispiel #1
0
        public DocumentIndexer(ITaskScheduler scheduler, TimeSpan?refreshIndexSearcherThreadInterval = null, TimeSpan?commitChangesThreadInterval = null)
        {
            try
            {
                _scheduler = scheduler;
                var solutionKey   = ServiceLocator.Resolve <SolutionKey>();
                var directoryInfo = new System.IO.DirectoryInfo(PathManager.Instance.GetIndexPath(solutionKey));
                LuceneIndexesDirectory = FSDirectory.Open(directoryInfo);
                Analyzer       = ServiceLocator.Resolve <Analyzer>();
                IndexWriter    = new IndexWriter(LuceneIndexesDirectory, Analyzer, IndexWriter.MaxFieldLength.LIMITED);
                Reader         = IndexWriter.GetReader();
                _indexSearcher = new IndexSearcher(Reader);
                QueryParser    = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, Configuration.Configuration.GetValue("DefaultSearchFieldName"), Analyzer);

                if (!refreshIndexSearcherThreadInterval.HasValue)
                {
                    refreshIndexSearcherThreadInterval = TimeSpan.FromSeconds(10);
                }
                var refreshIndexSearcherBackgroundWorker = new BackgroundWorker {
                    WorkerReportsProgress = false, WorkerSupportsCancellation = false
                };
                refreshIndexSearcherBackgroundWorker.DoWork += PeriodicallyRefreshIndexSearcherIfNeeded;
                refreshIndexSearcherBackgroundWorker.RunWorkerAsync(refreshIndexSearcherThreadInterval);

                if (commitChangesThreadInterval.HasValue)
                {
                    var commitChangesBackgroundWorker = new BackgroundWorker
                    {
                        WorkerReportsProgress      = false,
                        WorkerSupportsCancellation = false
                    };
                    commitChangesBackgroundWorker.DoWork += PeriodicallyCommitChangesIfNeeded;
                    commitChangesBackgroundWorker.RunWorkerAsync(commitChangesThreadInterval);
                }
                else
                {
                    _synchronousCommits = true;
                }
            }
            catch (CorruptIndexException corruptIndexEx)
            {
                LogEvents.IndexCorruptError(this, corruptIndexEx);
                throw new IndexerException(TranslationCode.Exception_Indexer_LuceneIndexIsCorrupt, corruptIndexEx);
            }
            catch (LockObtainFailedException lockObtainFailedEx)
            {
                LogEvents.IndexLockObtainFailed(this, lockObtainFailedEx);
                throw new IndexerException(TranslationCode.Exception_Indexer_LuceneIndexAlreadyOpened, lockObtainFailedEx);
            }
            catch (System.IO.IOException ioEx)
            {
                LogEvents.IndexIOError(this, ioEx);
                throw new IndexerException(TranslationCode.Exception_General_IOException, ioEx, ioEx.Message);
            }
        }