A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

Steven Galton
Apr 10, 2018
  578
(0 votes)

Managing Episerver Find Exceptions when querying the index

I have been working on the Episerver Find implementation here at Redweb for a client build. I was noticing that there were some errors that would occasionally happen when performing some queries against the index. After having a look around, I came across this article that gave me a solution to the issue and thought I would share the implementation into the project.

Firstly, I put a try catch around any of the Find services when they would be making a query against the index so that any errors would be caught. I can then check to see if the error is a ClientException or a ServiceException. If they are I can log the error with the exception to make sure there isn’t an issue with what we have written.

try
{
    var search = _findClient.Search<SupplierProduct>();

    if (!productCategoryId.IsNullOrWhiteSpace())
    {
        search = search.Filter(x => x.ParentCategory.Match(categoryId));
    }

    if (!string.IsNullOrWhiteSpace(queryString))
    {
        search = search.For(queryString).InField(x => x.Name);
    }

    if (!facetsList.IsNullOrEmpty())
    {
        search = search.MatchCategories(facetsList);
    }
    
    var supplierResults = search
        .ApplyBestBets()
        .UsingAutoBoost()
        .Skip((currentPage - 1) * resultsPerPage)
        .Take(resultsPerPage)
        .StaticallyCacheFor(TimeSpan.FromMinutes(1))
        .GetContentResult();

    return supplierResults;
}
catch (Exception ex) when(ex is ClientException || ex is ServiceException)
{
    Services.Episerver.Logger.Log(Level.Error, "Shop Search encountered an Episerver Find Exception", ex);
    return new EmptyContentResult<SupplierProduct>();
}

To make sure that the user has the best experience possible, a new type of content result was created. This means that if an error occurs an EmptyContentResult would be returned rather than an error. The new content result would have no results programmed set within the properties and can have the type passed into it so that it returns the correct class type for the search being performed.

public class EmptyContentResult<T> : ContentResult<T> where T : IContentData
{
    public EmptyContentResult() : base(
        Enumerable.Empty<T>(),
        new SearchResults<ContentInLanguageReference>(
            new SearchResult<ContentInLanguageReference>()
            {
                Facets = new FacetResults(),
                Hits = new HitCollection<ContentInLanguageReference>()
                {
                    Hits = Enumerable.Empty<SearchHit<ContentInLanguageReference>>().ToList()
                },
                Shards = new Shards()
            }))
    { }
}

This means that if any errors occur, although an empty page would show to the user, it would not break their view of the site and handle the error in an appropriate manner. This was very quick to implement across all the search methods on the site and also catches any of the errors that could occur out of our control.

I hope this helps some people out and I have also linked to the original source if anyone wants to look at it as well.

Apr 10, 2018

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP: Learning Optimizely Just Got Easier: Introducing the Optimizely Learning Centre

On the back of my last post about the Opti Graph Learning Centre, I am now happy to announce a revamped interactive learning platform that makes...

Graham Carr | Jan 31, 2026

Scheduled job for deleting content types and all related content

In my previous blog post which was about getting an overview of your sites content https://world.optimizely.com/blogs/Per-Nergard/Dates/2026/1/sche...

Per Nergård (MVP) | Jan 30, 2026

Working With Applications in Optimizely CMS 13

💡 Note:  The following content has been written based on Optimizely CMS 13 Preview 2 and may not accurately reflect the final release version. As...

Mark Stott | Jan 30, 2026

Experimentation at Speed Using Optimizely Opal and Web Experimentation

If you are working in experimentation, you will know that speed matters. The quicker you can go from idea to implementation, the faster you can...

Minesh Shah (Netcel) | Jan 30, 2026