I'm using FOSElasticaBundle and Doctrine in my project, and my code works for the selective index update using the Doctrine lifecycle events. The issue I come up against is if I an update a related entity separately.
For example a person may be related to a company through a manytomany relationship. If I update the company name through company entity directly, then indexes for the person related to the company will be out of date and still relate to the company's old name.
I'm a bit lost as to how to handle this, does anyone have any suggestions? Do I have to rely on a scheduled index update and cope with inaccurate index data in the mean time, or is there a way I can call an update for entities related to the entity that has been updated.
I am relying on JMSSerializer groups to establish the mappings. I appreciate this might not be the best way to do things in the longterm.
I've had the same problem. It seems my installation (Symfony 2.5.4 and FOSElastica 3.0.4) differs quite a bit from yours though. Therefore, there were some problems to get the code working. I'm posting my solution, because it may be useful for other developers out there.
The Listener isn't in FOS\ElasticaBundle\Doctrine\ORM\, but in FOS\ElasticaBundle\Doctrine. So you'll have to use that one. Also I had to use Doctrine\Common\EventArgs instead of Doctrine\ORM\Event\LifecycleEventArgs, 'cause otherwise my own postUpdate-method wasn't compatible with the one in the BaseListener.
In my app, a course (seminar) can have a lot of sessions, but in this project, elastica will only be using those sessions. The app needs to know some details of the course that is related to the session of course. So, here's my code:
In config.yml my elastica bundle config looks like this:
A little further, still in config.yml
My own listener (XXX\CourseBundle\EventListener\ElasticaCourseListener) then looks like this:
Now, when I update a course, it will be updated as a nested object in ElasticSearch ;-)
I'm using FosElastica 3.1.0 and I have tried the solution provided by Julien Rm without success :-(
After many days of research, I finally found the solution here
Hope this help !
with all comments and my research, I made a generic Gist for auto index child objects with fosElastica:
https://gist.github.com/Nightbr/ddb586394d95877dde8ed7445c51d973
In fact, I override the default Listener from FOSElastica and I add the
function updateRelations($entity)
. We will search all relations linked to the$entity
and if there are indexed in ES (the ES type exists) it will update the related documents.If anyone want to look at it and make any improvement it would be great! ^^
Thanks in advance
With the BC Break #729 of FosElastica 3.1.0, things have changed and the code above wasn't working :
For those who are trying to make it work with FOSElastica 3.1.X here is how I did manage to make a nested objected to be indexed into his parent into Elastic Search when persisting/updating/removing a nested entity :
Define the service listener :
Create the listener :
Hope this help !
I think I've found the solution on this page https://groups.google.com/forum/#!topic/elastica-php-client/WTONX-zBTI4 Thanks Cassiano
Basically you need to extend the FOS\ElasticaBundle\Doctrine\ORM\Listener so you can look for related entities and then update their index as well.
and your services defined as below
this will then update indexes for both :-)
Sorry, i can not comment under your answer but something is missing in the solution. You have to override preRemove too.