hyperf+GuzzleHttp实现Meili Search搜索引擎索引的查增改删操作
上一篇文章是centos7中Meili Search搜索引擎安装,现在来测试一下索引增删改查的基操
环境
hyperf2.1
centos7+Meili Search搜索引擎
路由:
注解方式
控制器:
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\View\RenderInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperf\DbConnection\Db;
use Hyperf\Guzzle\ClientFactory;
use Hyperf\HttpServer\Annotation\AutoController;
/**
* @AutoController()
*/
class TestController
{
/**
* @var \Hyperf\Guzzle\ClientFactory
*/
private $clientFactory;
public function __construct(ClientFactory $clientFactory)
{
$this->clientFactory = $clientFactory;
}
public function index(RenderInterface $render,ResponseInterface $response,RequestInterface $request)
{
//return $response->json('测试专用控制器');
return $response->json([
"errno"=>0,
"data" => '/upload/image/'
]);
}
public function meilisearch(RequestInterface $request,ResponseInterface $response)
{
$client = new \GuzzleHttp\Client();
//查询
// curl -X GET 'http://localhost:7700/indexes'
$resp = $client->request('GET', 'http://127.0.0.1:7700/indexes');
$a = $resp->getBody();
return $a;
//添加
//curl -X POST 'http://localhost:7700/indexes' --data '{"uid": "movies1","primaryKey": "movie_id1"}'
/*$body = [
"uid" => "movies2",
"primaryKey" => "movie_id2"
];
$resp = $client->request('POST', 'http://127.0.0.1:7700/indexes',
[
'headers' => [
'content-type' => 'application/json',
'accept' => 'application/ld+json'
],
'body' => json_encode($body),
]
);
return json_decode((string) $resp->getBody(),true);
*/
//修改
//curl -X PUT 'http://127.0.0.1:7700/indexes/movies2' --data '{"name" : "666"}'
/*$body = [
"name" => "666"
];
$resp = $client->request('PUT', 'http://127.0.0.1:7700/indexes/movies2',
[
'headers' => [
'content-type' => 'application/json',
'accept' => 'application/ld+json'
],
'body' => json_encode($body),
]
);
return json_decode((string) $resp->getBody(),true);
*/
//删除
//curl -X DELETE 'http://127.0.0.1:7700/indexes/movies1'
//$client->delete('http://127.0.0.1:7700/indexes/movies1');
//return 'test-Hyperf-meilisearch ';
}
}
截图比较麻烦,我就截一张有代表性的,Meili Search搜索引擎的操作日志看看:
在来一张
完
有兴趣的可以自行测试
官方文档:
https://docs.meilisearch.com/
相关文章