/// <summary> /// Creates the indexer. /// </summary> /// <returns></returns> private LuceneIndexer GetIndexer() { if (_indexer == null) { _indexer = new LuceneIndexer(this._indexModifier, _feedSources); } return(_indexer); }
/// <summary> /// Remove the feed (and all it's items) from the lucene search index. /// </summary> /// <param name="feedID">The feed url.</param> public void IndexRemove(string feedID) { if (!UseIndex || string.IsNullOrEmpty(feedID)) { return; } try { LuceneIndexer indexer = GetIndexer(); indexer.RemoveFeed(feedID); } catch (Exception ex) { Log.Error("Failure while remove item(s) from search index.", ex); } }
/// <summary> /// Remove the list of NewsItems from the lucene search index. /// </summary> /// <param name="newsItems">The news items list.</param> public void IndexRemove(INewsItem[] newsItems) { if (!UseIndex || newsItems == null) { return; } try { LuceneIndexer indexer = GetIndexer(); indexer.RemoveNewsItems(newsItems); } catch (Exception ex) { Log.Error("Failure while remove item(s) from search index.", ex); } }
/// <summary> /// Add the list of NewsItems to the lucene search index. /// </summary> /// <param name="newsItems">The news items list.</param> public void IndexAdd(IList <INewsItem> newsItems) { if (!UseIndex || newsItems == null) { return; } try { LuceneIndexer indexer = GetIndexer(); indexer.IndexNewsItems(newsItems); } catch (Exception ex) { Log.Error("Failure while add item(s) to search index.", ex); } }
// /// <summary> // /// Re-Index a feed. First, the feed gets removed completely from index, // /// then the items are added to index again. // /// </summary> // /// <param name="feedID">The feed URL.</param> // /// <param name="newsItems">The news items.</param> // public void ReIndex(string feedID, IList newsItems) { // if (!UseIndex) return; // //WASTODO: way too general, we should optimize that: // // re-indexing only the really new items, and remove only // // the purged items: // this.IndexRemove(feedID); // this.IndexAdd(newsItems); // } /// <summary> /// Re-Index a feed. First, the feed gets removed completely from index, /// then the items are added to index again. /// </summary> /// <param name="feed">The feed.</param> /// <param name="items">The NewsItems belonging to the feed</param> public void ReIndex(INewsFeed feed, IList <INewsItem> items) { if (!UseIndex || feed == null) { return; } try { LuceneIndexer indexer = GetIndexer(); indexer.RemoveNewsItems(feed.id); indexer.IndexNewsItems(items); } catch (Exception ex) { Log.Error("Failure while ReIndex item(s) in search index.", ex); } //this.IndexRemove(feed.id); //this.IndexAdd(newsHandler.GetCachedItemsForFeed(feed.link)); }
/// <summary> /// Checks the index. If it does not exists, this will start /// creating a new index. This process is recoverable/restartable and /// can/should be called each time the Search is expected to be used. /// </summary> /// <param name="force">if set to <c>true</c> a re-indexing of all /// items is started.</param> public void CheckIndex(bool force) { if (!UseIndex) { return; } bool restartIndexing = false; bool fileBasedIndex = this._settings.IndexPath != null && Directory.Exists(this._settings.IndexPath); string indexingStateFile = this.RestartIndexingStateFile; DateTime indexModifiedAt = DateTime.MinValue; if (fileBasedIndex) { indexModifiedAt = Directory.GetLastWriteTimeUtc(this._settings.IndexPath); bool indexStateFileExists = (indexingStateFile != null && File.Exists(indexingStateFile)); // if the index have to be (re-)created, we don't want to rely on old restart infos: if ((force || startIndexAll) && indexStateFileExists) { FileHelper.Delete(indexingStateFile); } // restart indexing: read state and go on with the next non-indexed feed restartIndexing = (!startIndexAll && indexStateFileExists); } else { startIndexAll = true; } if (force || restartIndexing || startIndexAll) { IDictionary restartInfo = null; DictionaryEntry lastIndexed = new DictionaryEntry(); if (restartIndexing) { // read indexState into restartInfo (persisted feed urls/id, that are yet indexed) restartInfo = ReadIndexingRestartStateFileContent(indexingStateFile, out lastIndexed); } if (restartInfo == null) { restartInfo = new HybridDictionary(); } if (startIndexAll) { this._indexModifier.CreateIndex(); } LuceneIndexer indexer = GetIndexer(); indexer.IndexingFinished += OnIndexingFinished; indexer.IndexingProgress += OnIndexingProgress; indexer.IndexAll(restartInfo, lastIndexed); } else if (fileBasedIndex) { // check, if we have to call OptimizeIndex(): DateTime lastIndexModification = this._settings.LastIndexOptimization; int compareResult = indexModifiedAt.CompareTo(lastIndexModification); // indexModifiedAt is greater than lastIndexModification, hence index was modified: if (compareResult > 0) { // attach event to get optimize index finished notification: this._indexModifier.FinishedIndexOperation += OnIndexModifierFinishedIndexOperation; this.IndexOptimize(); } else if (compareResult != 0) { // correct the bad persisted entry: this._settings.LastIndexOptimization = Directory.GetLastWriteTimeUtc(this._settings.IndexPath); } } }