ttf/sphinx

0.1.3 2013-04-28 17:41 UTC

This package is not auto-updated.

Last update: 2024-09-23 11:12:16 UTC


README

这是一个简单的服务提供商,用于将 SphinxSearch-API 与 silex 相结合。

注意事项

目前此项目仍在开发中,不适合在生产环境中使用。

API 可能会频繁且不可预测地更改。通常,我建议在 1.0.0 版本之前不要使用它。

如果您真的想在生产环境中使用此产品,我建议选择一个版本并坚持使用它,例如在您的 composer.json 中设置 "require 'ttf/sphinx': '0.1.2'"。

用法

像其他服务提供商一样注册服务提供商

$app->register( new Ttf\SphinxServiceProvider());

如果您在除默认的 "localhost:3312" 之外的主机/端口组合上运行 sphinx searchd,您需要在此提供这些参数

$app->register( new Ttf\SphinxServiceProvider(), array( 
    'sphinx.port' => 3413,
    'sphinx.host' => 'sphinx.example.org'
));

然后,您可以使用它作为任何其他服务提供商一样使用。首先,使用提供者的搜索方法,这将返回一个结果对象。

$app->get('/search', function( Request $request ) use ($app) {
    $result = $app['sphinx']->search( $request->get('q'), 'myCatalog' );
    return new Response( (String) $result, 200 );
});

将 $result 对象强制转换为字符串将返回您研究结果的易读表示。

在这种情况下,调用请求可能如下所示

http://silex.local/search?q=cheese

一个更复杂的例子将是

$app->get('/search', function( Request $request ) use ($app) {
    if( $result = $app['sphinx']->search( $request->get('q'), 'catalog' ) ) {
        $indices = $result->fetchIndices();
        
        $query = 'Select description, tags from stand where fiorg = ?';
        foreach( $indices as $index ) {
            $entries = $app['db']->fetchAssoc( $query, array( $index ));
        }
        $app['twig']->render('search.twig',
            array(
                'results' => $entries
            )
        );
    }
});

向 Sphinx API 传递参数

请注意,您也可以直接将参数传递给 sphinxsearch api。只需在服务上使用它们即可。

$app['sphinx']->SetRankingMode( SPH_RANK_SPH04 );
$app['sphinx']->SetFieldWeights( array( 'tags' => 10, 'description' => 5) );