rosh1ck / sphinxsearch-bundle

Sphinx 2.0 搜索包

安装次数: 8,614

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 0

分支: 26

公开问题: 0

类型:symfony-bundle

0.2.5 2020-09-08 14:34 UTC

This package is not auto-updated.

Last update: 2024-09-27 20:10:52 UTC


README

此包是针对 Symfony3 的分支和更新。

原始页面: https://github.com/IAkumaI/SphinxsearchBundle

安装

步骤 1: 使用 composer 下载 SphinxsearchBundle

在你的 composer.json 文件中添加 SphinxsearchBundle

{
    "require": {
        "roshc1k/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>

在这个例子中,将使用 IndexName 索引高亮显示 content 变量中匹配 "query word" 的内容。它使用 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"

要将多个查询转换为实体,请将 index_name 属性添加到你的 sphinx.conf 文件中,例如

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
...