Implement Multi-Language Search in a Single Store
A step-by-step guide on how to perform a search in several languages simultaneously on a single store view using Algolia’s Magento 2 extension.
By default, the extension places each store view’s data in a separate index: each store view has a dedicated index in a specific language. For example, searching for Hemd in an English store won’t return any results. To show the correct results when searching for Hemd, the extension needs to be customized to merge those indices.
Examples of default records
Index name: magento2_products_en
1
2
3
4
5
{
"name": "Shirt"
"description": "Very nice blue shirt",
// ... other attributes
}
Index name: magento2_products_de
1
2
3
4
5
{
"name": "Hemd"
"description": "Sehr schönes blaues Hemd",
// ... other attributes
}
Index name: magento2_products_es
1
2
3
4
5
{
"name": "Camisa"
"description": "Muy bonita camisa azul",
// ... other attributes
}
How to implement multi-language search
The records must contain values in all supported languages to search on one store view in those languages.
How to create a record in the extension
- Create a custom extension, which will allow you to listen to the extension’s custom events.
- Write a listener on the
algolia_after_create_product_object
event, the same way as written in the custom event extensionalgolia_products_index_before_set_settings
. - From $observer
variable passed to the
execute() method, you can fetchcustom_data
,productObject
, andsubProducts
data. - The prepared Algolia record is structured as an
array
incustom_data
data and can be modified like any other PHP array. - Write custom code to fetch searchable attributes (like name, description, or manufacturer) in all other languages and add it to the record in
custom_data
. Structure the data according to the showcase record.
Sample of observer’s execute() method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public function execute(Observer $observer)
{
/* @var \Magento\Catalog\Model\Product $product */
$product = $observer->getData('productObject');
/*
* Fetch language-specific values based on $product
*/
$productRecord = $observer->getData('custom_data');
$originalName = $productRecord['name'];
$originalDescription = $productRecord['description'];
$productRecord['name'] = [
'en' => $originalName,
'de' => $germanName,
'es' => $spanishName,
];
$productRecord['description'] = [
'en' => $originalDescription,
'de' => $germanDescription,
'es' => $spanishDescription,
];
}
Index settings
If your records are structured like the showcase record, you don’t need to send specific settings to Algolia because only parent attributes (like name, description, or manufacturer) need to be set as searchable (and this can be done in the UI configuration of the extension in your Magento administration).
Showcase record
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"name": {
"en": "Shirt",
"de": "Hemd",
"es": "Camisa"
},
"description": {
"en": "Very nice blue shirt",
"de": "Sehr schönes blaues Hemd",
"es": "Muy bonita camisa azul"
},
// ... other attributes
}