using System; using System.Collections.Generic; using System.Linq; using Examine; using Examine.LuceneEngine.Providers; using Umbraco.Core.Models.PublishedContent; using Umbraco.Examine; using Umbraco.Web.PublishedCache; namespace Umbraco.Web { /// /// Extension methods for Examine. /// public static class ExamineExtensions { /// /// Creates an containing all content from the . /// /// The search results. /// The cache to fetch the content from. /// /// An containing all content. /// /// cache /// /// Search results are skipped if it can't be fetched from the by its integer id. /// public static IEnumerable ToPublishedSearchResults(this IEnumerable results, IPublishedCache cache) { if (cache == null) throw new ArgumentNullException(nameof(cache)); var publishedSearchResults = new List(); foreach (var result in results) { if (int.TryParse(result.Id, out var contentId) && cache.GetById(contentId) is IPublishedContent content) { publishedSearchResults.Add(new PublishedSearchResult(content, result.Score)); } } return publishedSearchResults; } /// /// Creates an containing all content, media or members from the . /// /// The search results. /// The snapshot. /// /// An containing all content, media or members. /// /// snapshot /// /// Search results are skipped if it can't be fetched from the respective cache by its integer id. /// public static IEnumerable ToPublishedSearchResults(this IEnumerable results, IPublishedSnapshot snapshot) { if (snapshot == null) throw new ArgumentNullException(nameof(snapshot)); var publishedSearchResults = new List(); foreach (var result in results) { if (int.TryParse(result.Id, out var contentId) && result.Values.TryGetValue(LuceneIndex.CategoryFieldName, out var indexType)) { IPublishedContent content; switch (indexType) { case IndexTypes.Content: content = snapshot.Content.GetById(contentId); break; case IndexTypes.Media: content = snapshot.Media.GetById(contentId); break; case IndexTypes.Member: content = snapshot.Members.GetById(contentId); break; default: continue; } if (content != null) { publishedSearchResults.Add(new PublishedSearchResult(content, result.Score)); } } } return publishedSearchResults; } } }