Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eager loading/include associations at serialize time? #383

Closed
mehulkar opened this issue Feb 5, 2015 · 3 comments
Closed

eager loading/include associations at serialize time? #383

mehulkar opened this issue Feb 5, 2015 · 3 comments

Comments

@mehulkar
Copy link
Contributor

mehulkar commented Feb 5, 2015

I'm using ActiveModelSerializers to render json. In some cases, I'm calling #to_a on instances of Searckick::Results and rendering that. But my serializers have associations that are not indexed in elasticsearch. This is ending up in a n+1 query to the database and makes queries extremely slow.

Is there a way to eager load these associations at render time?

To be honest, I'm not sure if a good solution lies in Elasticsearch, ActiveRecord, Rails, AMS, or searchkick land. I don't think this is a searchkick issue, but opening it here because @ankane has been really helpful in the past and this seems like something other searchkick users could benefit from knowing. Stackoverflow is fairly unresponsive for niche issues like this.

@mehulkar
Copy link
Contributor Author

mehulkar commented Feb 5, 2015

Right now, the best solution seems to be that I map the ids from the searchkick response and make another database call like this:

MyModel.includes(associations).where(id: searchkick_results.map(&:id))

This makes another db call, but I was able to improve total roundtrip request time by about 10x this way.

@ankane
Copy link
Owner

ankane commented Feb 8, 2015

Hey Mehul, you can use the include option to eager load association.

Product.search "milk", include: [:brand, :stores]

@mehulkar
Copy link
Contributor Author

mehulkar commented Feb 8, 2015

Oh nice. i though the include option was used to eager load when indexing. missed that part of the docs. Thanks.

@mehulkar mehulkar closed this as completed Feb 8, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Dec 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants