hyperf+GuzzleHttp实现Meili Search搜索引擎文件的查增改删操作及数据库取出插入demo

2023-06-01 00:00:00 操作 插入 取出

接上一篇索引的操作,这篇是文件的操作咯


文件

文档是由可以存储任何类型数据的字段组成的对象。

每个字段都包含一个属性及其关联的值。

文档存储在索引中。



环境跟上一篇一样

hyperf2.1

centos7+meilisearch


路由也一样

注解



控制器:

<?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/'
       ]);
   }

   
   /**
    * meilisearch搜索引擎  实现文件增删改查
    */
   public function mlsearch()
   {
       $client = new \GuzzleHttp\Client();

       //单查
       //curl -X GET 'http://localhost:7700/indexes/movies/documents/113965'
       //$resp = $client->request('GET', 'http://127.0.0.1:7700/indexes/movies/documents/113965');

       //批查  后面可带limit参数
       //curl -X GET 'http://localhost:7700/indexes/movies/documents?limit=2'
       $resp = $client->request('GET', 'http://127.0.0.1:7700/indexes/movies/documents');
       return json_decode((string) $resp->getBody(),true);

       //单删
       //curl -X DELETE 'http://localhost:7700/indexes/movies/documents/287947'
       //$resp = $client->delete('http://127.0.0.1:7700/indexes/movies/documents/287947');

       //批删
       //curl -X POST 'http://localhost:7700/indexes/movies/documents/delete-batch' --data '[113965,113729,113727,112134,113728]'
       /*$body = [113965,113729,113727,112134,113728];
       $resp = $client->request('POST', 'http://127.0.0.1:7700/indexes/movies/documents/delete-batch',
           [
               'headers' => ['content-type' => 'application/json;charset=UTF-8','accept' => 'application/ld+json'],
               'body' => json_encode($body),
           ]
       );
       */


       //增
       //curl -X POST 'http://localhost:7700/indexes/movies/documents' --data '[{"id":"3","title":"中国"},{"id":"4","title":"ddd"}]'
       /*$a = '[{"id":"3","title":"中国"},{"id":"4","title":"ddd"}]';
       $resp = $client->request('POST', 'http://127.0.0.1:7700/indexes/movies/documents',
           [
               'headers' => ['content-type' => 'application/json;charset=UTF-8','accept' => 'application/ld+json'],
               'body' => $a,
           ]
       );
       */

       //改
       //curl -X PUT 'http://localhost:7700/indexes/movies/documents' --data '[{"id": 3,"title": "中国广州"}]'
       /*$body = '[{"id": 3,"title": "中国广州"}]';
       $resp = $client->request('PUT', 'http://127.0.0.1:7700/indexes/movies/documents',
           [
               'headers' => ['content-type' => 'application/json', 'accept' => 'application/ld+json'],
               'body' => $body,
           ]
       );
       */


       //从数据库数取出据库               content
       $art = DB::table('art')->select(DB::raw('art_id as id'), 'title', 'pubtime', 'view', 'cat_id')
           ->where('is_state', 0)->where('is_del', 1)
           ->orderBy('art_id', 'desc')->limit(5)->get()->toJson();
       //把数据插入
       $resp = $client->request('POST', 'http://127.0.0.1:7700/indexes/movies/documents',
           [
               'headers' => ['content-type' => 'application/json;charset=UTF-8','accept' => 'application/ld+json'],
               'body' => $art,
           ]
       );

       return json_decode((string) $resp->getBody(),true);
   }

}


效果随便看看:


meili流水log

log.png


增.png


改.png


数据库取出插入

db插入.png










相关文章