jaVer / sphinx-bundle
提供使用 SphinxQL 集成 Sphinx 搜索引擎与 Symfony
v1.6.4
2023-03-30 15:32 UTC
Requires
- php: >=8.0
- ext-pcntl: *
- ext-pdo: *
- ext-posix: *
Requires (Dev)
- behat/behat: ^3.7
- doctrine/dbal: ^2.12 || ^3.0
- doctrine/orm: ^2.8
- knplabs/knp-components: ^3.0 || ^4.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.3
- psr/log: ^1.0 || ^2.0 || ^3.0
- squizlabs/php_codesniffer: 3.7.*
- swivl/php-coding-standard: ^1.4
- symfony/config: ^4.4 || ^5.0 || ^6.0
- symfony/dependency-injection: ^4.4 || ^5.0 || ^6.0
- symfony/event-dispatcher: ^4.4 || ^5.0 || ^6.0
- symfony/http-foundation: ^4.4 || ^5.0 || ^6.0
- symfony/http-kernel: ^4.4 || ^5.0 || ^6.0
Suggests
- behat/behat: to be able to test search
- behat/symfony2-extension: to be able to test search
- doctrine/orm: to allow usage with Doctrine ORM
- friends-of-behat/symfony-extension: to be able to test search
- knplabs/knp-paginator-bundle: to allow usage with Knp Paginator
README
此包提供 Sphinx 搜索引擎与 Symfony 的集成。
特性
- SphinxQL 查询构建器
- 与 doctrine/orm 集成
- 与 knplabs/knp-paginator-bundle 集成
- 包含执行查询次数的 Symfony 分析器工具栏部分和包含详细执行查询信息的分析器页面
- 使用 Behat 场景测试搜索的能力
要求
- PHP 8.0+
- pdo_mysql php 扩展
安装
使用 composer 安装此包
composer require javer/sphinx-bundle
配置
将以下选项添加到您的 app/config/config.yml
javer_sphinx: host: 127.0.0.1 port: 9306
完整配置(默认值)
javer_sphinx: host: 127.0.0.1 port: 9306 config_path: "%kernel.project_dir%/config/sphinx.conf" data_dir: "%kernel.cache_dir%/sphinx" searchd_path: searchd
使用方法
SELECT 查询的合成示例,返回数组
$results = $this->container->get('sphinx') ->select('id', 'column1', 'column2', 'WEIGHT() as weight') ->from('index1', 'index2') ->where('column3', 'value1') ->where('column4', '>', 4) ->where('column5', [5, '6']) ->where('column6', 'NOT IN', [7, '8']) ->where('column7', 'BETWEEN', [9, 10]) ->match('column8', 'value2') ->match(['column9', 'column10'], 'value3') ->groupBy('column11') ->groupBy('column12') ->withinGroupOrderBy('column13', 'desc') ->withinGroupOrderBy('column14') ->having('weight', '>', 2) ->orderBy('column15', 'desc') ->orderBy('column16') ->offset(5) ->limit(10) ->option('agent_query_timeout', 10000) ->option('max_matches', 1000) ->option('field_weights', '(column9=10, column10=3)') ->getResults();
使用 Sphinx 在 Doctrine ORM QueryBuilder 中搜索短语来分页从数据库获取的实体列表
$queryBuilder = $this->container->get('doctrine.orm.default_entity_manager') ->createQueryBuilder() ->select('p', 'i') ->from('AppBundle:Product', 'p') ->join('AppBundle:Image', 'i') ->where('p.owner = :owner') ->setParameter('owner', $this->getUser()); $query = $this->container->get('sphinx') ->createQuery() ->select('*') ->from('product') ->match(['name', 'description'], $searchQuery) ->where('owner_id', $this->getUser()->getId()) ->orderBy('created', 'desc') ->useQueryBuilder($queryBuilder, 'p'); $paginator = $this->container->get('knp_paginator') ->paginate($query, $request->query->get('page', 1), 20);
上述示例的样本 shpinx.conf
source product
{
type = mysql
sql_host = localhost
sql_user = user
sql_pass = password
sql_db = database_name
sql_query = \
SELECT p.id, p.owner_id, p.name, p.description, UNIX_TIMESTAMP(p.created) as created, \
FROM product p \
WHERE p.deletedAt IS NULL
sql_attr_uint = owner_id
sql_attr_timestamp = created
}
index product
{
source = product
path = /usr/local/var/data/product
morphology = stem_en
min_stemming_len = 3
}
Behat 测试
为了能够在 Behat 场景中测试搜索,存在内置的 behat context SphinxContext。
使用 behat/symfony2-extension 安装
要使用它,您应该在 behat.yml 中添加此上下文,例如
selenium: extensions: Behat\Symfony2Extension: ~ suites: frontend: contexts: - Javer\SphinxBundle\Behat\Context\SphinxContext
请注意,需要 Symfony2Extension Behat 扩展才能使用此功能。
使用 friends-of-behat/symfony-extension 安装
在容器中注册 SphinxDIContext
#config/services_test.yaml services: Javer\SphinxBundle\Behat\Context\SphinxDIContext: arguments: $container: '@test.service_container'
然后在 behat.yml 中使用它
default: suites: default: contexts: - Javer\SphinxBundle\Behat\Context\SphinxDIContext
使用方法
然后您应该在场景中添加一个新步骤
Given I create search index and run sphinx
此步骤
- 根据您的配置为 sphinx 创建新的配置
- 将所有 MySQL 索引转换为实时索引
- 启动守护进程
- 将数据库中的数据加载到转换为 MySQL -> 实时索引的索引中
- 在场景结束时停止守护进程
请注意,您应该明确声明以下形式的索引中所有文本字段
source product
{
#!sql_field_string = name
}
当您在 sphinx.conf 中声明 MySQL 索引的字段时,这不必要,但这是将索引转换为实时所需。
如果您将 sqlite 作为运行测试的数据库引擎,请注意,sqlite 中不提供 MySQL 的所有功能,因此您应该使用这些功能的可移植类似物
IF(condition, true, false)
->CASE WHEN condition THEN true ELSE false END
- 等等
Docker
您可以使用 docker 镜像在测试环境中在 docker 容器中运行守护进程,只需将以下配置选项添加到 config/packages/test/javer_sphinx.yaml
javer_sphinx: docker_image: javer/sphinx