meilisearch/meilisearch-php
Official PHP client for Meilisearch, the open‑source search engine. Connect to Meilisearch or Meilisearch Cloud to index documents, configure indexes, and run fast, typo‑tolerant searches. Supports customizable HTTP clients and common PHP tooling.
addDocuments() can be dispatched to a queue).ServiceProvider (e.g., bind Meilisearch\Client to the container)..env integration (e.g., MEILI_HOST, MEILI_API_KEY).addDocuments()) can leverage Laravel’s queue system.Cache::remember()).| Risk Area | Mitigation Strategy |
|---|---|
| API Versioning | Monitor Meilisearch’s roadmap for breaking changes. Use semantic versioning in Laravel’s composer.json. |
| Performance | Benchmark indexing/search latency under production load. Consider index sharding for large datasets. |
| Error Handling | Wrap Meilisearch API calls in Laravel’s try/catch and log errors via Log::error(). |
| Dependency Bloat | Only install required HTTP clients (e.g., guzzlehttp/guzzle) to avoid unnecessary dependencies. |
| Self-Hosting vs. Cloud | Evaluate Meilisearch Cloud for managed hosting or self-host for compliance/control. |
guzzlehttp/guzzle (most mature, widely used).symfony/http-client, php-http/curl-client (if Guzzle is avoided).addDocuments() to Laravel’s queue (e.g., bus:queue) for async indexing.use Meilisearch\Client;
use Illuminate\Support\Facades\Bus;
Bus::dispatch(function () {
$client->index('products')->addDocuments($newProducts);
});
Cache::remember() or use Laravel’s Cache::tags() for invalidation.$results = Cache::remember("meili_search_{$query}", now()->addMinutes(5), function () use ($index, $query) {
return $index->search($query)->getHits();
});
docker run -p 7700:7700 getmeili/meilisearch:v1.3).DB::table('products')->where(...)) with Meilisearch.// Before: Database query
$products = Product::where('name', 'like', '%phone%')->get();
// After: Meilisearch
$hits = $client->index('products')->search('phone')->getHits();
public function handle(Request $request, Closure $next) {
if ($request->has('search')) {
$results = $this->meilisearch->search($request->search);
return response()->json($results);
}
return $next($request);
}
use Meilisearch\Client;
use App\Models\Product;
$client = new Client(config('meilisearch.host'), config('meilisearch.key'));
$index = $client->index('products');
$index->addDocuments(Product::all()->toArray());
ScoutDriver with a custom Meilisearch driver:
// app/Providers/AppServiceProvider.php
public function boot() {
Scout::extend('meilisearch', function ($app) {
return new MeilisearchScoutDriver($app['meilisearch.client']);
});
}
search() instead of where()).HttpTests to mock Meilisearch responses:
$this->mock(Meilisearch\Client::class, function ($mock) {
$mock->shouldReceive('search')->andReturn(['hits' => []]);
});
.env:
MEILI_HOST=http://localhost:7700
MEILI_API_KEY=masterKey
AppServiceProvider:
$this->app->singleton(Meilisearch\Client::class, function ($app) {
return new Client(
config('meilisearch.host'),
config('meilisearch.key'),
new GuzzleHttp\Client()
);
});
config/meilisearch.php):
return [
'indexes' => [
'products' => [
'primaryKey' => 'id',
'filterableAttributes' => ['category', 'price_range'],
],
],
];
php artisan meilisearch:seed
composer.json to pin the Meilisearch client version:
"meilisearch/meilisearch-php": "^2.0"
single channel:
try {
$index->addDocuments($docs);
} catch (\Exception $e) {
Log::error("Meilisearch indexing failed: " . $e->getMessage());
}
How can I help you explore Laravel packages today?