Installation:
composer require caxy/elasticsearch-bundle:0.0.*
Add the bundle to AppKernel.php:
new Caxy\Bundle\ElasticsearchBundle\CaxyElasticsearchBundle(),
Basic Configuration (config.yml):
caxy_elasticsearch:
client:
default:
hosts: ["localhost:9200"]
First Use Case: Inject the client in a controller/service and perform a simple search:
use Caxy\Bundle\ElasticsearchBundle\Client\ClientInterface;
class SearchController extends Controller
{
public function searchAction(ClientInterface $client)
{
$params = [
'index' => 'your_index',
'body' => ['query' => ['match_all' => new \stdClass()]]
];
$response = $client->search($params);
return $this->json($response);
}
}
public function __construct(ClientInterface $elasticsearch)
{
$this->client = $elasticsearch;
}
caxy_elasticsearch:
client:
named:
analytics:
hosts: ["analytics-cluster:9200"]
Access via:
$this->container->get('caxy_elasticsearch_client.analytics');
Indexing Documents:
$params = [
'index' => 'products',
'type' => 'product',
'id' => 123,
'body' => ['name' => 'Laptop', 'price' => 999.99]
];
$this->client->index($params);
Searching with Aggregations:
$params = [
'index' => 'products',
'body' => [
'aggs' => [
'avg_price' => ['avg' => ['field' => 'price']]
]
]
];
$response = $this->client->search($params);
Bulk Operations:
$body = [
['index' => ['_index' => 'products', '_type' => 'product', '_id' => 1]],
['name' => 'Mouse', 'price' => 19.99],
['index' => ['_index' => 'products', '_type' => 'product', '_id' => 2]],
['name' => 'Keyboard', 'price' => 49.99]
];
$this->client->bulk(['body' => $body]);
$dispatcher->dispatch('elasticsearch.index', new ElasticsearchEvent($params, $response));
// In your entity
public function prePersist()
{
$this->syncToElasticsearch();
}
Host Configuration:
hosts in config.yml includes both IP and port (e.g., localhost:9200), not just localhost.curl -X GET "localhost:9200/"
Client Initialization:
ClientInterface is injected where needed.Logging:
log_path) must be writeable by the PHP process. Use absolute paths:
log_path: "%kernel.logs_dir%/elasticsearch.log"
Deprecated Methods:
Client::search() → Client::search() remains, but parameters may shift).caxy_elasticsearch:
client:
default:
hosts: ["localhost:9200"]
log_level: Logger::DEBUG
$response = $this->client->search($params);
error_log(print_r($response, true)); // Inspect full response
Custom Client Classes: Override the default client class for extended functionality:
caxy_elasticsearch:
client:
named:
custom:
class: App\Service\CustomElasticsearchClient
hosts: ["localhost:9200"]
Event Listeners:
Subscribe to bundle events (e.g., elasticsearch.client.initialize):
// services.yml
services:
app.elasticsearch.listener:
class: App\EventListener\ElasticsearchListener
tags:
- { name: kernel.event_listener, event: elasticsearch.client.initialize, method: onClientInit }
Configuration Overrides: Use environment variables for dynamic configs:
hosts: ["%env(ES_HOST)%"]
Set via .env:
ES_HOST=elasticsearch:9200
$bulkParams = ['body' => []];
foreach ($entities as $entity) {
$bulkParams['body'][] = ['index' => ['_index' => 'products', '_type' => 'product']];
$bulkParams['body'][] = $entity->toArray();
}
$this->client->bulk($bulkParams);
How can I help you explore Laravel packages today?