83 lines
3.3 KiB
Markdown
Executable File
83 lines
3.3 KiB
Markdown
Executable File
|
||
三连击:
|
||
php artisan config:clear &&
|
||
php artisan scout:flush "App\Models\Articles" &&
|
||
php artisan scout:sync-index-settings &&
|
||
php artisan scout:import "App\Models\Articles"
|
||
|
||
|
||
|
||
============
|
||
|
||
🛡️ Laravel Scout + Meilisearch 维护手册 (药水哥版)
|
||
|
||
1. 核心生存命令 (日常必点)
|
||
|
||
这些是你平时最常用的命令,每个都要烂熟于心:
|
||
|
||
全量导入 (Import) 当你第一次上线,或者数据库里突然多了几千条旧数据时执行:
|
||
|
||
Bash
|
||
php artisan scout:import "App\Models\Articles"
|
||
清空索引 (Flush) 如果你发现数据全乱了,或者想推倒重来:
|
||
|
||
Bash
|
||
php artisan scout:flush "App\Models\Articles"
|
||
同步配置 (Sync Settings) 你在 config/scout.php 里改了 searchableAttributes 或 sortableAttributes 后执行:
|
||
|
||
Bash
|
||
php artisan scout:sync-index-settings
|
||
注意:如果报错没这个命令,就用我之前教你的 Tinker 方式强制更新。
|
||
|
||
2. Tinker 调试“后门” (高级进阶)
|
||
|
||
当命令不生效时,直接进 Tinker 用代码说话:
|
||
|
||
PHP
|
||
// 1. 检查模型转出的数组对不对
|
||
App\Models\Article::first()->toSearchableArray();
|
||
|
||
// 2. 查看 Meilisearch 当前的配置详情 (看看 searchableAttributes 到底有谁)
|
||
$oClient = new Meilisearch\Client('http://127.0.0.1:7700');
|
||
dump($oClient->index('articles')->getSettings());
|
||
|
||
// 3. 强制删除某个索引(慎用,相当于删库)
|
||
$oClient->deleteIndex('articles');
|
||
3. 配置文件原则 (config/scout.php)
|
||
|
||
维护时,请确保你的 meilisearch 配置段长这样:
|
||
|
||
PHP
|
||
'meilisearch' => [
|
||
'host' => env('MEILISEARCH_HOST', 'http://127.0.0.1:7700'),
|
||
'key' => env('MEILISEARCH_KEY', null),
|
||
'index-settings' => [
|
||
'articles' => [ // 这里的名字要和 searchableAs() 一致
|
||
'searchableAttributes' => ['sTitle', 'sCategoryName', 'sContent'], // 搜索权限
|
||
'filterableAttributes' => ['iStatus', 'iCategoryId'], // 过滤权限
|
||
'sortableAttributes' => ['updated_at', 'iIsTop'], // 排序权限
|
||
],
|
||
],
|
||
],
|
||
4. 药水哥的排避坑原则 (维护必读)
|
||
|
||
“不导不生效”原则: 你改了 Article.php 里的 toSearchableArray 逻辑(比如新增了字段),必须重新跑 scout:import,否则搜索引擎里永远是旧数据。
|
||
|
||
“先配后搜”原则: 如果你想在 search()->where(...) 里用某个字段,这个字段必须先出现在 filterableAttributes 里,不然会直接报错。
|
||
|
||
“队列观察”原则: 如果在 .env 里设置了 QUEUE_CONNECTION=redis(或其它),数据同步是异步的。如果发现搜不到,先去看看队列任务(php artisan queue:work)是不是卡住了。
|
||
|
||
“匈牙利命名”贯彻: 在索引里也保持 sTitle、iStatus 这种命名,这样你在前端看到 JSON 时,一眼就能分清类型,不容易写出 1 == '1' 这种低级错误。
|
||
|
||
5. 紧急状况:搜索引擎连不上了?
|
||
|
||
如果程序报错 Connection refused,按这个顺序排查:
|
||
|
||
进程: ps aux | grep meilisearch 看看程序还在不在。
|
||
|
||
端口: lsof -i:7700 看看端口有没有被占用。
|
||
|
||
内存: Meilisearch 索引非常吃内存,如果服务器崩了,看看是不是 OOM(内存溢出)了。
|
||
|
||
兄弟,这份文档就是你的“定海神针”。以后不管是加字段还是改排序,按这个流程走,保证稳如老狗!
|