I'm using elasticsearch in symfony2 the FOSElasticaBundle and was wondering what is standard practice for retrieving objects after searching.
Say I have mapped all fields of my doctrine object so they are all stored, more importantly returned, in elasticsearch. Now, after I search and have an Elastica\ResultSet
. The default finder in the FOSElasticaBundle will ignore the data in the ResultSet
and query the sql db to get Doctrine objects (which are then passed to the template to be drawn).
I was wondering if this was commonly done (two queries to different datastores) as it seems a bit wasteful. Or do people create Doctrine objects manually from the
ResultSet
.If you do create the objects manually, it could be possible that elasticsearch's data is outdated. Now this is not a problem for viewing, but wouldn't creating doctrine objects from outdated data possibly cause this old data to get persisted (if some part of the code flushes; we had this issue with symfony 1.4 and doctrine 1.2).
Thanks!
Update
After reading a bit more, I have 2 ideas (except for the easy but wasteful extra sql query method):
Create a custom ElasticaToModelTransformer, and also detach the entities from doctrines entity manager, ensuring they do not get persisted accidentally. Reading a bit more from the doctrine docs it seems like I might not actually need to do this. Are unserialized objects attached to the entity manager?
Create a normalized php array representing the object and have all the templates dray from this array rather than the doctrine object. As talked about on the symfony serializer component docs this is a state between the object as an instance of an entity, and the object serialized into some format (JSON, xml), and so this array could be created by either an elasticsearch result or a doctrine object and the template does not need to care.
This approach does mean you cannot call methods to retrieve related objects from the template if needed, you are limited to the info in the array.