I've an Entity strain that have a collection of Locus Entities. I use Elasticsearch to index the Locus Documents (in each locus document I refer to a Strain attribute).
I would want do something like: when the user edit the Strain entity, it manually re-index (update) all the Locus documents (because I need to update the Strain attribute in all Locus Documents).
To do it I've choose to use a Listener on the Doctrine PostUpdate event, and check if it's a Strain object. But I don't know how to ask to elasticsearch the reindexation on my objects. Someone know how to do it ?
<?php
namespace AppBundle\EventListener;
use AppBundle\Entity\Strain;
use Doctrine\ORM\Event\LifecycleEventArgs;
class StrainListener
{
public function postUpdate(LifecycleEventArgs $args)
{
$em = $args->getEntityManager();
$strain = $args->getEntity();
if (!$strain instanceof Strain) {
return;
}
$locusList = $em->getRepository('AppBundle:Locus')->findLocusFromStrain($strain);
// A command that ask to Elasticsearch reindex Locus
return;
}
}
Finally I've find a solution: ObjectPersister (FOS\ElasticaBundle\Persister\ObjectPersister) with the method: ->replaceMany(array);
<?php
namespace AppBundle\EventListener;
use AppBundle\Entity\Strain;
use Doctrine\ORM\Event\LifecycleEventArgs;
use FOS\ElasticaBundle\Persister\ObjectPersister;
class StrainListener
{
private $objectPersister;
public function __construct(ObjectPersister $objectPersister) {
$this->objectPersister = $objectPersister;
}
public function postUpdate(LifecycleEventArgs $args)
{
$strain = $args->getEntity();
if (!$strain instanceof Strain) {
return;
}
$em = $args->getEntityManager();
$locusList = $em->getRepository('AppBundle:Locus')->findLocusFromStrain($strain);
$this->objectPersister->replaceMany($locusList);
return;
}
}