maninhat/search-sphinxsearchbundle

Sphinx 搜索包用于 Symfony 2。

此软件包的官方仓库似乎已消失,因此该软件包已被冻结。

安装: 310

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 3

分支: 33

开放问题: 1

类型:symfony-bundle

dev-master 2012-09-13 16:12 UTC

This package is not auto-updated.

Last update: 2023-03-18 05:42:13 UTC


README

安装

  1. 下载软件包
  2. 配置自动加载器
  3. 启用软件包
  4. 配置软件包

步骤 1:下载软件包

您实际上如何下载软件包完全取决于您。但是,一旦下载,您应将其放置在 src/Search/SphinxsearchBundle 目录中。

步骤 2:配置自动加载器

<?php
// app/autoload.php

$loader->registerNamespaces(array(
        // ...
        'Search' => __DIR__ . '/../src',
));

步骤 3:启用软件包

<?php
// app/AppKernel.php

public function registerBundles()
{
        $bundles = array(
                // ...
                new Search\SphinxsearchBundle\SphinxsearchBundle(),
        );
}

步骤 4:配置软件包

sphinxsearch:
    indexes:
        - name: allSite                      #Index label
          index:                             #list of indexes
              - %sphinxsearch_index_pages%   #index name
              - %sphinxsearch_index_books%
          field_weights:
              label: 10
              content: 5
        - name: books
          index:
              - %sphinxsearch_index_books%
          field_weights:
              label: 10
              content: 5
    searchd:
        host:   %sphinxsearch_host%
        port:   %sphinxsearch_port%
        socket: %sphinxsearch_socket%
    indexer:
        bin:    %sphinxsearch_indexer_bin%

    mapping:
        Book:
           repository: "GIDiaBundle:Book"    #Doctrine repository name
           parameter: "model_name"           #returned by sphinx. By this parameter Bundle will choose repository
           value: 1                          #uniq value for parameter
        Page:
           repository: "GIDiaBundle:Page"
           parameter: "model_name"
           value: 2

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

在上面的示例配置中,allSite 用作索引 %sphinxsearch_index_pages%%sphinxsearch_index_books%(在您的 sphinx.conf 中定义)的标签。这允许您避免在代码中硬编码原始索引名称。您还可以可选地定义搜索时应用的字段权重。

示例

我们的基本搜索很简单

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

这将对标记为 Items 和 Categories 的索引执行搜索查询,并使用在您的 sphinx.conf 中定义的任何默认值(在这里以及在其中)。

如果您要进行更多基本搜索之外的操作,它看起来可能如下所示

$indexesToSearch = array(
  'Items' => array(
    'result_offset' => 0,
    'result_limit' => 25,
  ),
  'Categories' => array(
    'result_offset' => 0,
    'result_limit' => 10,
  ),
);
$sphinxSearch = $this->get('search.sphinxsearch.search');
$sphinxSearch->setMatchMode(SPH_MATCH_EXTENDED);
$sphinxSearch->setFilter('disabled', array(1), true);
$searchResults = $sphinxSearch->search('search query', $indexesToSearch);

这将对 Items 和 Categories 再次进行搜索查询,但现在 Items 将返回前 25 个匹配项,而 Categories 将返回前 10 个。为了定义结果偏移量或结果限制,您必须明确定义这两个值。此外,这将使用扩展匹配模式,并排除所有设置为 1 的禁用属性的结果。

许可

Copyright (c) 2012, Ryan Rogers
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.