fullpipe/sphinxsearch-bundle

Sphinx 搜索包用于 Symfony 2

安装: 76

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 2

类型:symfony-bundle

v1.0 2013-10-20 12:19 UTC

This package is auto-updated.

Last update: 2024-08-27 00:40:29 UTC


README

此包是基于 verdet23/SphinxSearchBundle 的分支,而 verdet23/SphinxSearchBundle 又是基于 timewasted/Search-SphinxsearchBundle

安装

  1. 使用 composer 下载此包
  2. 配置此包

步骤 1: 下载包

在您的 composer.json 中添加 SphinxSearchBundle

{
    "require": {
        "fullpipe/sphinxsearch-bundle": "*"
    }
}

包需要通过 "neutron/sphinxsearch-api" 依赖 SphinxApi,指定版本时根据您的 sphinxsearch 系统包版本。

步骤 2: 配置包

sphinx_search:
    indexes:
        Categories:
            index: %sphinxsearch_index_categories%
            entity: AcmeDemoBundle:Category
        Items:
            index: %sphinxsearch_index_items%
            entity: AcmeDemoBundle:Item
    searchd:
        host:   %sphinxsearch_host%
        port:   %sphinxsearch_port%
        socket: %sphinxsearch_socket%
    indexer:
        sudo:   %sphinxsearch_indexer_sudo%
        bin:    %sphinxsearch_indexer_bin%
        config: %sphinxsearch_indexer_config%

至少必须定义一个索引,您可以定义任意多个。

在上面的示例配置中,Categories 被用作名为 %sphinxsearch_index_categories%(在您的 sphinxsearch.conf 中定义)的索引的标签。这允许您在代码中避免硬编码原始索引名称。

使用示例

最基本的搜索,以上述配置为例,将是

$indexesToSearch = array('Items');
$sphinxSearch = $this->get('search.sphinxsearch.search');
$searchResults = $sphinxSearch->search('search query', $indexesToSearch);

结果

    array(10) {
        ["error"]=> string(0) ""
        ["warning"]=> string(0) ""
        ["status"]=> int(0)
        ...

        ["matches"]=> array(28) {
            [123]=> array(2) {
                ...
                ["weight"]=> string(1) "9"

                ["entity"]=> object(Acme\DemoBundle\Entity\Item) //here is my Item

                ["attrs"]=> array(1) {
                    ["name"]=> string(33) "Лаврова Екатерина"
                }
                ...
            }
        }

        ...

这将对标签为 Items 的索引执行 search query 搜索。搜索结果存储在 $searchResults 中。

您还可以执行更复杂的搜索,例如

$indexesToSearch = array('Items');
$options = array(
  'result_offset' => 0,
  'result_limit' => 25,
  'field_weights' => array(
    'Name' => 2,
    'SKU' => 3,
  ),
);
$sphinxSearch = $this->get('search.sphinxsearch.search');
$sphinxSearch->setMatchMode(SPH_MATCH_EXTENDED2);
$sphinxSearch->setFilter('disabled', array(1), true);
$searchResults = $sphinxSearch->search('search query', $indexesToSearch, $options);

这将再次搜索 Items 中的 search query,但现在它只返回前 25 个匹配项,并将 NameSKU 字段的重要性高于正常。请注意,为了定义 result_offsetresult_limit,您必须明确定义这两个值。此外,此搜索将使用 扩展查询语法,并排除所有将 disabled 属性设置为 1 的结果。

如果您想使用 Pagerfanta

use use Pagerfanta\Pagerfanta;

...

$adapter = $this->get('search.sphinxsearch.pagerfanta.adapter');

$indexesToSearch = array('Items');
$options = array(
  'field_weights' => array(
    'Name' => 2,
    'SKU' => 3,
  ),
);

$adapter->setSearchParams('search query', $indexesToSearch, $options);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(28);
$pagerfanta->setCurrentPage($request->get('page', 1));

$pagerfanta 将包含匹配数组

许可证

Copyright (c) 2012, Eugene Bravov
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.