Filter out best bets in typed search

Vote:
 

From https://world.optimizely.com/documentation/developer-guides/search-navigation/NET-Client-API/searching/Best-Bets/ i see that IsBestBet() (i states that the method is called IsBestBetStyle, but it seems to be a typo?) and HasBestBetStyle() only work for unified search. Is there now way of telling if a hit in types search is a result of best best settings?

Regards,

Bjørn Terje Svennes

Running .Net client version 13.4.4.0

#267725
Dec 02, 2021 11:58
Vote:
 

Yes, you can tell in Typed Search using the Best Bet Repository class.

Check out the ApplyBoostedProperties method in the Foundation Search Service

Foundation/SearchService.cs at develop · episerver/Foundation (github.com)

        /// <summary>
        /// Sets Featured Product property and Best Bet Product property to ProductViewModels.
        /// </summary>
        /// <param name="productViewModels">The ProductViewModels is added two properties: Featured Product and Best Bet.</param>
        /// <param name="searchResult">The search result (product list).</param>
        /// <param name="currentContent">The product category.</param>
        /// <param name="searchQuery">The search query string to filter Best Bet result.</param>
        private void ApplyBoostedProperties(ref List<ProductTileViewModel> productViewModels, IContentResult<EntryContentBase> searchResult, IContent currentContent, string searchQuery)
        {
            var node = currentContent as GenericNode;
            var products = new List<EntryContentBase>();

            if (node != null)
            {
                UpdateListWithFeatured(ref productViewModels, node);
            }

            var bestBetList = _bestBetRepository.List().Where(i => i.PhraseCriterion.Phrase.CompareTo(searchQuery) == 0);
            //Filter for product best bet only.
            var productBestBet = bestBetList.Where(i => i.BestBetSelector is CommerceBestBetSelector);
            var ownStyleBestBet = bestBetList.Where(i => i.BestBetSelector is CommerceBestBetSelector && i.HasOwnStyle);
            productViewModels.ToList()
                             .ForEach(p =>
                             {
                                 if (productBestBet.Any(i => ((CommerceBestBetSelector)i.BestBetSelector).ContentLink.ID == p.ProductId))
                                 {
                                     p.IsBestBetProduct = true;
                                 }
                                 if (ownStyleBestBet.Any(i => ((CommerceBestBetSelector)i.BestBetSelector).ContentLink.ID == p.ProductId))
                                 {
                                     p.HasBestBetStyle = true;
                                 }
                             });
        }
#267780
Dec 03, 2021 12:13
Vote:
 

Thank you very much, Johnny. This will do the trick.

#267784
Dec 03, 2021 14:22
Johnny Mullaney - Dec 03, 2021 14:23
No problem Bjorn. Glad my answer helped!
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.