djfm/algolia-search-symfony-doctrine-bundle
Installation
composer require djfm/algolia-search-symfony-doctrine-bundle
Add the bundle to config/bundles.php:
Djfm\AlgoliaSearchBundle\AlgoliaSearchBundle::class => ['all' => true],
Configuration Publish the default config:
php bin/console djfm:algolia:install
Update config/packages/djfm_algolia_search.yaml with your Algolia credentials:
djfm_algolia_search:
application_id: 'YOUR_APP_ID'
search_key: 'YOUR_SEARCH_KEY'
admin_key: 'YOUR_ADMIN_KEY'
First Use Case: Indexing a Doctrine Entity
Annotate your entity with @Algolia\Index:
use Djfm\AlgoliaSearchBundle\Annotation\Index;
/**
* @Index(indexName="products")
*/
#[ORM\Entity]
class Product {}
Run the indexer:
php bin/console djfm:algolia:index:populate
@Index annotation and IndexableListener for automatic indexing.djfm:algolia:index:* commands for manual control.AlgoliaIndexEvent for custom logic during indexing.Automatic Indexing
IndexableListener to auto-index entities on prePersist, preUpdate, and preRemove.config/packages/djfm_algolia_search.yaml:
djfm_algolia_search:
listeners:
indexable: true
Manual Indexing
php bin/console djfm:algolia:index:populate --entity="App\Entity\Product"
$this->get('djfm_algolia.indexer')->indexEntity($product);
Searching
AlgoliaSearchService:
$results = $this->get('djfm_algolia.search')->search('products', 'query');
Batch Processing
php bin/console djfm:algolia:index:populate --entity="App\Entity\Product" --batch-size=100
Custom Attributes Override default indexed attributes via annotation:
/**
* @Index(indexName="products", attributes={"name", "description", "price"})
*/
class Product {}
Event Subscribers
Extend indexing logic with AlgoliaIndexEvent:
public function onIndex(AlgoliaIndexEvent $event) {
$event->getEntity()->set('custom_field', 'value');
}
Symfony Forms Integrate search results into forms:
$form->add('search', SearchType::class, [
'service' => 'djfm_algolia.search',
'index' => 'products',
]);
Caching Cache search results with Symfony’s cache system:
djfm_algolia_search:
cache: true
cache_pool: 'app.cache.search'
Index Naming Conflicts
indexName in @Index is unique per entity to avoid overwrites.Product → product).Large Datasets
--batch-size or queue workers (e.g., Symfony Messenger).Doctrine Proxy Issues
djfm_algolia_search:
ignore_proxies: true
Admin Key Exposure
admin_key to version control. Use environment variables or Symfony’s parameter_bag.Enable Logging
djfm_algolia_search:
debug: true
Logs appear in var/log/dev.log.
Check Indexed Data Use Algolia’s dashboard or CLI:
php bin/console djfm:algolia:debug:index --index="products"
Common Errors
search_key/admin_key in config.php bin/console djfm:algolia:index:create to initialize.__serialize()/__unserialize().Partial Indexing
Use partialIndex to update only changed fields:
$this->get('djfm_algolia.indexer')->partialIndexEntity($product, ['name', 'price']);
Custom Serializers
Implement AlgoliaSerializerInterface for complex objects:
class CustomSerializer implements AlgoliaSerializerInterface {
public function serialize($object, array $attributes) {
return ['custom' => $object->getCustomData()];
}
}
Register in config:
djfm_algolia_search:
serializers:
App\Entity\Product: App\Serializer\CustomSerializer
Multi-Environment Config
Use Symfony’s %env% for environment-specific keys:
djfm_algolia_search:
application_id: '%env(ALGOLIA_APP_ID)%'
Performance
indexer.delay to batch writes:
djfm_algolia_search:
indexer:
delay: 500 # milliseconds
Testing
test_products) in CI:
djfm_algolia_search:
application_id: '%env(ALGOLIA_TEST_APP_ID)%'
How can I help you explore Laravel packages today?