mfjordvald/sphinxql

将 SphinxQL 集成到 Laravel 8 中。简化 Laravel-Sphinx(RT) 搜索

2.2.0 2024-07-06 02:52 UTC

This package is auto-updated.

Last update: 2024-09-06 03:13:56 UTC


README

这是一个简单的库,可以帮助您使用 SphinxQL 查询 Sphinx 搜索服务器。创建此包的主要动机是方便地在 Laravel 8 中与 Sphinx 实时索引进行接口交互(更新 rt 索引仅通过 SphinxQL 可行)

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

此包应与通过 foolz/sphinxql-query-builder 库进行 Manticore Search 兼容。

安装

使用 composer 安装此包。

    $ composer require mfjordvald/sphinxql

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

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

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

以及别名

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

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

    php artisan config:publish mfjordvald/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 索引的一个主要优点是它们设置和管理起来更容易。无需处理 cron 作业,因为 "索引器" 组件不被使用(组件更少)。无需担心主-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 6模型事件(http://four.laravel.com/docs/eloquent#model-events),确保您的模型与索引保持同步非常简单!

以下是一些可以轻松添加到模型“创建”、“更新”和“删除”事件的代码片段:

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(SphinxQL::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许可协议授权

致谢

这是一个基于https://github.com/mnshankar/SphinxQL的分支,mnshankar最初做了这项工作,但现在似乎转向了其他项目。