Elasticsearch provides powerful search beyond what databases offer. Laravel integration enables fuzzy matching, faceted search, and blazing-fast queries. At ZIRA Software, Elasticsearch powers search across millions of documents with sub-second response times.
Installation
Install Elasticsearch client:
composer require elasticsearch/elasticsearch
Configure connection:
// config/elasticsearch.php
return [
'hosts' => [
env('ELASTICSEARCH_HOST', 'localhost:9200'),
],
];
Basic Integration
Create service:
use Elasticsearch\ClientBuilder;
class ElasticsearchService
{
protected $client;
public function __construct()
{
$this->client = ClientBuilder::create()
->setHosts(config('elasticsearch.hosts'))
->build();
}
public function index($index, $id, $body)
{
return $this->client->index([
'index' => $index,
'id' => $id,
'body' => $body,
]);
}
public function search($index, $query)
{
return $this->client->search([
'index' => $index,
'body' => [
'query' => $query,
],
]);
}
}
Model Observer
Auto-index on save:
class PostObserver
{
public function saved(Post $post)
{
app(ElasticsearchService::class)->index('posts', $post->id, [
'title' => $post->title,
'content' => $post->content,
'tags' => $post->tags->pluck('name'),
]);
}
}
Conclusion
Elasticsearch transforms search capabilities. Laravel integration is straightforward with powerful results.
Need advanced search for your application? Contact ZIRA Software for Elasticsearch implementation.