Guides / Managing results / Optimize search results / Handling natural languages

By design the Algolia engine is language agnostic. This means that, out of the box, Algolia supports all languages and alphabets, including symbol-based languages such as Chinese, Japanese, and Korean.

Additionally, Algolia handles multi-languages on the same website/app, meaning 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 enable multi-language search.

The best solution for your use-case

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

  1. Does the ranking need to be different for each language? There are a couple of reasons the ranking might need 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 popularity scores per region)
  2. If you put all languages in one record, will the records be bigger than 10kb? Algolia limits record size to a maximum size of 10kb.

If you have multiple languages, you may need to insert a lot of text in your records. It’s important to determine if your records will end up being bigger than 10kb.

One or Two indices

If neither one of these are true, you can use one index where each record contains all languages.

Otherwise, you’ll need to use two indices.

In other words, you’ll need two indices if:

  • 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

Option 1: One index per language

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

1
2
3
4
5
6
[
  {
    "objectID": 1,
    "title": "The Wolf of Wall Street"
  }
]
1
2
3
4
5
6
[
  {
    "objectID": 1,
    "title": "Le loup de Wall Street"
  }
]
1
2
3
4
5
6
[
  {
    "objectID": 1,
    "title": "El lobo de Wall Street"
  }
]

Once your records are indexed in the different indices, you only need to select, from the front end, which index to target.

Option 2: Each record contains all languages

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

1
2
3
4
5
6
7
8
[
  {
    "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.

1
2
3
4
5
$client->initIndex("movies")->setSettings(array(
  "searchableAttributes" => array(
    "title_eng,title_fr,title_es"
  )
));

Then at query time you’ll need to specify which attributes you want searchable, depending on the language of the user. Here’s how to accomplish this with the JS API client:

1
2
3
4
5
6
// search only in the French titles
index.search('wolf', {
  'restrictSearchableAttributes': 'title_fr'
}).then(({ hits }) => {
  console.log(hits);
});

Did you find this page helpful?