If you are using Episerver Social on your site, you may want to add the average rating of a page to the find index. Samples based on the SocialAlloy project by Episerver.
Read my blog here
If you are using Episerver Social on your site, you may want to add the average rating of a page to the find index. Samples based on the SocialAlloy project by Episerver.
Read my blog here
using System.Collections.Generic; | |
using System.Linq; | |
using EPiServer.Core; | |
using EPiServer.Filters; | |
using EPiServer.Framework.Web; | |
using EPiServer.ServiceLocation; | |
using EPiServer.SocialAlloy.Web.Social.Repositories; | |
/// <summary> | |
/// Extension methods for content | |
/// </summary> | |
public static class ContentExtensions | |
{ | |
/// <summary> | |
/// Gets the rating average for the specified <paramref name="content"/>. | |
/// </summary> | |
/// <param name="content">The content.</param> | |
/// <returns>The average rating for the content.</returns> | |
public static double GetRatingAverage(this IContent content) | |
{ | |
ISocialRatingRepository socialRatingRepository = | |
ServiceLocator.Current.GetInstance<ISocialRatingRepository>(); | |
return socialRatingRepository.GetRatingStatistics(content.ContentGuid.ToString())?.Average ?? 0; | |
} | |
} |
using System; | |
using EPiServer.Core; | |
using EPiServer.Find.Cms; | |
using EPiServer.ServiceLocation; | |
using EPiServer.Social.ActivityStreams.Core; | |
using EPiServer.SocialAlloy.Web.Social.Models; | |
/// <summary> | |
/// Class ContentRatedActivityHandler. | |
/// </summary> | |
/// <seealso cref="EPiServer.Social.ActivityStreams.Core.IActivityHandler{SocialRatingActivity}" /> | |
public class ContentRatedActivityHandler : IActivityHandler<SocialRatingActivity> | |
{ | |
/// <summary> | |
/// Handle the specified activity. | |
/// </summary> | |
/// <param name="activity">The activity to be handled</param> | |
/// <param name="extension">The payload of the activity being handled</param> | |
public void Handle(Activity activity, SocialRatingActivity extension) | |
{ | |
IContentRepository contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
IContent content = contentRepository.Get<IContent>(Guid.Parse(activity.Target.Id)); | |
HandleFindIndexing(content); | |
} | |
/// <summary> | |
/// Handles the find indexing. | |
/// </summary> | |
/// <param name="content">The content.</param> | |
private static void HandleFindIndexing(IContent content) | |
{ | |
bool shouldIndex; | |
bool ok = ContentIndexer.Instance.TryShouldIndex(content, out shouldIndex); | |
if (ok && shouldIndex) | |
{ | |
ContentIndexer.Instance.Index(content); | |
} | |
} | |
} |
using EPiServer.Core; | |
using EPiServer.Find.ClientConventions; | |
using EPiServer.Find.Framework; | |
using EPiServer.Framework; | |
using EPiServer.Framework.Initialization; | |
/// <summary> | |
/// Class FindInitializationModule. | |
/// </summary> | |
/// <seealso cref="EPiServer.Framework.IInitializableModule" /> | |
[InitializableModule] | |
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))] | |
public class FindInitializationModule : IInitializableModule | |
{ | |
/// <summary> | |
/// Initializes this instance. | |
/// </summary> | |
/// <param name="context">The context.</param> | |
/// <remarks>Gets called as part of the EPiServer Framework initialization sequence. Note that it will be called | |
/// only once per AppDomain, unless the method throws an exception. If an exception is thrown, the initialization | |
/// method will be called repeatedly for each request reaching the site until the method succeeds.</remarks> | |
public void Initialize(InitializationEngine context) | |
{ | |
SearchClient.Instance.Conventions.ForInstancesOf<IContent>().IncludeField(x => x.GetRatingAverage()); | |
} | |
/// <summary> | |
/// Resets the module into an uninitialized state. | |
/// </summary> | |
/// <param name="context">The context.</param> | |
/// <remarks><para> | |
/// This method is usually not called when running under a web application since the web app may be shut down very | |
/// abruptly, but your module should still implement it properly since it will make integration and unit testing | |
/// much simpler. | |
/// </para> | |
/// <para> | |
/// Any work done by <see cref="M:EPiServer.Framework.IInitializableModule.Initialize(EPiServer.Framework.Initialization.InitializationEngine)" /> as well as any code executing on <see cref="E:EPiServer.Framework.Initialization.InitializationEngine.InitComplete" /> should be reversed. | |
/// </para></remarks> | |
public void Uninitialize(InitializationEngine context) | |
{ | |
} | |
} |
IContentResult<SitePageData> contentResult = this.searchClient.Search<SitePageData>() | |
.Filter(c => c.GetRatingAverage().GreaterThan(0)) | |
.OrderByDescending(c => c.GetRatingAverage()).Take(25) | |
.GetContentResult(); |