Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Algolia Search Symfony Doctrine Bundle Laravel Package

djfm/algolia-search-symfony-doctrine-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require djfm/algolia-search-symfony-doctrine-bundle
    

    Add the bundle to config/bundles.php:

    Djfm\AlgoliaSearchBundle\AlgoliaSearchBundle::class => ['all' => true],
    
  2. 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'
    
  3. 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
    

Where to Look First

  • Doctrine Integration: Focus on the @Index annotation and IndexableListener for automatic indexing.
  • Commands: Check djfm:algolia:index:* commands for manual control.
  • Event System: Use AlgoliaIndexEvent for custom logic during indexing.

Implementation Patterns

Workflows

  1. Automatic Indexing

    • Use the IndexableListener to auto-index entities on prePersist, preUpdate, and preRemove.
    • Example: Enable in config/packages/djfm_algolia_search.yaml:
      djfm_algolia_search:
          listeners:
              indexable: true
      
  2. Manual Indexing

    • Trigger indexing via command:
      php bin/console djfm:algolia:index:populate --entity="App\Entity\Product"
      
    • Or programmatically:
      $this->get('djfm_algolia.indexer')->indexEntity($product);
      
  3. Searching

    • Use the AlgoliaSearchService:
      $results = $this->get('djfm_algolia.search')->search('products', 'query');
      
  4. Batch Processing

    • For large datasets, use chunked indexing:
      php bin/console djfm:algolia:index:populate --entity="App\Entity\Product" --batch-size=100
      

Integration Tips

  • 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'
    

Gotchas and Tips

Pitfalls

  1. Index Naming Conflicts

    • Ensure indexName in @Index is unique per entity to avoid overwrites.
    • Defaults to lowercase class name (e.g., Productproduct).
  2. Large Datasets

    • Avoid indexing millions of records at once; use --batch-size or queue workers (e.g., Symfony Messenger).
  3. Doctrine Proxy Issues

    • Disable proxy generation for indexed entities if using custom serialization:
      djfm_algolia_search:
          ignore_proxies: true
      
  4. Admin Key Exposure

    • Never commit admin_key to version control. Use environment variables or Symfony’s parameter_bag.

Debugging

  • 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

    • "Invalid API Key": Verify search_key/admin_key in config.
    • "Index Not Found": Run php bin/console djfm:algolia:index:create to initialize.
    • Serialization Errors: Ensure entity properties are public or use __serialize()/__unserialize().

Tips

  1. Partial Indexing Use partialIndex to update only changed fields:

    $this->get('djfm_algolia.indexer')->partialIndexEntity($product, ['name', 'price']);
    
  2. 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
    
  3. Multi-Environment Config Use Symfony’s %env% for environment-specific keys:

    djfm_algolia_search:
        application_id: '%env(ALGOLIA_APP_ID)%'
    
  4. Performance

    • Debounce Indexing: Use indexer.delay to batch writes:
      djfm_algolia_search:
          indexer:
              delay: 500 # milliseconds
      
    • Async Indexing: Offload to a queue (e.g., Symfony Messenger) for high-traffic apps.
  5. Testing

    • Use Algolia’s test indices (e.g., test_products) in CI:
      djfm_algolia_search:
          application_id: '%env(ALGOLIA_TEST_APP_ID)%'
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware