alkhvalko / sphinxsearch-bundle
Sphinx 搜索包用于 Symfony
Requires
- php: >=7.2
This package is auto-updated.
Last update: 2024-09-16 15:00:44 UTC
README
使用此包,您可以在您的 Symfony2 项目中使用 Sphinx 进行搜索。
安装
步骤 1:使用 composer 下载 SphinxsearchBundle
在您的 composer.json 中添加 SphinxsearchBundle
{ "require": { "iakumai/sphinxsearch-bundle": "dev-master" } }
现在,您必须使用以下命令更新您的 vendors
$ php composer.phar update iakumai/sphinxsearch-bundle
步骤 2:在 AppKernel.php 中添加包
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new IAkumaI\SphinxsearchBundle\SphinxsearchBundle() ); }
步骤 3:配置您的 config.yml
默认情况下,包不需要配置,但提供了一些选项。
完整配置
# 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"
服务
- @iakumai.sphinxsearch.search - 使用 Sphinx 进行搜索的基本搜索引擎。
您可能想使用 @iakumai.sphinxsearch.search 服务中的另一个类。要做到这一点,将完整类名放入名为 %iakumai.sphinxsearch.search.class% 的参数中。
- @iakumai.sphinxsearch.doctrine.bridge - 到 doctrine 数据库的桥梁。
您可能想使用 @iakumai.sphinxsearch.doctrine.bridge 服务中的另一个类。要做到这一点,将完整类名放入名为 %iakumai.sphinxsearch.doctrine.bridge.class% 的参数中。它必须实现 IAkumaI\SphinxsearchBundle\Doctrine\BridgeInterface
接口。
异常
- EmptyIndexException - 如果尝试在不使用索引的情况下搜索,您将看到此异常。
- NoSphinxAPIException - 如果找不到 SphinxAPI,则会抛出此异常。
突出显示搜索结果
您可以使用 sphinx_highlight 过滤器在模板中突出显示搜索词。
例如
<div class="text-block"> {{ content|sphinx_highlight('IndexName', 'query word', {limit:100}) }} </div>
在此示例中,对于 "查询词" 在 content 变量中的匹配将突出显示为 IndexName 索引。它使用 BuildExcerpts 方法进行此操作。
有用功能
按日期范围进行 Sphinx 搜索
例如,搜索链接看起来像 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')); } }
示例
此代码将使用 IndexName 索引在 q-get 参数中搜索查询。
// In a controller public function searchAction(Request $request) { $searchd = $this->get('iakumai.sphinxsearch.search'); return $sphinxSearch->search($request->query->get('q', ''), array('IndexName')); }
您可以使用 PHP SphinxAPI 提供的所有方法。
例如
// 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')); }
现在,如果搜索一个索引或定义 sphinx 配置中的 index_name 属性,则包可以自动将搜索结果转换为实体。为此,首先配置索引名称,例如
# app/config/config.yml sphinxsearch: indexes: IndexName: "Bundle:Entity"
要转换多个查询,请向您的 sphinx.conf 文件中添加 index_name 属性,例如
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
}
现在您可以执行 searchEx() 方法
// 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 现在将包含类似以下内容
array(10) {
.....
["matches"]=>
array(20) {
[22]=>
array(3) {
["weight"]=>
string(1) "2"
["attrs"]=>
array(0) {
}
["entity"]=> ... // Here is your Bundle:Entity
}
.........
$results_two 现在将包含类似以下内容
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 适配器
此包还包括针对出色的 Pagerfanta 包 的专用适配器。
/** @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 ...