Try our conversational search powered by Generative AI!

Steven Galton
Jan 2, 2018
  2084
(3 votes)

Indexing extra Episerver Find properties onto a single variant for Episerver Commerce

Recently on a client project that I have been working on at Redweb, I have been working on getting Episerver Find working with Episerver Commerce. This was a simple task as you just needed to do some configuration and run the index job in the Commerce manager (Guide found here). 

The main issue that arose was that the catalogue structure was very complicated, and information was split across difference parents and children. This made it a bit difficult to get all the information needed for a single variant. Rather than doing multiple searches, I found that you could simply index this information into a single variant. 

To get the information to index onto the single variant, I created some properties that are ignored by the CMS. These would be used to store the actual information needed from the different parents of the variant.

[Ignore]
public string LocationName => SearchLocationName();

[Ignore]
public string Title => SearchTitleText();

[Ignore]
public string Description => SearchDescriptionText();

[Ignore]
public bool IsWeekendCourse => CourseRunType.HasFlag(CourseRunType.Weekend);

[Ignore]
public bool IsWeekdayCourse => CourseRunType.HasFlag(CourseRunType.Weekday);

Some more properties were added that would store a reference to the parents of the variant that were also hidden from the CMS. Some methods were created that would then extract the information from these properties and then assign them to the properties created above.

private CourseVenueProduct _parentCourseVenueProduct;
private CourseProduct _parentCourseProduct;
private CourseCategory _parentCourseCategory;
private CourseRunType ? _courseRunType;

[Ignore]
public CourseVenueProduct ParentCourseVenueProduct 
{
    get 
    {
        if (_parentCourseVenueProduct == null) 
        {
            var parentProduct = this.GetParentProducts().FirstOrDefault();
            var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
            _parentCourseVenueProduct = loader.Get<CourseVenueProduct>(parentProduct);
        }
        
        return _parentCourseVenueProduct;
    }
}

[Ignore]
public CourseProduct ParentCourseProduct 
{
    get 
    {
        if (_parentCourseProduct == null) 
        {
            var parentCourseProduct = this.ParentLink;
            var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
            _parentCourseProduct = loader.Get<CourseProduct>(parentCourseProduct);
        }
    
        return _parentCourseProduct;
    }
}

[Ignore]
public CourseCategory ParentCourseCategory 
{
    get 
    {
        if (_parentCourseCategory == null) 
        {
            var parentCourseCategory = ParentCourseProduct.ParentLink;
            var loader = ServiceLocator.Current.GetInstance<ContentLoader>();
            _parentCourseCategory = loader.Get<CourseCategory> (parentCourseCategory);
        }
        
        return _parentCourseCategory;
    }
}

[Ignore]
public CourseRunType CourseRunType 
{
    get 
    {
        if (_courseRunType == null) 
        {
            var workingDaysService = ServiceLocator.Current.GetInstance<IWorkingDays>();
            _courseRunType = workingDaysService.GetCourseRunType(CourseStartDate, CourseEndDate);
        }
    
        return _courseRunType.Value;
    }
}

private string SearchLocationName() 
{
    return ParentCourseVenueProduct.DisplayName;
}

private string SearchTitleText() 
{
    return ParentCourseCategory.DisplayName;
}

private string SearchDescriptionText() 
{
    return ParentCourseProduct.DisplayName;
}

When a new item was created, updated or the indexing job was run, the new properties would be updated and indexed so they could be used to filter against in the search queries. There was also an added benefit as you could also use these properties elsewhere in the solution.

Image Capture.PNG

[Pasting files is not allowed][Pasting files is not allowed]

Jan 02, 2018

Comments

Jeroen Stemerdink
Jeroen Stemerdink Jan 2, 2018 09:57 AM

I am not a big fan of adding properties with a lot of logic to your models, especially when it is only needed for your search index.

Have a ook at this thread, which is IMHO a much cleaner approach., and works just as well.

Steven Galton
Steven Galton Jan 2, 2018 10:11 AM

We used the convention way to add the properties to our search index before but realised that we needed to use the properties in other methods. We will be looking to move the logic into their own helpers in the future as well.

Please login to comment.
Latest blogs
Upgrade Optimizely CMS From v11 to v12

Why Upgrade? There are many improvements implemented in version 12, but most importantly is that the whole framework is migrated from .Net Framewor...

MilosR | May 13, 2024

Configured Commerce - Infrastructure Updates Ahoy!

I'm very happy to share an important milestone - we no longer have any customers in our legacy v1 environment!  This means that the Configured...

John McCarroll | May 10, 2024

A day in the life of an Optimizely Developer - Enabling Opti ID within your application

Hello and welcome to another instalment of A Day In The Life Of An Optimizely developer, in this blog post I will provide details on Optimizely's...

Graham Carr | May 9, 2024

How to add a custom property in Optimizely Graph

In the Optimizely CMS content can be synchronized to the Optimizely Graph service for it then to be exposed by the GraphQL API. In some cases, you...

Ynze | May 9, 2024 | Syndicated blog

New Security Improvement released for Optimizely CMS 11

A new security improvement has been released for Optimizely CMS 11. You should update now!

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog

Azure AI Language – Key Phrase Extraction in Optimizely CMS

In this article, I demonstrate how the key phrase extraction feature, offered by the Azure AI Language service, can be used to generate a list of k...

Anil Patel | May 7, 2024 | Syndicated blog