Use snapshot to convert search results into content, media or members
This commit is contained in:
@@ -2,32 +2,95 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Examine;
|
||||
using Umbraco.Core;
|
||||
using Examine.LuceneEngine.Providers;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Examine;
|
||||
using Umbraco.Web.PublishedCache;
|
||||
|
||||
namespace Umbraco.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for Examine
|
||||
/// Extension methods for Examine.
|
||||
/// </summary>
|
||||
public static class ExamineExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates an <see cref="IEnumerable{PublishedSearchResult}" /> containing all content from the <paramref name="cache" />.
|
||||
/// </summary>
|
||||
/// <param name="results">The search results.</param>
|
||||
/// <param name="cache">The cache to fetch the content from.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="IEnumerable{PublishedSearchResult}" /> containing all content.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException">cache</exception>
|
||||
/// <remarks>
|
||||
/// Search results are skipped if it can't be fetched from the <paramref name="cache" /> by its integer id.
|
||||
/// </remarks>
|
||||
public static IEnumerable<PublishedSearchResult> ToPublishedSearchResults(this IEnumerable<ISearchResult> results, IPublishedCache cache)
|
||||
{
|
||||
var list = new List<PublishedSearchResult>();
|
||||
if (cache == null) throw new ArgumentNullException(nameof(cache));
|
||||
|
||||
var publishedSearchResults = new List<PublishedSearchResult>();
|
||||
|
||||
foreach (var result in results.OrderByDescending(x => x.Score))
|
||||
{
|
||||
if (!int.TryParse(result.Id, out var intId)) continue; //invalid
|
||||
var content = cache.GetById(intId);
|
||||
if (content == null) continue; // skip if this doesn't exist in the cache
|
||||
|
||||
list.Add(new PublishedSearchResult(content, result.Score));
|
||||
|
||||
if (int.TryParse(result.Id, out var contentId) &&
|
||||
cache.GetById(contentId) is IPublishedContent content)
|
||||
{
|
||||
publishedSearchResults.Add(new PublishedSearchResult(content, result.Score));
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
return publishedSearchResults;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="IEnumerable{PublishedSearchResult}" /> containing all content, media or members from the <paramref name="snapshot" />.
|
||||
/// </summary>
|
||||
/// <param name="results">The search results.</param>
|
||||
/// <param name="snapshot">The snapshot.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="IEnumerable{PublishedSearchResult}" /> containing all content, media or members.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException">snapshot</exception>
|
||||
/// <remarks>
|
||||
/// Search results are skipped if it can't be fetched from the respective cache by its integer id.
|
||||
/// </remarks>
|
||||
public static IEnumerable<PublishedSearchResult> ToPublishedSearchResults(this IEnumerable<ISearchResult> results, IPublishedSnapshot snapshot)
|
||||
{
|
||||
if (snapshot == null) throw new ArgumentNullException(nameof(snapshot));
|
||||
|
||||
var publishedSearchResults = new List<PublishedSearchResult>();
|
||||
|
||||
foreach (var result in results.OrderByDescending(x => x.Score))
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace Umbraco.Web
|
||||
IEnumerable<PublishedSearchResult> Search(string term, int skip, int take, out long totalRecords, string culture = "*", string indexName = null);
|
||||
|
||||
/// <summary>
|
||||
/// Executes the query and converts the results to <see cref="PublishedSearchResult" /> using the <see cref="Umbraco.Web.PublishedCache.IPublishedContentCache" />.
|
||||
/// Executes the query and converts the results to <see cref="PublishedSearchResult" />.
|
||||
/// </summary>
|
||||
/// <param name="query">The query.</param>
|
||||
/// <returns>
|
||||
@@ -83,7 +83,7 @@ namespace Umbraco.Web
|
||||
IEnumerable<PublishedSearchResult> Search(IQueryExecutor query);
|
||||
|
||||
/// <summary>
|
||||
/// Executes the query and converts the results to <see cref="PublishedSearchResult" /> using the <see cref="Umbraco.Web.PublishedCache.IPublishedContentCache" />.
|
||||
/// Executes the query and converts the results to <see cref="PublishedSearchResult" />.
|
||||
/// </summary>
|
||||
/// <param name="query">The query.</param>
|
||||
/// <param name="skip">The amount of results to skip.</param>
|
||||
@@ -92,9 +92,6 @@ namespace Umbraco.Web
|
||||
/// <returns>
|
||||
/// The search results.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// Make sure only content is searched (<c>searcher.CreateQuery(IndexTypes.Content)</c>), as this only returns content, but the <paramref name="totalRecords"/> could otherwise also include media records.
|
||||
/// </remarks>
|
||||
IEnumerable<PublishedSearchResult> Search(IQueryExecutor query, int skip, int take, out long totalRecords);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,7 +246,7 @@ namespace Umbraco.Web
|
||||
|
||||
totalRecords = results.TotalItemCount;
|
||||
|
||||
return results.Skip(skip).ToPublishedSearchResults(_publishedSnapshot.Content);
|
||||
return results.Skip(skip).ToPublishedSearchResults(_publishedSnapshot);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user