Created
April 18, 2024 07:48
-
-
Save shopapps/e6ea29c4e208388e3f381703165a07ec to your computer and use it in GitHub Desktop.
laravel console command file to update settings in meillisearch engine, specifically the maxTotalHits value to increase beyond 1000
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Meilisearch\Client; | |
class MeillisearchSettings extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'meillisearch:settings {--index_name=? : index to apply settings} {--maxTotalHits=? : Change pagination.maxTotalHits value} {--model= : model to apply settings to}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Update Meilisearch settings for a model'; | |
/** | |
* Execute the console command. | |
*/ | |
public function handle() | |
{ | |
$index_name = $this->option('index_name'); | |
if(empty($index_name)) { | |
$model = resolve($this->option('model')); | |
if (empty($model)) { | |
$this->error('Index or Model is required'); | |
return 1; | |
} | |
$index_name = $model->searchableAs(); | |
} | |
$maxTotalHits = (int) $this->option('maxTotalHits'); | |
$settings = []; | |
if($maxTotalHits) { | |
data_set($settings, 'pagination.maxTotalHits', $maxTotalHits); | |
} | |
$client = new Client(config('scout.meilisearch.host'), config('scout.meilisearch.key')); | |
$client->index($index_name)->updateSettings($settings); | |
$this->info('Settings updated for ' . $index_name . ' index to ' . $maxTotalHits); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example usage: