mochaka/sphinxql

此软件包已被废弃,不再维护。没有建议的替代软件包。

集成 SphinxQL 与 Laravel 5。简化 Laravel-Sphinx(RT) 搜索

v1.0.1 2015-06-27 08:39 UTC

This package is not auto-updated.

Last update: 2018-12-18 18:27:49 UTC


README

这是一个简单的库,可以帮助您使用 SphinxQL 查询 Sphinx 搜索服务器。
我创建此软件包的主要动机是方便与 Laravel 4 中的 Sphinx 实时索引进行接口(在 Laravel 4 中,更新实时索引只能使用 SphinxQL)

作为额外的好处,SphinxQL 的性能比 SphinxAPI 更好:http://sphinxsearch.com/blog/2010/04/25/sphinxapi-vs-sphinxql-benchmark/

安装

mochaka/sphinxql 添加到 composer.json

    "mochaka/sphinxql": "1.0"

运行 composer update 以下载 Sphinxql。注意,Sphinxql 依赖于 'FoolCode/SphinxQL-Query-Builder',它执行了大部分工作(http://foolcode.github.io/SphinxQL-Query-Builder/

现在打开 app/config/app.php 并将服务提供者添加到您的 providers 数组中。

    'providers' => array(
        'Mochaka\Sphinxql\SphinxqlServiceProvider',
    )

以及别名

    'aliases' => array(
        'SphinxQL'         => 'Mochaka\Sphinxql\Facades\SphinxqlFacade',
    )

如果您需要覆盖默认配置选项(服务器/端口),请使用配置发布命令

php artisan config:publish Mochaka/sphinxql

Sphinx 中的 RT(实时)索引

Sphinx 中传统索引与 RT 索引之间的主要区别是

  1. RT 使用“推送”模式。这意味着将索引与数据库保持同步的任务委托给您的应用程序(记住,在 RT 方案中没有“索引器”)。因此,通常在 RT 索引的情况下,索引最初为空,随着时间的推移通过应用程序发送的 SphinxQL 查询逐渐填充。

  2. RT 比传统索引器方法使用更多的 RAM。以下是一篇有教育意义的博客,详细说明了原因:http://www.ivinco.com/blog/sphinx-in-action-good-and-bad-in-sphinx-real-time-indexes/ 此外,有关更多信息,请务必阅读 RT 的内部结构: http://sphinxsearch.com/docs/archives/1.10/rt-internals.html 在任何类型的索引策略中(传统或 RT),为了获得最佳效果,建议您尽可能为您的 Sphinx 服务器分配 RAM。因此,额外的 RAM 要求不应真正阻止您使用 RT。

  3. RT 索引的一个主要优点是它们更容易设置和管理。不需要与“索引器”组件纠缠(更少的移动部件)。无需担心 main-delta 方案(通常如此)。当然,您的索引即时具有搜索数据!

Sphinx 的较新版本(2.1.1+)在使 RT 索引可用于生产方面取得了巨大进步:http://sphinxsearch.com/blog/2013/01/15/realtime-index-improvements-in-2-1-1/

当前版本使用合理的配置默认值。因此,您可以拥有一个干净的同义词.sphinx.conf 文件,该文件可以处理大多数常见情况。 http://sphinxsearch.com/docs/current.html#sphinx-deprecations-defaults

以下是以下示例中使用的(最小)sphinx.conf 文件(用于 RT 索引)

index rt_test
{
    type = rt   
    path = /var/lib/sphinxsearch/data/rt
    rt_field = title
    rt_field = content
    rt_attr_uint = gid
}
searchd
{
    # Configure the searchd listening port.
    listen = 9306:mysql41
    binlog_path = /var/lib/sphinxsearch/data
    pid_file = /var/www/sphinx-rt/app/storage/sphinx/searchd.pid

    # sudo searchd -c sphinx.conf - to start search daemon listening on above port   
    # mysql -P 9306 -h 127.0.0.1 - connect to sphinx server daemon
}        

查询构建器文档

由foolcode的好心人们开发和提供的PHP SphinxQL查询构建器包文档齐全、测试完善。我强烈建议您查看他们的网页:http://foolcode.github.io/SphinxQL-Query-Builder/

杂项使用技巧

使用Laravel 4模型事件(http://four.laravel.com/docs/eloquent#model-events),确保您的模型与索引保持同步非常简单!

以下是一些可以轻松添加到模型的“created”、“updated”和“deleted”事件中的片段:

Blog::created(function($model){
	$qins = SphinxQL::query()->insert()->into('rt_test');
	$qins->set(array('id'=>99, 'title'=>'My Title', 'content'=>'My Content', 'gid'=>444))->execute();
	//more realistically, it will look something like
	//$qins->set($model->toArray())->execute();
});

同样,替换和删除可以如此轻松地处理:

Blog::updated(function($model){
	$qrepl = SphinxQL::query()->replace()->into('rt_test');
	$qrepl->set(array('id'=>99, 'title'=>'My Title', 'content'=>'My Content', 'gid'=>444))->execute();
});
Blog::deleted(function($model){
	SphinxQL::query()->delete()->from('rt_test')->where('id',$model->id)->execute();
});

搜索查询可以构建如下:

$q = SphinxQL::query()->select()->from('rt_test')->match('content', 'test');
	       ->execute();	       

上述语句返回一个包含命中项的数组(如果找到)。

查看生成的SQL语句

dd($q->compile()->getCompiled());

有关所有可用选项,请参阅文档(http://foolcode.github.io/SphinxQL-Query-Builder/)。

获取元信息

SphinxQL::query()->meta();

还可以像这样对服务器执行原始SQL查询:

$q = SphinxQL::raw('select * from rt_test');

您可以将任何有效的SphinxQL语句作为参数传递给raw()函数。

与Eloquent集成

此包使将搜索结果与数据库行集成变得非常简单。请记住,sphinx查询仅返回包含ID的命中项数组。应用程序必须发出针对数据库的查询以检索实际的表行。

$q = SphinxQL::query()->select()
			->from('rt_test')
			->match('content', 'test')
	       	->execute();
	       	
dd(Sphinx::with($q)->get('Blog'));	       	

第一条语句在sphinx服务器上运行查询并返回一个数组。

"with()"函数将Sphinx搜索引擎返回的"hit"数组链接到"get"。 "get()"函数具有以下签名:

public function get($name=null, $key='id')

其中 $name 是以下之一:

  1. null - 函数仅返回一个ID数组

  2. eloquent模型 - 函数返回一个包含匹配ID的表行的EloquentCollection

  3. 表示表名的字符串 - 函数返回指定表(使用DB::table('name'))的行数组

可以使用的 $key 参数来更改主键列名称(默认为'id')

许可

本软件根据MIT许可协议授权