Meilisearch
composer require meilisearch/meilisearch-php guzzlehttp/guzzle http-interop/http-factory-guzzle:^1.0
https://github.com/meilisearch/meilisearch-php
Получить ключ:
#Install Meilisearch
curl -L https://install.meilisearch.com | sh
# Launch Meilisearch
./meilisearch --master-key=masterKey
docker-compose.yml:
meilisearch:
image: getmeili/meilisearch:latest
container_name: meilisearch
ports:
- "7700:7700"
volumes:
- ./www/meilisearch:/meili_data
Controller на примере YII:
public function actionIndex()
{
$client = new Meilisearch();
$client->index('artical');
return $this->render('index');
}
Выполняем post запрос
public function actionSearch()
{
$text = Yii::$app->request->post('text');
$client = new Meilisearch();
$hits = $client->search('artical',$text);
if (count($hits) == 0){
$hits='Ничего не найдено';
}
return $this->render('search', ['text' => $hits]);
}
Model:
class Meilisearch
{
private string $host = 'http://___________:7700';
private string $token = key;
public Client $client;
public function __construct()
{
$this->client = new Client($this->host, $this->token);
}
public function index(string $db_name)
{
$name = 'app\models\\' . ucfirst($db_name);
$index = $this->client->index($db_name);
if (class_exists($name)) { //проверка на существование модели
$documents = $name::find()->all();
$setDocuments = [];
foreach ($documents as $document) { // загружаем данные из БД
$setDocuments[] = $document->attributes;
}
$index->addDocuments($setDocuments); // добавляем документы
}else{
echo 'Model not find!';
}
}
// поиск строки по модели
public function search(string $db_name, string $text)
{
$hits = $this->client->index($db_name)->search($text)->getHits();
return $hits;
}
}
View:
if (is_array($text)){
foreach ($text as $value){
echo $value['title'].'<br>';
echo $value['content'].'<br>';
echo $value['first_name'].' '.$value['last_name'].'<br>';
echo $value['registered_at'].'<br>';
}
}else{
echo $text;
}