This time I’ll start directly with the code.  First an utility method to create the connection:

[csharp]
private ElasticClient Connect(IEnumerable contents)
{
var defaultLanguageCode = "eng";
var uri = new System.Uri(ConfigurationManager.AppSettings["ElasticSearchServer"]);
var settings = new ConnectionSettings(uri).SetDefaultIndex(defaultLanguageCode);
var client = new ElasticClient(settings);
}
[/csharp]

And here’s the interesting part:

[csharp]
public IEnumerable<SearchItem> Search(string text, int page, int pageSize, IEnumerable<string> languages)
{
ElasticClient client = this.Connect();
IQueryResponse<SearchItem> searchResults = client.Search<SearchItem>(s => this.GetIndexSearchDescriptor(s, languages)
.QueryString(text)
.Skip(System.Math.Max(0, page) * pageSize)
.Take(pageSize));

if (searchResults.Total != 0 && searchResults.Hits != null && searchResults.Hits.Hits != null)
{
int totalPages = (int)System.Math.Ceiling((double)((float)searchResults.Total / (float)pageSize));
var results = searchResults.Hits.Hits;

return results.Select(h => h.Source).ToArray();
}
return Enumerable.Empty<SearchItem>();
}
[/csharp]

As you may see, the Search method takes the text parameter and a list of languages. In the last post we indexed the content translations using language codes (eg: eng, ita, esp and so on…) as index names. So the idea here is to use the GetIndexSearchDescriptor method to get a SearchDescriptor instance from the language codes and run a query using the text in input.
As a bonus I have added quick&dirty pagination just for the sake of it 😀

[csharp]
private SearchDescriptor<SearchItem> GetIndexSearchDescriptor(SearchDescriptor<SearchItem> s, IEnumerable<string> languages)
{
if (languages == null || !languages.Any<string>())
return s.AllIndices();

return s.Indices(languages);
}
[/csharp]