-
Notifications
You must be signed in to change notification settings - Fork 801
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
[MODEL] Add the :includes option to ActiveRecord adapter #472
Conversation
that can be used to eager load submodels just like you'd do with regular ActiveRecord. This helps eliminate the N+1 problem when using elasticsearch-model searches. Example: class Article < ActiveRecord::Base belongs_to :author # ... end Article.search(...).records(includes: [:author]) # Would be similar to using: Article.includes(:author).where(...)
It definitely makes sense to nicely support the behaviour, I'm just wondering, since |
You can technically call
I guess I could do
It seems like it works fine, but that's not ideal. |
Actually, it's not working as I expected...
The Elasticsearch query is performed twice! |
Having same issue, this PR would help a lot! |
I've used this approach - but still, elasticsearch could support this feature, since approach I've used is hacky. |
That seems like a pretty nice solution, well done. What I had done personally is just give up on kaminari pagination, using |
Hi, @karmi. Any plans to get this merged? |
My apologies to all, and first of all to you, @dabit, this took too long. Thanks for pinging me, @brianstorti! I've added an integration test in the attached commit, and merged. I wanted to explore if we couldn't fix the more natural way of expressing the same, Thanks for the link to the decorator article, @donce! |
@karmi no worries, this is great timing as precisely yesterday I had to patch one of my apps with this change and I did get a considerable performance boost. When there's a lot of data showing up in a page from different related models, that N+1 situation can kill you. Thanks! |
@brianstorti, I'd like to accumulate more changes before a release to Rubygems, but if you need a release for your environment and cannot just use Git in |
Actually, there has been a lot accumulated already :), so just released |
Was excited to find this PR, but having some issues implementing: gem 'elasticsearch-rails', '~> 0.1.9'
has_many :product_images, dependent: :destroy
results = Product.search(query)
results = results.page(params[:page]).records(includes: [:product_images]) I get an argument error: Have I misunderstood how to implement this? Thanks in advance! |
I don't think you need to call results if you're calling records. What about just |
that can be used to eager load submodels just like
you'd do with regular ActiveRecord.
This helps eliminate the N+1 problem when using
elasticsearch-model searches.
Example:
There might be implications that I'm not considering for other adapters but it works for me with ActiveRecord and all tests are green.