bangpound/elasticsearch-bundle
Installation:
composer require caxy/elasticsearch-bundle
Update AppKernel.php to register the bundle:
new Caxy\Bundle\ElasticsearchBundle\CaxyElasticsearchBundle(),
Configure Elasticsearch:
Add to config.yml:
caxy_elasticsearch:
client:
default:
hosts: ["localhost:9200"]
First Use Case: Inject the client in a controller/service:
use Caxy\Bundle\ElasticsearchBundle\Client\ClientInterface;
class SearchController
{
public function search(ClientInterface $esClient)
{
$results = $esClient->search([
'index' => 'your_index',
'body' => ['query' => ['match_all' => new \stdClass()]]
]);
return $results;
}
}
public function __construct(ClientInterface $esClient) { ... }
caxy_elasticsearch:
client:
named:
analytics:
hosts: ["analytics-es:9200"]
Access via:
$this->container->get('caxy_elasticsearch_client.analytics');
Indexing Data:
$esClient->index([
'index' => 'products',
'id' => $productId,
'body' => $productData
]);
Searching:
$params = [
'index' => 'products',
'body' => [
'query' => [
'bool' => [
'must' => [
['match' => ['name' => 'laptop']],
['range' => ['price' => ['gte' => 500]]]
]
]
]
]
];
$results = $esClient->search($params);
Bulk Operations:
$bulkBody = [];
foreach ($products as $product) {
$bulkBody[] = ['index' => ['_index' => 'products', '_id' => $product['id']]];
$bulkBody[] = $product;
}
$esClient->bulk(['body' => $bulkBody]);
Mapping Management:
$esClient->indices()->create([
'index' => 'products',
'body' => [
'mappings' => [
'properties' => [
'name' => ['type' => 'text'],
'price' => ['type' => 'float']
]
]
]
]);
kernel.request).ApiPlatform\Metadata\Operation to add Elasticsearch queries.Host Configuration:
hosts in config.yml includes both IP and port (e.g., ["localhost:9200"]).localhost/ will fail).Connection Timeouts:
caxy_elasticsearch:
client:
default:
hosts: ["localhost:9200"]
timeout: 30s
Named Clients Override:
Deprecated Methods:
termvector) may be deprecated. Check Elasticsearch PHP API docs.caxy_elasticsearch:
client:
default:
hosts: ["localhost:9200"]
log_path: "%kernel.logs_dir%/elasticsearch.log"
log_level: Logger::DEBUG
$results['hits'] and $results['_shards'] for errors:
if (isset($results['error'])) {
throw new \RuntimeException($results['error']['reason']);
}
Custom Client Classes:
Extend Caxy\Bundle\ElasticsearchBundle\Client\Client to add domain-specific methods:
class CustomClient extends Client
{
public function searchByName($index, $name)
{
return $this->search([
'index' => $index,
'body' => ['query' => ['match' => ['name' => $name]]]
]);
}
}
Register in config.yml:
caxy_elasticsearch:
client:
named:
custom:
class: App\Service\CustomClient
hosts: ["localhost:9200"]
Event Subscribers:
Listen to CaxyElasticsearchBundleEvents::CLIENT_INITIALIZED to modify the client instance:
$subscriber = new class implements EventSubscriberInterface {
public static function getSubscribedEvents()
{
return [
'caxy_elasticsearch.client_initialized' => 'onClientInitialized'
];
}
public function onClientInitialized(ClientInitializedEvent $event)
{
$event->setClient($this->addCustomLogic($event->getClient()));
}
};
Async Indexing:
Use Symfony’s Messenger component to queue indexing tasks:
$message = new IndexProductMessage($productId, $productData);
$this->messageBus->dispatch($message);
Process in a worker with the Elasticsearch client.
How can I help you explore Laravel packages today?