87 lines
1.7 KiB
PHP
87 lines
1.7 KiB
PHP
|
|
<?php namespace RainLab\Blog\Components;
|
||
|
|
|
||
|
|
use Lang;
|
||
|
|
use Response;
|
||
|
|
use Cms\Classes\Page;
|
||
|
|
use Cms\Classes\ComponentBase;
|
||
|
|
use RainLab\Blog\Models\Post as BlogPost;
|
||
|
|
use RainLab\Blog\Models\Category as BlogCategory;
|
||
|
|
use Carbon\Carbon;
|
||
|
|
use GuzzleHttp\Client;
|
||
|
|
|
||
|
|
class Meilisearch extends ComponentBase
|
||
|
|
{
|
||
|
|
|
||
|
|
public $posts;
|
||
|
|
|
||
|
|
public $query;
|
||
|
|
|
||
|
|
public $page;
|
||
|
|
|
||
|
|
|
||
|
|
public function componentDetails()
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'name' => 'meilisearch',
|
||
|
|
'description' => 'meilisearch'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function onRun()
|
||
|
|
{
|
||
|
|
$this->posts = $this->listPosts();
|
||
|
|
//$this->page = $this->listPosts();
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function listPosts()
|
||
|
|
{
|
||
|
|
|
||
|
|
$query = get('q');
|
||
|
|
$page = get('page');
|
||
|
|
//dd($query);
|
||
|
|
if(is_null($page))
|
||
|
|
{
|
||
|
|
$page = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
if(is_null($query))
|
||
|
|
{
|
||
|
|
$query = 'здравоохранения';
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->page = $page;
|
||
|
|
|
||
|
|
$this->query = $query;
|
||
|
|
|
||
|
|
|
||
|
|
$per_page = 2;
|
||
|
|
$offset=$per_page * ($page - 1);
|
||
|
|
|
||
|
|
$client = new Client([
|
||
|
|
'headers' => [ 'Content-Type' => 'application/json' ]
|
||
|
|
]);
|
||
|
|
|
||
|
|
|
||
|
|
$response = $client->post('http://localhost:7700/indexes/posts_index/search',
|
||
|
|
['body' => json_encode(
|
||
|
|
[
|
||
|
|
'q' => $query,
|
||
|
|
'limit' => $per_page,
|
||
|
|
'offset' => $offset + $per_page,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
);
|
||
|
|
|
||
|
|
|
||
|
|
$res = json_decode($response->getBody()->getContents());
|
||
|
|
|
||
|
|
//$query = post('query');
|
||
|
|
//dd($res);
|
||
|
|
return $res->hits;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|