Try our conversational search powered by Generative AI!

dada
Jan 28, 2022
  3795
(3 votes)

Configure your own Search & Navigation timeouts

Changing the timeout for Search & Navigation requests is something that has been requested over and over in forums and support cases for quite some time.
The default request timeout is set to 100 seconds (HttpWebRequest.Timeout default) which could be problematic when requests takes longer than expected.

You might know that you can set the request timeout in configuration

This will be used for all types of Find requests and could pose a problem if you want to lower it to 10 secs but at the same time not affect requests that should be allowed to run longer e.g bulk requests.

Timeout is set in milliseconds.

CMS 11 and lower

<appSettings><add key="episerver:FindDefaultRequestTimeout" value="30000" />

CMS 12

   "AppSettings": {
        "episerver:FindDefaultRequestTimeout": 30000
    }

More on configuration here https://world.optimizely.com/documentation/developer-guides/CMS/configuration/

But it's also possible with some code to set the timeout for a specific request

Add the following extension methods

public static ITypeSearch<TResult> SetTimeout<TResult>(this ITypeSearch<TResult> search, int timeout)
{
    return new Search<TResult, IQuery>(search, context =>
    {
        var existingAction = context.CommandAction;
        context.CommandAction = command =>
        {
            if (existingAction.IsNotNull())
            {
                existingAction(command);
            }
            command.ExplicitRequestTimeout = timeout;
        };

    });
}

public static ISearch<TResult> SetTimeout<TResult>(this ISearch<TResult> search, int timeout)
{
    return new Search<TResult, IQuery>(search, context =>
    {
        var existingAction = context.CommandAction;
        context.CommandAction = command =>
        {
            if (existingAction.IsNotNull())
            {
                existingAction(command);
            }
            command.ExplicitRequestTimeout = timeout;
        };

    });
}

public static IMultiSearch<TResult> SetTimeout<TResult>(this IMultiSearch<TResult> multiSearch, int timeout)
{
    var searches = new List<ISearch<TResult>>(multiSearch.Searches);
    multiSearch.Searches.Clear();
    foreach (var search in searches)
    {
        multiSearch.Searches.Add(SetTimeout(search, timeout));
    }
    return multiSearch;
}

And use it like this

var results = SearchClient.Instance
  .UnifiedSearch
  .For("random fruit")
  .SetTimeout(10000)
  .GetResult();

var results = searchClient.Search<Fruits>
  .For("banana")
  .SetTimeout(1000)
  .GetResult();

var results = searchClient.MultiSearch<Fruits>()
  .Search<Exotic>(x => x.For("Kiwi").InField(y => y.SearchTitle()))
  .Search<Ordinary>(x => x.For("Apple").InField(y => y.SearchTitle()))
  .SetTimeout(1000)
  .GetResult();


Make sure you catch your timeouts. They will throw a ServiceException.
More on Find exceptions you should consider catching is available in Jonas Bergqvist's blog post Exceptions in find 

Jan 28, 2022

Comments

Matthew Boniface
Matthew Boniface Oct 6, 2023 05:35 AM

Thanks for this post, really need this on most projects. However, I found two things didn't work for me:

1) CMS 12 DefaultRequestTimeout I found needed to be configured as:

{
  "EPiServer": {
    "Find": {
      //developer index
      "ServiceUrl": "https://demo02.find.episerver.net/xxxAAAyyyZZZ/",
      "DefaultIndex": "me_xx202310",
      "DefaultRequestTimeout": 10000,
      "ReindexMySitesOnly": true
    },

2) The IMultiSearch extension methods seemed to have no effect - no matter what I tried I found that it just used the DefaultRequestTimeout.

Please login to comment.
Latest blogs
Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog