Multilingual Search

Introduction

By design the engine is language agnostic. It supports out of the box – without any extra work – all languages / alphabets (including symbol based languages like Chinese, Japanese, Korean, Russian, …) used in the world.

Better than that, we can handle multi-languages on the same website/app: some users could search in French, and some in English, using the same Algolia account on the background.

The purpose of this guide is to explain how to organize your indices to set up a multi-language search.

The best solution for your use-case

There are different ways to handle multiple languages in your search. To determine the best solution for you, you need to answer the 2 following questions:

1. Does the ranking need to be different for each language?

There are multiple reasons for the ranking to change depending on the language:

  • The price is not the same in different countries (and you want to sort by price)
  • The object doesn’t have the same popularity in all regions of the world (and you have popularities scores per region)

2. If you put all languages in one record, will the records be bigger than 10kb?

Algolia limits the size of the records to a maximum size of 10kb.

If you have multiple languages, you may need to insert a lot of text in your records. Will your records end up being bigger than 10kb?

Pick your solution

If either one of the following is true:

  • You have a different ranking strategy depending on the language
  • If you put all languages in the same record, the size of the record would be bigger than 10kb

Then the only solution is to create one index per language. Otherwise, go with one index where each record contains all languages.

Option 1: One index per language

In this solution, you need to create one index per language:

// Index movies_eng
{
  "objectID": 1,
  "title": "The Wolf of Wall Street",
}, // …
// Index movies_fr
{
  "objectID": 1,
  "title": "Le loup de Wall Street",
}, // …
// Index movies_es
{
  "objectID": 1,
  "title": "El lobo de Wall Street",
}, // …

Once your records are indexed in the different indices, the only thing that you now need to do, is to decide - on the front-end - which index to target. Here’s how you can do it in Javascript, using our Javascript Helper:

// If the user wants to search in French
helper.setIndex("movies_fr");

Option 2: Each record contains all languages

In this solution, you’ll create one index containing all languages. Your records will look like that:

// Index movies
{
  "objectID": 1,
  "title_eng": "The Wolf of Wall Street",
  "title_fr": "Le loup de Wall Street",
  "title_es": "El lobo de Wall Street"
},
// …

What you now need to do is to set the attributes from all languages searchable, using searchableAttributes.

Algolia.init_index('movies').set_settings({"searchableAttributes"=>["title_eng,title_fr,title_es"]})
<?php
$client->initIndex("movies")->setSettings(array("searchableAttributes" => array("title_eng,title_fr,title_es")));
client.initIndex('movies').setSettings({searchableAttributes: "title_eng,title_fr,title_es"]});
client.init_index('movies').set_settings({"searchableAttributes":["title_eng,title_fr,title_es"]})
client.initIndex('movies').setSettings(new IndexSettings().setSearchableAttributes(Arrays.asList("title_eng", "title_fr", "title_es")));
val result: Future[Task] = client.execute {
  changeSettings of "myIndex" `with` IndexSettings(
    searchableAttributes = Some(Seq(AttributesToIndex.attributes("title_eng", "title_fr", "title_es"))),
  )
}
res, err := client.InitIndex("movies").SetSettings(algoliasearch.Map{
	"searchableAttributes": []string{"title_eng", "title_fr", "title_es"},
})

Then you’d need at query time, to specify in which attributes you actually want to search, depending on the language of the user. Here’s how you can do it in Javascript, using our Javascript Helper:

// Search only in the French title
helper.setQueryParameter('restrictSearchableAttributes',['title_fr']);

Did you find this page helpful?

We're always looking for advice to help improve our documentation! Please let us know what's working (or what's not!) - we're constantly iterating thanks to the feedback we receive.

Send us your suggestions!