diff --git a/examples/DancingGoat/Search/AdvancedSearchIndexingStrategy.cs b/examples/DancingGoat/Search/AdvancedSearchIndexingStrategy.cs index ba5978d..2b60a05 100644 --- a/examples/DancingGoat/Search/AdvancedSearchIndexingStrategy.cs +++ b/examples/DancingGoat/Search/AdvancedSearchIndexingStrategy.cs @@ -53,51 +53,48 @@ WebCrawlerService webCrawler // IIndexEventItemModel could be a reusable content item or a web page item, so we use // pattern matching to get access to the web page item specific type and fields - if (algoliaPageItem is IndexEventWebPageItemModel indexedPage) + if (algoliaPageItem is not IndexEventWebPageItemModel indexedPage) { - if (string.Equals(algoliaPageItem.ContentTypeName, ArticlePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase)) + return null; + } + if (string.Equals(algoliaPageItem.ContentTypeName, ArticlePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase)) + { + // The implementation of GetPage() is below + var page = await GetPage( + indexedPage.ItemGuid, + indexedPage.WebsiteChannelName, + indexedPage.LanguageName, + ArticlePage.CONTENT_TYPE_NAME); + + if (page is null) { - // The implementation of GetPage() is below - var page = await GetPage( - indexedPage.ItemGuid, - indexedPage.WebsiteChannelName, - indexedPage.LanguageName, - ArticlePage.CONTENT_TYPE_NAME); - - if (page is null) - { - return null; - } - - resultProperties.SortableTitle = resultProperties.Title = page?.ArticleTitle ?? string.Empty; - - string rawContent = await webCrawler.CrawlWebPage(page!); - resultProperties.Content = htmlSanitizer.SanitizeHtmlDocument(rawContent); + return null; } - else if (string.Equals(algoliaPageItem.ContentTypeName, HomePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase)) + + resultProperties.SortableTitle = resultProperties.Title = page?.ArticleTitle ?? string.Empty; + + string rawContent = await webCrawler.CrawlWebPage(page!); + resultProperties.Content = htmlSanitizer.SanitizeHtmlDocument(rawContent); + } + else if (string.Equals(algoliaPageItem.ContentTypeName, HomePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase)) + { + var page = await GetPage( + indexedPage.ItemGuid, + indexedPage.WebsiteChannelName, + indexedPage.LanguageName, + HomePage.CONTENT_TYPE_NAME); + + if (page is null) { - var page = await GetPage( - indexedPage.ItemGuid, - indexedPage.WebsiteChannelName, - indexedPage.LanguageName, - HomePage.CONTENT_TYPE_NAME); - - if (page is null) - { - return null; - } - - if (page.HomePageBanner.IsNullOrEmpty()) - { - return null; - } - - resultProperties.Title = page!.HomePageBanner.First().BannerHeaderText; + return null; } - else + + if (page.HomePageBanner.IsNullOrEmpty()) { return null; } + + resultProperties.Title = page!.HomePageBanner.First().BannerHeaderText; } else { diff --git a/examples/DancingGoat/Search/ReusableContentItemsIndexingStrategy.cs b/examples/DancingGoat/Search/ReusableContentItemsIndexingStrategy.cs index beba2e2..17c2e20 100644 --- a/examples/DancingGoat/Search/ReusableContentItemsIndexingStrategy.cs +++ b/examples/DancingGoat/Search/ReusableContentItemsIndexingStrategy.cs @@ -63,54 +63,51 @@ WebCrawlerService webCrawler // IIndexEventItemModel could be a reusable content item or a web page item, so we use // pattern matching to get access to the web page item specific type and fields - if (algoliaPageItem is IndexEventReusableItemModel indexedItem) + if (algoliaPageItem is not IndexEventReusableItemModel indexedItem) { - if (string.Equals(algoliaPageItem.ContentTypeName, Banner.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase)) + return null; + } + if (string.Equals(algoliaPageItem.ContentTypeName, Banner.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase)) + { + var query = new ContentItemQueryBuilder() + .ForContentType(HomePage.CONTENT_TYPE_NAME, + config => + config + .WithLinkedItems(4) + // Because the changedItem is a reusable content item, we don't have a website channel name to use here + // so we use a hardcoded channel name. + .ForWebsite(INDEXED_WEBSITECHANNEL_NAME) + // Retrieves all HomePages that link to the Banner through the HomePage.HomePageBanner field + .Linking(nameof(HomePage.HomePageBanner), new[] { indexedItem.ItemID })) + .InLanguage(indexedItem.LanguageName); + + var associatedWebPageItem = (await queryExecutor.GetWebPageResult(query, webPageMapper.Map)).First(); + string url = string.Empty; + try { - var query = new ContentItemQueryBuilder() - .ForContentType(HomePage.CONTENT_TYPE_NAME, - config => - config - .WithLinkedItems(4) - // Because the changedItem is a reusable content item, we don't have a website channel name to use here - // so we use a hardcoded channel name. - .ForWebsite(INDEXED_WEBSITECHANNEL_NAME) - // Retrieves all HomePages that link to the Banner through the HomePage.HomePageBanner field - .Linking(nameof(HomePage.HomePageBanner), new[] { indexedItem.ItemID })) - .InLanguage(indexedItem.LanguageName); - - var associatedWebPageItem = (await queryExecutor.GetWebPageResult(query, webPageMapper.Map)).First(); - string url = string.Empty; - try - { - url = (await urlRetriever.Retrieve(associatedWebPageItem.SystemFields.WebPageItemTreePath, - INDEXED_WEBSITECHANNEL_NAME, indexedItem.LanguageName)).RelativePath; - } - catch (Exception) - { - // Retrieve can throw an exception when processing a page update LuceneQueueItem - // and the page was deleted before the update task has processed. In this case, return no item. - return null; - } - - //If the indexed item is a reusable content item, we need to set the url manually. - resultProperties.Url = url; - resultProperties.SortableTitle = resultProperties.Title = associatedWebPageItem!.HomePageBanner.First().BannerText; - string rawContent = await webCrawler.CrawlWebPage(associatedWebPageItem!); - resultProperties.Content = htmlSanitizer.SanitizeHtmlDocument(rawContent); - - //If the indexed item is a reusable content item, we need to set the url manually. - var result = new List() - { - AssignProperties(resultProperties) - }; - - return result; + url = (await urlRetriever.Retrieve(associatedWebPageItem.SystemFields.WebPageItemTreePath, + INDEXED_WEBSITECHANNEL_NAME, indexedItem.LanguageName)).RelativePath; } - else + catch (Exception) { + // Retrieve can throw an exception when processing a page update LuceneQueueItem + // and the page was deleted before the update task has processed. In this case, return no item. return null; } + + //If the indexed item is a reusable content item, we need to set the url manually. + resultProperties.Url = url; + resultProperties.SortableTitle = resultProperties.Title = associatedWebPageItem!.HomePageBanner.First().BannerText; + string rawContent = await webCrawler.CrawlWebPage(associatedWebPageItem!); + resultProperties.Content = htmlSanitizer.SanitizeHtmlDocument(rawContent); + + //If the indexed item is a reusable content item, we need to set the url manually. + var result = new List() + { + AssignProperties(resultProperties) + }; + + return result; } else { diff --git a/examples/DancingGoat/Search/SimpleSearchIndexingStrategy.cs b/examples/DancingGoat/Search/SimpleSearchIndexingStrategy.cs index 2f33fac..0dac327 100644 --- a/examples/DancingGoat/Search/SimpleSearchIndexingStrategy.cs +++ b/examples/DancingGoat/Search/SimpleSearchIndexingStrategy.cs @@ -36,36 +36,33 @@ public override IndexSettings GetAlgoliaIndexSettings() => { var result = new List(); - string title = string.Empty; + string title; // IIndexEventItemModel could be a reusable content item or a web page item, so we use // pattern matching to get access to the web page item specific type and fields - if (algoliaPageItem is IndexEventWebPageItemModel indexedPage) + if (algoliaPageItem is not IndexEventWebPageItemModel indexedPage) { - if (string.Equals(algoliaPageItem.ContentTypeName, HomePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase)) + return null; + } + if (string.Equals(algoliaPageItem.ContentTypeName, HomePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase)) + { + var page = await GetPage( + indexedPage.ItemGuid, + indexedPage.WebsiteChannelName, + indexedPage.LanguageName, + HomePage.CONTENT_TYPE_NAME); + + if (page is null) { - var page = await GetPage( - indexedPage.ItemGuid, - indexedPage.WebsiteChannelName, - indexedPage.LanguageName, - HomePage.CONTENT_TYPE_NAME); - - if (page is null) - { - return null; - } - - if (page.HomePageBanner.IsNullOrEmpty()) - { - return null; - } - - title = page!.HomePageBanner.First().BannerHeaderText; + return null; } - else + + if (page.HomePageBanner.IsNullOrEmpty()) { return null; } + + title = page!.HomePageBanner.First().BannerHeaderText; } else {