Welcome to the navigation

Ad magna dolor enim occaecat mollit duis ut consectetur proident, sunt laboris consequat, do cillum ipsum deserunt nulla velit id nisi non reprehenderit quis voluptate. Labore ut irure deserunt laborum, in consequat, tempor ut adipisicing ea officia ipsum nulla dolor anim voluptate cillum eu velit est nisi laboris sed id

Yeah, this will be replaced... But please enjoy the search!

Episerver Find error 400 when using synonyms

I got a 400 error from Episerver Find when I published ran my search implementation on a customer test environment. The only thing that was different from my development environment (where the code worked) was the Find Index.

Excerpt of the exception

The remote server returned an error: (400) Bad Request.
SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;

Well that didn't say anything. The query generating the exception

var lang = Thread.CurrentThread.CurrentCulture;

var language =
    Language.GetAll()
        .FirstOrDefault(x => 
            x.Name.Equals(lang.Name, StringComparison.CurrentCultureIgnoreCase));

var query = SearchClient.Instance.UnifiedSearch(language: Language.language)
    .For(searchQuery.Query)
    .AndInField(x => x.SearchCategories)
    .UsingSynonyms()
    .Filter(SiteFilterBuilder)
    .Track()
    .Take(1000);

Since this is placed in a Web Api I use the Thread Culture to be able to resolve the correct language, next I fetched the language from the Language class. This is similar to stating the language using the static properties

SearchClient.Instance.UnifiedSearch(language: Language.English)

There's nothing wrong with the query itself and the language branches are clearly visible in the Find Overview. 

I forgot that Find doesn't work that way and that this view is in no way related to your service settings. Since I have no access to the index service settings I had to rely on the api's to check the service configuration.

Available language branches

The following query helped me out

// construct a searchClient using IClient
IClient _searchClient = Client.CreateFromConfig(); var languages = _searchClient.Settings.Languages;

// or by using the singleton
var languages = SearchClient.Instance.Settings.Languages;

When using my developer index I had two languages

And when using the index from the test environment I only had swedish

This means that

  • Find will allow you to send multiple languages to the index regardless of your language branch settings
  • Find will allow you to fetch any page that is indexed regardless of your language branch settings as long as you do not state a language that isn't configured in the settings
  • Find will not allow you to run complex language specific queries on languages that are not setup in the index service

The last bullet is where my query failed, if I remove the UsingSynonyms extension from the query it runs just fine, except from not returning any results of course since I had set the language parameter. 

Solution

First of all, dont use the pattern I used above, instead use IClient settings. Second, contact Episerver and tell them to re-configure your index with the missing languages.

var lang = Thread.CurrentThread.CurrentCulture; // or whatever works for you

var query = SearchClient.Instance.UnifiedSearch(
        language: SearchClient.Instance.Settings.Languages.GetSupportedLanguage(lang) // possibly use the SearchClient.Instance singleton instead 
                    ?? Language.None)
    .For(searchQuery.Query)
    .AndInField(x => x.SearchCategories) // maybe
    .UsingSynonyms()
    .Track()
    .Take(1000);

var result = query.GetResult();

Much better!

Please note that this portal is a BETA site , I will continuously improve all functionality and performance during the coming weeks / the author