evilfreelancer/laravel-manticoresearch

在您的Laravel应用程序中轻松使用官方ManticoreSearch客户端

1.6.2 2021-11-07 19:29 UTC

This package is auto-updated.

Last update: 2024-09-09 08:05:47 UTC


README

Latest Stable Version Total Downloads Build Status Code Coverage Code Climate Scrutinizer Code Quality License

Laravel ManticoreSearch插件

在您的Laravel或Lumen应用程序中使用官方ManticoreSearch客户端的最简单方法。

composer require evilfreelancer/laravel-manticoresearch

安装后

Laravel

包的服务提供者将自动注册其服务提供者。

发布配置文件

php artisan vendor:publish --provider="ManticoreSearch\Laravel\ServiceProvider"

通过.env文件进行替代配置方法

按照上述建议发布配置文件后,您可以通过将以下内容添加到应用程序的.env文件(使用适当的值)来配置ManticoreSearch:

MANTICORESEARCH_HOST=localhost
MANTICORESEARCH_PORT=9200
MANTICORESEARCH_TRANSPORT=Http
MANTICORESEARCH_USER=
MANTICORESEARCH_PASS=

所有可用的环境变量

Lumen

如果您使用Lumen,请在bootstrap/app.php中注册服务提供者和配置

// Enable shortname of facade
$app->withFacades(true, [
    'ManticoreSearch\Laravel\Facade' => 'Facade',
]);

// Register Config Files
$app->configure('manticoresearch');

// Register Service Providers
$app->register(ManticoreSearch\Laravel\ServiceProvider::class);

手动将配置文件复制到您的应用程序中。

如何使用

ManticoreSearch门面只是ManticoreSearch客户端的入口点,因此您之前可能使用过

require_once __DIR__ . '/vendor/autoload.php';

$config = ['host'=>'127.0.0.1', 'port'=>9308];
$client = new \Manticoresearch\Client($config);
$index  = new \Manticoresearch\Index($client);
$index->setName('movies'); 

而不是上面这些几行,您可以使用单行解决方案

$index = \ManticoreSearch::index('movies');

这将运行默认连接上的命令。您可以在任何连接上运行命令(参见配置文件中的defaultConnection设置和connections数组)。

$index   = \ManticoreSearch::connection('connectionName')->index($nameOfIndex);
$pq      = \ManticoreSearch::connection('connectionName')->pq();
$cluster = \ManticoreSearch::connection('connectionName')->cluster();
$indices = \ManticoreSearch::connection('connectionName')->indices();
$nodes   = \ManticoreSearch::connection('connectionName')->nodes();

// etc...

客户端类的方法

\ManticoreSearch::connection('connectionName')->sql($params);
\ManticoreSearch::connection('connectionName')->replace($params);
\ManticoreSearch::connection('connectionName')->delete($params);

// etc...

不使用门面的Lumen用户需要使用依赖注入或应用程序容器来获取ManticoreSearch Index对象

// using injection:
public function handle(\ManticoreSearch\Laravel\Manager $manticoresearch)
{
    $manticoresearch->describe();
}

// using application container:
$manticoreSearch = $this->app('manticoresearch');

当然,依赖注入和应用程序容器也适用于Laravel应用程序。

日志记录

由于ManticoreSearch的PHP客户端支持通过PSR兼容的日志记录器进行日志记录,您可以使用它们的方式与官方文档中展示的方式相同。

例如,您想使用Monolog记录器。

composer require monolog/monolog

默认情况下,您需要编写如下内容

$logger = new \Monolog\Logger('name');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('/my/log.file', Logger::INFO));
$config = ['host' => '127.0.0.1', 'port' => 9306];
$client = new \Manticoresearch\Client($config, $logger);
$index  = new \Manticoresearch\Index($client);
$index->setName('movies');

但是,如果您想同时使用Monolog和此库,则可以简化代码如下

$logger = new \Monolog\Logger('name');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('/my/log.file', Logger::INFO));

$index = \ManticoreSearch::connection('connectionName', $logger)->index('movies');

测试

只需安装开发需求composer install --dev,然后从该库的根目录执行以下命令

./vendor/bin/phpunit

链接