javer/sphinx-bundle
Composer 安装命令:
composer require javer/sphinx-bundle
包简介
Provides integration of Sphinx search engine with Symfony using SphinxQL
README 文档
README
This bundle provides integration of Sphinx search engine with Symfony.
Features:
- SphinxQL Query Builder
- Integration with doctrine/orm
- Integration with knplabs/knp-paginator-bundle
- Symfony Profiler toolbar section with number of executed queries and profiler page with detailed information about executed queries
- Ability to test search using Behat scenarios
Requirements
- PHP 8.0+
- pdo_mysql php extension
Installation
Install the bundle using composer:
composer require javer/sphinx-bundle
Configuration
Add to your app/config/config.yml the following options:
javer_sphinx: host: 127.0.0.1 port: 9306
Full configuration with default values:
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
Usage
Synthetic example of SELECT query which returns an array:
$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();
Paginate a list of entities fetched from the database using Doctrine ORM QueryBuilder by searching phrase in them using Sphinx:
$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);
Sample shpinx.conf for the given example above:
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 tests
To be able to test search in Behat scenarios there is built-in behat context SphinxContext.
Installation with behat/symfony2-extension
To use it you should add this context in your behat.yml, for example:
selenium: extensions: Behat\Symfony2Extension: ~ suites: frontend: contexts: - Javer\SphinxBundle\Behat\Context\SphinxContext
Please note that Symfony2Extension Behat extension is required to be able to use this feature.
Installation with friends-of-behat/symfony-extension
Register SphinxDIContext in the container:
#config/services_test.yaml services: Javer\SphinxBundle\Behat\Context\SphinxDIContext: arguments: $container: '@test.service_container'
and then use it in your behat.yml:
default: suites: default: contexts: - Javer\SphinxBundle\Behat\Context\SphinxDIContext
Usage
Then you should add a new step to your scenario:
Given I create search index and run sphinx
This step:
- creates a new configuration for sphinx based on your configuration
- converts all MySQL indexes to real-time indexes
- starts daemon
- loads data from the database to indexes for converted MySQL -> real-time indexes
- stops daemon at the end of the scenario
Please note that you should explicitly declare all text fields in your indexes in the following form:
source product
{
#!sql_field_string = name
}
It is not necessary when you declare fields for MySQL index in sphinx.conf, but it is needed to be able to convert indexes to real-time.
If you use sqlite as the database engine for running tests you should take into account that not all functions of the MySQL are presented in sqlite, so you should use portable analogs for these functions:
IF(condition, true, false)->CASE WHEN condition THEN true ELSE false END- and so on
Docker
You can use a docker image to run the daemon in the docker container for the test environment, just add the following configuration option to config/packages/test/javer_sphinx.yaml:
javer_sphinx: docker_image: javer/sphinx
javer/sphinx-bundle 适用场景与选型建议
javer/sphinx-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 191.53k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2017 年 05 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「Symfony2」 「search」 「doctrine2」 「sphinx」 「sphinxql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 javer/sphinx-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 javer/sphinx-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 javer/sphinx-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
The bundle for easy using json-rpc api on your project
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
Bundle Symfony DaplosBundle
Symfony2 Barcode Generator Bundle with Twig function extension
统计信息
- 总下载量: 191.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-21