Elasticsearch Integration with Bitrix CMS
The built-in Bitrix search relies on b_search_content and b_search_content_stem tables — a morphological index based on MySQL/MariaDB. When 50–100 thousand documents are indexed, full-text queries start producing delays of 200–500 ms. For catalogs with a million SKUs or when complex search filters are needed (price range, multiple facets simultaneously), the built-in search becomes a bottleneck. Elasticsearch solves both problems: it returns results in 5–30 ms and supports arbitrary aggregations.
Integration Architecture
The integration consists of three parts:
- Indexer — a component that reads data from Bitrix (infoblock elements, users, pages) and writes documents to the Elasticsearch index.
-
Search gateway — replaces standard
b_search_contentqueries with Elasticsearch API calls. - Event handlers — update the index when entities are modified or deleted.
Index Structure for Product Catalog
The index is created via Elasticsearch Mapping API. Example mapping for products:
PUT /bitrix_catalog
{
"mappings": {
"properties": {
"id": { "type": "integer" },
"iblock_id": { "type": "integer" },
"name": { "type": "text", "analyzer": "russian" },
"description": { "type": "text", "analyzer": "russian" },
"sku": { "type": "keyword" },
"price": { "type": "float" },
"active": { "type": "boolean" },
"section_id": { "type": "integer" },
"properties": { "type": "object" },
"updated_at": { "type": "date" }
}
},
"settings": {
"analysis": {
"analyzer": {
"russian": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "russian_stop", "russian_stemmer"]
}
},
"filter": {
"russian_stemmer": { "type": "stemmer", "language": "russian" },
"russian_stop": { "type": "stop", "stopwords": "_russian_" }
}
}
}
}
The russian analyzer with stemmer is the key difference from MySQL FULLTEXT, which cannot understand morphology without additional dictionaries.
Data Indexing
Primary indexing is launched via a cron script. Data is read in batches through CIBlockElement::GetList() with nTopCount = 100 and offset to avoid memory overload:
\Bitrix\Main\Loader::includeModule('iblock');
$client = new \Elasticsearch\ClientBuilder::create()
->setHosts(['localhost:9200'])
->build();
$offset = 0;
$batchSize = 100;
do {
$res = \CIBlockElement::GetList(
[],
['IBLOCK_ID' => CATALOG_IBLOCK_ID, 'ACTIVE' => 'Y'],
false,
['nTopCount' => $batchSize, 'nPageSize' => $batchSize, 'iNumPage' => floor($offset / $batchSize) + 1],
['ID', 'NAME', 'DETAIL_TEXT', 'IBLOCK_ID', 'IBLOCK_SECTION_ID']
);
$bulk = [];
while ($item = $res->GetNext()) {
$bulk[] = ['index' => ['_index' => 'bitrix_catalog', '_id' => $item['ID']]];
$bulk[] = [
'id' => (int)$item['ID'],
'iblock_id' => (int)$item['IBLOCK_ID'],
'name' => $item['NAME'],
'description'=> strip_tags($item['DETAIL_TEXT']),
'section_id' => (int)$item['IBLOCK_SECTION_ID'],
'active' => true,
'updated_at' => date('c'),
];
$offset++;
}
if (!empty($bulk)) {
$client->bulk(['body' => $bulk]);
}
} while ($res->SelectedRowsCount() === $batchSize);
The Bulk API allows sending up to 1000 documents per request. Do not use individual index requests for primary indexing — that's 10–50 times slower.
Index Update on Product Change
Subscribe to infoblock events:
// local/php_interface/init.php
AddEventHandler('iblock', 'OnAfterIBlockElementUpdate', 'esUpdateProduct');
AddEventHandler('iblock', 'OnAfterIBlockElementDelete', 'esDeleteProduct');
function esUpdateProduct(array &$arFields): void
{
$client = getEsClient();
$productId = (int)$arFields['ID'];
// Reindex single document
$client->index([
'index' => 'bitrix_catalog',
'id' => $productId,
'body' => buildProductDocument($productId),
]);
}
function esDeleteProduct(int $productId): void
{
getEsClient()->delete(['index' => 'bitrix_catalog', 'id' => $productId]);
}
The OnAfterIBlockElementUpdate event fires even when changing data via API (importing from 1C), which is important for index freshness.
Search Query
Replacing the standard bitrix:search.page component with a custom one that calls Elasticsearch:
$response = $client->search([
'index' => 'bitrix_catalog',
'body' => [
'query' => [
'multi_match' => [
'query' => $searchQuery,
'fields' => ['name^3', 'description', 'sku'],
'type' => 'best_fields',
'fuzziness' => 'AUTO',
],
],
'sort' => ['_score' => ['order' => 'desc']],
'from' => ($page - 1) * $pageSize,
'size' => $pageSize,
],
]);
The fuzziness: AUTO parameter enables typo-tolerant search: for words up to 5 characters, 1 substitution is allowed; for longer words, 2 are allowed.
Implementation Timeline
| Scale | Scope | Timeline |
|---|---|---|
| Basic | ES installation, mapping, indexer, search gateway | 5–7 days |
| Full | Faceted search via aggregations, suggestions (suggest), synonyms, autocomplete | 10–14 days |







