/// <summary> /// Indexes the specified article to ElasticServer /// </summary> /// <param name="article">The article.</param> /// <param name="rssLookupKey">The rss lookup key.</param> /// <param name="client">The ElasticClient.</param> /// <returns>The method result.</returns> private static async Task <MethodResult> IndexArticle(Article article, ShortArticleKey rssLookupKey, IElasticClient client) { var result = await IndexArticle(article, client); if (result.IsSucceeded()) { var indexingResult = await client.IndexAsync(rssLookupKey, i => i.Index(AppConfiguration.ElasticRssSpeedIndexName)); if (!indexingResult.IsValid) { return(new MethodResult(SuccessState.UnknownError, indexingResult.DebugInformation)); } } return(result); }
/// <summary> /// Checks if the specified Article is a duplicate /// </summary> /// <param name="rssSpeedLookupKey">Rss key to check</param> /// <param name="client">The current client</param> /// <returns>A value indicating whether the article already exists.</returns> private static async Task <bool> IsArticleDuplicate(ShortArticleKey rssSpeedLookupKey, IElasticClient client) { var key = rssSpeedLookupKey; var articleExists = await client.SearchAsync <ShortArticleKey>(s => s .Size(1) .Index(AppConfiguration.ElasticRssSpeedIndexName) .Query(q => q.MatchPhrase(m => m.Field(f => f.Updated) .Query(key.Updated.ToString())) && q.MatchPhrase(m => m.Field(f => f.Link) .Query(key.Link.ToString())) && q.MatchPhrase(m => m.Field(f => f.SourceId) .Query(key.SourceId.ToString())))); return(articleExists.IsValid && articleExists.Documents.Any()); }
/// <summary> /// Indexes an RSS article thread safe. /// </summary> /// <param name="article">The article.</param> /// <param name="key">The rss key.</param> public async Task IndexRssFeedItemThreadSafeAsync(Article article, RssKey key) { #if ST lock (lockObj) #endif { try { //using (new PerfTracer(nameof(IndexRssFeedItemThreadSafeAsync))) { var client = new ElasticClient(settings); var speedKey = new ShortArticleKey(article.IndexingSourceId, key.Updated, key.Link); #if ST if (!(IsArticleDuplicate(speedKey, client).Result)) #else if (!(await IsArticleDuplicate(speedKey, client))) #endif { #if ST var result = IndexArticle(article, speedKey, client).Result; #else var result = await IndexArticle(article, speedKey, client); #endif if (result.Status == SuccessState.UnknownError) { throw new Exception("IndexArticle failed: " + result.UserMessage); } } } } catch (Exception e) { Console.WriteLine(e); } } }