When building a search experience for a multilingual audience, it's important to ensure that users receive results and query suggestions in their preferred language. This is particularly relevant in countries such as Switzerland or Belgium, where multiple languages are commonly used.
In this article, we'll look at one approach to handling multilingual content in Algolia by storing multiple language versions of each record in a single index and filtering results at query time. We'll also see how to configure Query Suggestions so that users receive suggestions in the correct language.
The Use Case
Suppose your application serves users in countries that support multiple languages. For example:
- Switzerland: French, German, Italian
One way to support this is to store all language versions of your products in a single Algolia index and apply a language filter whenever a user performs a search.
The Data Structure
Each translated version of a product is indexed as a separate record. For example, if a product is available in both English and Italian, the index could contain the following records:
[
{
"objectID": "bag_123_it",
"name": "Borsa",
"description": "Descrizione in italiano",
"language": "it"
},
{
"objectID": "bag_123_en",
"name": "Bag",
"description": "Description in English",
"language": "en"
}
]Each record:
- Contains translated values for all searchable attributes (such as
nameanddescription). - Includes a
languageattribute that identifies the record's language.
This approach increases the total number of records stored in the index, which may increase record usage. However, it allows you to manage all languages from a single index while returning results in the user's preferred language.
Filtering Results by Language
To ensure users only see results in their preferred language, configure the language attribute as an attributesForFaceting attribute, then apply a filter when searching:
This guarantees that only Italian results are returned.
If you'd like to analyse search behaviour by language, you can also add analytics tags to your searches:
Query Suggestions
Algolia's Query Suggestions can also be configured to return suggestions that match the user's selected language.
To do this:
Configure the Query Suggestions index so that the
languagefacet from the source index is available as a category- Configure Show top categories to be equal to or greater than the number of languages supported by your index. This ensures that language categories are retained when generating suggestions.
- Apply a language filter when querying the Query Suggestions index:
This ensures that users browsing in Italian only receive Italian query suggestions.
Alternative Approach
An alternative architecture is to create a separate Algolia index for each language (for example, products_en, products_it, and products_fr).
This removes the need to filter by language at query time and allows each language to have its own search configuration and relevance tuning. However, it also means managing multiple indices.
The best approach depends on your application's requirements, the number of supported languages, and how independently you want to manage each language's search experience.