Skip to content

Commit 8c7ebb6

Browse files
Add guide on multilingual datasets (#3359)
--------- Co-authored-by: gui machiavelli <[email protected]>
1 parent ddcce77 commit 8c7ebb6

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

docs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@
286286
"learn/indexing/rename_an_index",
287287
"learn/indexing/indexing_best_practices",
288288
"learn/indexing/ram_multithreading_performance",
289-
"learn/indexing/tokenization"
289+
"learn/indexing/tokenization",
290+
"learn/indexing/multilingual-datasets"
290291
]
291292
},
292293
{
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
title: Handling multilingual datasets
3+
description: This guide covers indexing strategies, language-specific tokenizers, and best practices for aligning document and query tokenization.
4+
---
5+
6+
When working with datasets that include content in multiple languages, it’s important to ensure that both documents and queries are processed correctly. This guide explains how to index and search multilingual datasets in Meilisearch, highlighting best practices, useful features, and what to avoid.
7+
8+
## Recommended indexing strategy
9+
10+
### Create a separate index for each language (recommended)
11+
12+
If you have a multilingual dataset, the best practice is to create one index per language.
13+
14+
#### Benefits
15+
16+
- Provides natural sharding of your data by language, making it easier to maintain and scale.
17+
18+
- Lets you apply language-specific settings, such as [stop words](/reference/api/settings#stop-words), and [separators](/reference/api/settings#separator-tokens).
19+
20+
- Simplifies the handling of complex languages like Chinese or Japanese, which require specialized tokenizers.
21+
22+
#### Searching across languages
23+
24+
If you want to allow users to search in more than one language at once, you can:
25+
26+
- Run a [multi-search](/reference/api/multi_search), querying several indexes in parallel.
27+
28+
- Use [federated search](/reference/api/multi_search#federated-multi-search-requests), aggregating results from multiple language indexes into a single response.
29+
30+
### Create a single index for multiple languages
31+
32+
In some cases, you may prefer to keep multiple languages in a **single index**. This approach is generally acceptable for proof of concepts or datasets with fewer than ~1M documents.
33+
34+
#### When it works well
35+
36+
- Suitable for languages that use spaces to separate words and share similar tokenization behavior (e.g., English, French, Italian, Spanish, Portuguese).
37+
38+
- Useful when you want a simple setup without maintaining multiple indexes.
39+
40+
#### Limitations
41+
42+
- Languages with compound words (like German) or diacritics that change meaning (like Swedish), as well as non-space-separated writing systems (like Chinese, or Japanese), work better in their own index since they require specialized [tokenizers](/learn/indexing/tokenization).
43+
44+
- Chinese and Japanese documents should not be mixed in the same field, since distinguishing between them automatically is very difficult. Each of these languages works best in its own dedicated index. However, if fields are strictly separated by language (e.g., title_zh always Chinese, title_ja always Japanese), it is possible to store them in the same index.
45+
46+
- As the number of documents and languages grows, performance and relevancy can decrease, since queries must run across a larger, mixed dataset.
47+
48+
#### Best practices for the single index approach
49+
50+
- Use language-specific field names with a prefix or suffix (e.g., title_fr, title_en, or fr_title).
51+
52+
- Declare these fields as [localized attributes](/reference/api/settings#localized-attributes) so Meilisearch can apply the correct tokenizer to each one.
53+
54+
- This allows you to filter and search by language, even when multiple languages are stored in the same index.
55+
56+
## Language detection and configuration
57+
58+
Accurate language detection is essential for applying the right tokenizer and normalization rules, which directly impact search quality.
59+
60+
By default, Meilisearch automatically detects the language of your documents and queries.
61+
62+
This automatic detection works well in most cases, especially with longer texts. However, results can vary depending on the type of input:
63+
64+
- **Documents**: detection is generally reliable for longer content, but short snippets may produce less accurate results.
65+
- **Queries**: short or partial inputs (such as type-as-you-search) are harder to identify correctly, making explicit configuration more important.
66+
67+
When you explicitly set `localizedAttributes` for documents and `locales` for queries, you **restrict the detection to the languages you’ve declared**.
68+
69+
**Benefits**:
70+
71+
- Meilisearch only chooses between the specified languages (e.g., English vs German).
72+
73+
- Detection is more **reliable and consistent**, reducing mismatches.
74+
75+
For search to work effectively, **queries must be tokenized and normalized in the same way as documents**. If strategies are not aligned, queries may fail to match even when the correct terms exist in the index.
76+
77+
### Aligning document and query tokenization
78+
79+
To keep queries and documents consistent, Meilisearch provides configuration options for both sides. Meilisearch uses the same `locales` configuration concept for both documents and queries:
80+
81+
- In **documents**, `locales` are declared through `localizedAttributes`.
82+
- In **queries**, `locales` are passed as a [search parameter].
83+
84+
#### Declaring locales for documents
85+
86+
The [`localizedAttributes` setting](/reference/api/settings#localized-attributes) allows you to explicitly define which languages are present in your dataset, and in which fields.
87+
88+
For example, if your dataset contains multilingual titles, you can declare which attribute belongs to which language:
89+
90+
```json
91+
{
92+
"id": 1,
93+
"title_en": "Danube Steamship Company",
94+
"title_de": "Donaudampfschifffahrtsgesellschaft",
95+
"title_fr": "Compagnie de navigation à vapeur du Danube"
96+
}
97+
```
98+
99+
```javascript
100+
client.index('INDEX_NAME').updateLocalizedAttributes([
101+
{ attributePatterns: ['*_en'], locales: ['eng'] },
102+
{ attributePatterns: ['*_de'], locales: ['deu'] },
103+
{ attributePatterns: ['*_fr'], locales: ['fra'] }
104+
])
105+
```
106+
107+
#### Specifying locales for queries
108+
109+
When performing searches, you can specify [query locales](/reference/api/search#query-locales) to ensure queries are tokenized with the correct rules.
110+
111+
```javascript
112+
client.index('INDEX_NAME').search('schiff', { locales: ['deu'] })
113+
```
114+
115+
This ensures queries are interpreted with the correct tokenizer and normalization rules, avoiding false mismatches.
116+
117+
## Conclusion
118+
119+
Handling multilingual datasets in Meilisearch requires careful planning of both indexing and querying.
120+
By choosing the right indexing strategy, and explicitly configuring languages with `localizedAttributes` and `locales`, you ensure that documents and queries are processed consistently.

0 commit comments

Comments
 (0)