FOSElasticaBundle and Doctrine Hydration
Asked Answered
I

1

9

I'm busy checking out elasticsearch for a new project that I'm starting on. I'm currently running Symfony2.5, with the latest FOSElasticaBundle and all that. I'm doing some benchmarks into performance of the bundle (I know elastic in itself is pretty darn fast), but I've come across a small problem that may actually be easy to solve, if only the bundle's documentation were more verbose.

My actual question is simply how to, instead of returning an array of Doctrine objects (which I believe are populated via a DB query), to return a "raw" array of the response from Elastica itself? Including the response time, max score, etc. The docs for the FOSElasticaBundle are pretty vague on these topics...

It seems a bit counter-productive and slow to have to re-query the database when you already have all the information you're looking for from elastic? Or is there something very obvious that I'm missing here?

Thanks for any help!

Impending answered 5/6, 2014 at 20:58 Comment(0)
I
16

Found the answer... Little bit of sleep can get a person a long way it seems.

If anybody else is trying to figure this out, after going through the whole hydrate: false thing and what not (which is not related to this question in any way, by the way).

Simply use the service:

$this->get('fos_elastica.index.app.foo')->search($query) to get the Elastica Type instance.

$this->get('fos_elastica.finder.app.foo')->find($query) gets an array of Doctrine entities.

Hope this helps someone.

Impending answered 6/6, 2014 at 7:34 Comment(5)
Did you figured out if it is possible to somehow retrive JSON results in custom FOSElastica repository?Cathiecathleen
@Zap, elastica should just return an array of values, including total hits, time, etc. Most of the stuff you'd get when running straight curl queries. If you really want, you could always just use json_encode on the returned value, but I can't really see why you'd want that.Impending
@Impending it is indeed possible to use the index service. But this means you won't be able to use the pagination, which is implemented in the finder. We solved this issue by creating our own finder service. When instantiating it we insert our own elasticaToModel transformer. This transformer is just an implementation of the ORM ElasticaToModelTransformer but we have added an extra setter to disable the hydration using Doctrine objects. Instead we just return an array of the data from the Elastica result objects.Py
May I ask where you found this? I'm currently researching this and would like to learn more.Nichani
The service gets built on the fly by the FOSElasticaBundle. Using PHPStorm + Symfony Plugin, you can just do $this->get(', hit Ctrl+Space and get the available services.Impending

© 2022 - 2024 — McMap. All rights reserved.