Code Snippets
webadmin | February 13, 2026 , 7 min read
Table Of Content
curl -s "https://laravel.build/laravel-elasticsearch" | bash
cd laravel-elasticsearch
./vendor/bin/sail up
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.7.0
environment:
- discovery.type=single-node
- xpack.security.enabled=false
ports:
- "9200:9200"
# Then restart Sail:
./vendor/bin/sail down
./vendor/bin/sail up -d
./vendor/bin/sail composer require elasticsearch/elasticsearch
<?php
use Elasticsearch\ClientBuilder;
class ElasticsearchService
{
protected $client;
public function __construct()
{
$this->client = ClientBuilder::create()
->setHosts(['elasticsearch:9200'])
->build();
}
public function index(string $index, array $body)
{
return $this->client->index([
'index' => $index,
'body' => $body,
]);
}
public function search(string $index, array $query)
{
return $this->client->search([
'index' => $index,
'body' => $query,
]);
}
public function bulk(array $params)
{
return $this->client->bulk($params);
}
}
PS C:\Users\pc> php artisan make:observer ProductObserver --model=Product
use App\Models\Product;
use App\Observers\ProductObserver;
public function boot()
{
Product::observe(ProductObserver::class);
}
$es = app(ElasticsearchService::class);
$es->index('products', [
'id' => 1,
'name' => 'Blue Sneakers',
'price' => 89.99
]);
$query = [
'query' => [
'match' => [
'name' => 'sneakers'
]
]
];
$results = $es->search('products', $query);
Related Blogs
15 Best Payment Gateways in the UAE in 2025 [Latest Updated]
Discover the top 15 payment gateway options in the UAE, including features, and pricing plans for businesses seeking secure online transactions.
Suprabhat Sen
Nov 25 ,
13 min read
Top 13 Mobile Payment Gateway Solutions [2025]
Discover the top 13 mobile payment gateways, with key features, pros, cons, and integration tips to help you choose the best solution for your app.
Suprabhat Sen
Nov 8 ,
18 min read
10 Best Ecommerce Payment Gateways in UAE
Explore the best ecommerce payment gateways in UAE. Compare providers, costs, compliance, and features to choose the right solution for your business.
Suprabhat Sen
Sep 30 ,
13 min read

