Mike Robbins

Sitecore Developer Blog

Follow me on GitHub

Sitecore ContentSearch Language Based Search

Just a quick blog on using Sitecore content search within multilingual sites.

A very basic way of achieving a multilingual search,  i've used in the past involves building a query to filter on the language from the Sitecore.Context.Language. This method is explained in a previous tweet from Jamie Little and a blog post, however a comment from Stephen Pope shed more detail on content search, where you can actually pass in a culture into the content search.

https://twitter.com/stephenpope/status/493034877051490305

So how does this work in practice

Within the Lucene or Solr configuration (e.g. "Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config") we can see that there is a collection of  analyzers specified along with the culture name the analyzer is for.

https://gist.github.com/dbea6aecd0fcb0a47d3d

Switching analyzer for the given language

When constructing the context.GetQueryable there is an overloaded method that accepts a CultureExecutionContext. This allows you to pass in a culture to use for the search, context search then uses the config to locate the correct analyzer for the given culture info.

In the example below, I get the current cultureInfo from the context language and pass this into the GetQueryable method. This allows content search to automatically switch between analyzers based on the current site language.

https://gist.github.com/775fa757f54ae4f6d2f0