Skip to content

Instantly share code, notes, and snippets.

@shopapps
Created April 18, 2024 07:48
Show Gist options
  • Save shopapps/e6ea29c4e208388e3f381703165a07ec to your computer and use it in GitHub Desktop.
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
<?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;
}
}
@shopapps
Copy link
Author

example usage:

 php artisan meillisearch:settings --index_name="aws_files_index" --maxTotalHits=20000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment