ObjectPersister configuration in FOSElasticaBundle

646 views Asked by At

For my project I need a simple API for inserting documents into ElasticSearch. FOS\ElasticaBundle\Persister\ObjectPersister seems to be the right thing for me, but I see that the service defined with this class is abstract:

<service id="fos_elastica.object_persister" class="FOS\ElasticaBundle\Persister\ObjectPersister" abstract="true">
            <argument /> <!-- index -->
            <argument /> <!-- model to elastica transformer -->
            <argument /> <!-- model -->
            <argument /> <!-- properties mapping -->
            <argument /> <!-- options -->
</service>

How can I configure it to be able to inject into my services? Perhaps I should change smth in fos_elastica.yaml, which now looks like this:

fos_elastica:
    clients:
        default:
            url: '%env(ELASTICSEARCH_URL)%'
    indexes:
        categories: ~
1

There are 1 answers

2
Dylan KAS On

Modify your fos_elastica config with:

fos_elastica:
    default_manager: mongodb
    clients:
        default:
            url: '%env(ELASTICSEARCH_URL)%'
    indexes:
        app:
            types:
                categories:
                    persistence:
                        # the driver can be orm, mongodb or phpcr
                        driver: mongodb
                        model: App\Document\Category #or whatever it is
                        provider: ~
                        finder: ~

You may need to modify your doctrine_mongodb configuration:

document_managers:
        default:
            auto_mapping: true

And you should be able to persist your Category entity with MongoDb object manager.

use Doctrine\ODM\MongoDB\DocumentManager;

class YourService {

   private $documentManager;   

   public function  __construct(DocumentManager $documentManager){
       $this->documentManager= $documentManager;
   }

   public function yourService(){
       //use $this->documentManager
   }

}