olegpro/bitrix-sphinx
此包为 1С-Битри克斯添加了通过 ORM D7 与 sphinx 索引交互的功能
v0.3.0
2021-06-27 15:12 UTC
Requires
- php: >=7.4
README
此包为 1С-Битри克斯添加了通过 ORM D7 与 sphinx 索引交互的功能,如同使用常见的实体(例如 \Bitrix\Iblock\ElementTable 或 \Bitrix\Catalog\PriceTable)一样
配置
创建 ORM 实体类
<?php use Bitrix\Main; use Bitrix\Main\Localization\Loc; use Olegpro\BitrixSphinx\Entity\SphinxDataManager; use Olegpro\BitrixSphinx\Entity\SphinxQuery; Loc::loadMessages(__FILE__); class SampleTable extends SphinxDataManager { /** * Returns index sphinx name for entity. * * @return string */ public static function getTableName() { return 'sample_index'; } /** * Returns sphinx-connection name for entity * * @return string */ public static function getConnectionName() { return 'sphinx'; } /** * Creates and returns the Query object for the entity * * @return SphinxQuery */ public static function query() { return new SphinxQuery(static::getEntity()); } /** * Returns entity map definition. * * @return array */ public static function getMap() { return [ new Main\Entity\IntegerField('id', [ 'primary' => true, ]), new Main\Entity\StringField('name'), new Main\Entity\BooleanField('available', [ 'values' => [0, 1], ]) ]; } }
在文件 /bitrix/.settings.php 的 connections 部分描述新的连接
'connections' => array( 'value' => array( 'default' => array( // ... ) ), 'sphinx' => array( 'className' => '\\Olegpro\\BitrixSphinx\\DB\\SphinxConnection', 'host' => '127.0.0.1:9306', 'database' => '', 'login' => '', 'password' => '', 'options' => 1, ), ), 'readonly' => true, ),
使用
<?php use Bitrix\Main\Application; use Bitrix\Main\Entity\ExpressionFieldd; require($_SERVER['DOCUMENT_ROOT'] . '/bitrix/header.php'); Application::getConnection(SampleTable::getConnectionName())->startTracker(true); $iterator = SampleTable::getList([ 'select' => [ '*', new ExpressionField('weight', 'WEIGHT()', 'id'), ], 'match' => 'книга', 'filter' => [ '=available' => 1, ], 'limit' => 10, 'order' => [ 'weight' => 'DESC', ], 'option' => [ 'max_matches' => 50000, ], ]); echo '<pre>';print_r($iterator->getTrackerQuery()->getSql());echo '</pre>'; echo '<pre>';print_r($iterator->fetchAll());echo '</pre>';
分页导航使用
<?php use Bitrix\Main\Application; use Bitrix\Main\Entity\ExpressionFieldd; use Bitrix\Main\UI\PageNavigation; require($_SERVER['DOCUMENT_ROOT'] . '/bitrix/header.php'); Application::getConnection(SampleTable::getConnectionName())->startTracker(true); $nav = new PageNavigation('s'); $nav->allowAllRecords(false) ->setPageSize(10) ->initFromUri(); $iterator = SampleTable::getList([ 'select' => [ '*', new ExpressionField('weight', 'WEIGHT()', 'id'), ], 'match' => 'книга', 'filter' => [ '=available' => 1, ], 'count_total' => true, 'offset' => $nav->getOffset(), 'limit' => $nav->getLimit(), 'order' => [ 'weight' => 'DESC', ], 'option' => [ 'max_matches' => 50000, ], ]); $nav->setRecordCount($iterator->getCount()); echo '<pre>';print_r($iterator->getTrackerQuery()->getSql());echo '</pre>'; echo '<pre>';print_r($iterator->fetchAll());echo '</pre>'; $APPLICATION->IncludeComponent( "bitrix:main.pagenavigation", "", array( "NAV_OBJECT" => $nav, "SEF_MODE" => "N", ), false );
仅使用 master 连接
可以全局安装,添加到 /bitrix/.settings.php 中
'olegpro_bitrix_sphinx' => array ( 'value' => array ( 'use_connection_master_only' => true, ), 'readonly' => false, ),
可以具体针对每个请求传递 use_connection_master_only = true
$iterator = SampleTable::getList([ 'select' => [ '*', new ExpressionField('weight', 'WEIGHT()', 'id'), ], 'match' => 'книга', 'filter' => [ '=available' => 1, ], 'limit' => 10, 'order' => [ 'weight' => 'DESC', ], 'option' => [ 'max_matches' => 50000, ], 'use_connection_master_only' => true, ]);
安装包
将库添加到 Composer
composer require olegpro/bitrix-sphinx