sheerockoff/bitrix-elastic-indexer

此包已被弃用,不再维护。未建议替代包。

将Bitrix信息块索引到Elasticsearch。

v0.1.5 2023-03-22 12:00 UTC

This package is auto-updated.

Last update: 2024-07-09 15:53:27 UTC


README

PHPUnit Coverage Scrutinizer

Bitrix数据信息块索引到Elasticsearch的帮助器。

安装

composer require sheerockoff/bitrix-elastic-indexer

快速入门

连接依赖项,创建 Elasticsearch 客户端。

<?php

use Elasticsearch\ClientBuilder;
use Sheerockoff\BitrixElastic\Indexer;

require 'vendor/autoload.php';

$elastic = ClientBuilder::create()->setHosts(['http://elasticsearch:9200'])->build();

创建 Indexer 实例。

$indexer = new Indexer($elastic);

默认情况下,当 Indexer 方法遇到错误时会抛出异常。要忽略某些异常,需要将 false 作为第二个参数(strictMode)传递给构造函数。

$indexer = new Indexer($elastic, false);

获取信息块的索引映射。

$infoBlockMapping = $indexer->getInfoBlockMapping($iBlockId);

更新Elasticsearch中的索引映射。方法只会更新当前索引中缺失的属性映射。现有属性的映射不会更改,以避免错误。

$indexer->putMapping('goods', $infoBlockMapping);

从Elasticsearch获取当前索引映射。

$elasticMapping = $indexer->getMapping('goods');

获取元素的索引原始数据。

/** @var _CIBElement $element */
$rawData = $indexer->getElementRawData($element);

根据Elasticsearch索引映射规范化索引的原始数据。

$normalizedData = $indexer->normalizeData($elasticMapping, $rawData);

将数据保存到Elasticsearch索引中。

$indexer->put('goods', $id, $normalizedData);

使用类似Bitrix格式的过滤器在索引中搜索。

$response = $indexer->search('goods', [
    'IBLOCK_ID' => 1,
    'SECTION_CODE' => 'mobile',
    'INCLUDE_SUBSECTIONS' => 'Y',
    'ACTIVE' => 'Y',
    '>CATALOG_PRICE_1' => 0,
    '>CATALOG_STORE_AMOUNT_1' => 0,
    'PROPERTY_TAGS' => ['hit', 'sale']
]);

排序也使用类似Bitrix的格式。

$response = $indexer->search('goods', ['ACTIVE' => 'Y'], [
    'CATALOG_PRICE_1' => 'ASC',
    'ID' => 'DESC'
]);

可以在 search 方法的最后一个参数中指定分页和其他查询参数。

$response = $indexer->search('goods', ['ACTIVE' => 'Y'], ['ID' => 'ASC'], [
    'from' => 40,
    'size' => 20
]);

开发和测试

在Docker容器中运行测试

docker compose up -d
docker compose exec bitrix composer install
docker compose exec bitrix vendor/bin/phpunit