iakumai/sphinxsearch-bundle
Composer 安装命令:
composer require iakumai/sphinxsearch-bundle
包简介
Sphinx search bundle for Symfony 2
README 文档
README
With this bundle you can use Sphinx to search in your Symfony2 project.
Installation
Step 1: Download SphinxsearchBundle using composer
In your composer.json, add SphinxsearchBundle:
{ "require": { "iakumai/sphinxsearch-bundle": "dev-master" } }
Now, you must update your vendors using this command :
$ php composer.phar update iakumai/sphinxsearch-bundle
Step 2: Add bundle in AppKernel.php
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new IAkumaI\SphinxsearchBundle\SphinxsearchBundle() ); }
Step 3: Configure your config.yml
By default bundle does not need to be a configured, but has some options for you.
Full configuration :
# app/config/config.yml sphinxsearch: searchd: # Host name for your Sphinx daemon host: localhost # Port number for your Sphinx daemon port: 9312 # If you want to connect via scoket socket: /path/to/socket.file indexes: # List of sphinx index names (key) and entity names (value) # to use it in searchEx() method IndexName: "Bundle:Entity"
Services
- @iakumai.sphinxsearch.search - base search engine to use Sphinx search.
Maybe you want to use another class in @iakumai.sphinxsearch.search service. To do this put a full class name to the parameter named %iakumai.sphinxsearch.search.class%.
- @iakumai.sphinxsearch.doctrine.bridge - bridge to dictrine datebase.
Maybe you want to use another class in @iakumai.sphinxsearch.doctrine.bridge service. To do this put a full class name to the parameter named %iakumai.sphinxsearch.doctrine.bridge.class%. It must implements a IAkumaI\SphinxsearchBundle\Doctrine\BridgeInterface
Exceptions
- EmptyIndexException - you will see this exception if try to search without indexes.
- NoSphinxAPIException - this exception throws if not SphinxAPI was found.
Highlight search results
You can highlight search words in templates by use sphinx_highlight filter.
For example:
<div class="text-block"> {{ content|sphinx_highlight('IndexName', 'query word', {limit:100}) }} </div>
In this example matches for "query word" in content variable will be highlighted for IndexName index. It use BuildExcerpts method for this.
Useful features
Sphinx search by date range
For example, search link looks like http://site.ru/search/?date-start=26.09.2013&date-end=27.09.2013
// ... use Symfony\Bundle\FrameworkBundle\Controller\Controller; class SearchController extends Controller { public function indexAction(Request $request) { // Get a search service $sphinx = $this->get('iakumai.sphinxsearch.search'); // Convert request parameters to \DateTime if ($datestart = $request->query->get('date-start')) { $datestart = \DateTime::createFromFormat('d.m.Y', $datestart); } if ($dateend = $request->query->get('date-end')) { $dateend = \DateTime::createFromFormat('d.m.Y', $dateend); } // Apply sphinx filter // updated - is a timestamp-attribute name in sphinx config $sphinx->setFilterBetweenDates('updated', $datestart, $dateend); return $sphinx->search($request->query->get('q', ''), array('IndexName')); } }
Examples
This code will use IndexName index to search for a query in q-get parameter:
// In a controller public function searchAction(Request $request) { $searchd = $this->get('iakumai.sphinxsearch.search'); return $sphinxSearch->search($request->query->get('q', ''), array('IndexName')); }
You can use all methods, that provides by PHP SphinxAPI.
For example:
// In a controller public function searchAction(Request $request) { $searchd = $this->get('iakumai.sphinxsearch.search'); $searchd->setLimits(0, 100); return $sphinxSearch->search($request->query->get('q', ''), array('IndexName')); }
Now bundle can auto convert search results to entities if you will search for one index or define a index_name attribute in sphinx config. To to this, first configure index names, for example:
# app/config/config.yml sphinxsearch: indexes: IndexName: "Bundle:Entity"
To convert multiple queries please add index_name attribute to your sphinx.conf file, for example:
source Example
{
sql_query = SELECT id, ...., 'IndexName' as 'index_name' FROM my_table
sql_attr_string = index_name
}
index IndexName
{
source = Example
path = /your/own/path
}
Now you can execute searchEx() method:
// In a controller public function searchAction(Request $request) { $searchd = $this->get('iakumai.sphinxsearch.search'); $results_one = $sphinxSearch->searchEx($request->query->get('q', ''), 'IndexName'); // or for multiple indexes (index_name attribute must exists) $results_two = $sphinxSearch->searchEx($request->query->get('q', ''), array('IndexName', 'SeconIndexName')); }
$results_one now will contains something like this:
array(10) {
.....
["matches"]=>
array(20) {
[22]=>
array(3) {
["weight"]=>
string(1) "2"
["attrs"]=>
array(0) {
}
["entity"]=> ... // Here is your Bundle:Entity
}
.........
$results_two now will contains something like this:
array(10) {
.....
["matches"]=>
array(20) {
[22]=>
array(3) {
["weight"]=>
string(1) "2"
["attrs"]=>
array(0) {
["index_name"]=>
string(9) "IndexName"
}
["entity"]=> ... // Here is your Bundle:Entity
}
.........
Pagerfanta adapter
This bundle also includes special adapter for excellent Pagerfanta bundle
/** @var $sphinx \IAkumaI\SphinxsearchBundle\Search\Sphinxsearch */ $sphinx = $this->get('iakumai.sphinxsearch.search'); /** @var $sphinxDoctrineBridge \IAkumaI\SphinxsearchBundle\Doctrine\Bridge */ $sphinxDoctrineBridge = $this->get('iakumai.sphinxsearch.doctrine.bridge'); $sphinx->setBridge($sphinxDoctrineBridge); //IMPORTANT! Set doctrine bridge. $query = 'search query'; $entityIndexType = 'Books'; $adapter = new \IAkumaI\SphinxsearchBundle\Pagerfanta\Adapter\SphinxSearchAdapter($sphinx, $query, $entityIndexType, [ 'max_results' => 1000000, ]); $pager = new Pagerfanta($adapter); // Use pagerfanta as always ...
iakumai/sphinxsearch-bundle 适用场景与选型建议
iakumai/sphinxsearch-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 205.63k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2013 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Symfony2」 「sphinx」 「sphinxsearch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 iakumai/sphinxsearch-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iakumai/sphinxsearch-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 iakumai/sphinxsearch-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel query builder for SphinxQL
A Sphinx Query Builder extension for the Laravel Database package
Laravel package to query Sphinxsearch in Laravel 5
Sphinx Search PHP API
ShopExpress SphinxSearchClient API for pdo-crud service
Symfony2 Barcode Generator Bundle with Twig function extension
统计信息
- 总下载量: 205.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 29
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-09-25