Skip to content

each_with_hit doesn't support includes to eager load associations #221

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

Closed
sr3d opened this issue Aug 29, 2014 · 5 comments
Closed

each_with_hit doesn't support includes to eager load associations #221

sr3d opened this issue Aug 29, 2014 · 5 comments

Comments

@sr3d
Copy link

sr3d commented Aug 29, 2014

collection.records.includes(:some_association).each_with_hit will error out: undefined method `each_with_hit' for #School::ActiveRecord_Relation:0x007fb13c51fe88

I looked around the source code and couldn't see it at all
(https://sourcegraph.com/github.com/elasticsearch/elasticsearch-rails@b500bf03d18f0852b9ee6ebc1aa15fe684f9a9f2/.tree/elasticsearch-model/lib/elasticsearch/model/response/records.rb). With Tire gem it's possible but somehow in the ES-Rails it's not.

@karmi
Copy link
Contributor

karmi commented Sep 1, 2014

@sr3d Yeah, I don't think this is possible, since the includes() changes the object, and we no longer have access to the search results... Would like to support it, but have no immediate idea how to do it now.

@karmi
Copy link
Contributor

karmi commented Sep 9, 2014

So, any news? I don't see this as immediately supportable/implementable. I'd lean to close this ticket.

@karmi karmi closed this as completed Sep 15, 2014
@dimroc
Copy link

dimroc commented Nov 12, 2014

Just thought I'd give an update to this. I also ran into this, and quite frankly, eager loading (using includes(:association)) is critical to performance in a lot of scenarios.

I also contemplated modifying the elasticsearch-model gem, but ended up making a class called EagerPagination that solves this. The gist can be found here, and a blog post can be found here: https://gist.github.com/dimroc/ccc6c80c747ee8f957f3

http://www.dimroc.com/2014/11/12/eager-pagination-with-elasticsearch-rails/

Let me know what you think.

@alepore
Copy link

alepore commented May 14, 2016

for reference, includes support was added: #472

@nerfologist
Copy link

nerfologist commented Nov 9, 2017

Hello, I might be missing something here but the following still fails (on elasticsearch-model 5.0.1) due to includes returning a plain ActiveRecord::Relation:

users = User.search(params)
  .records            # custom ES-rails relation
  .includes(:address) # standard AR relation! No more custom methods
  .map_with_hit { |user, hit| user._score = hit._score } # NoMethodError

# ...
undefined method `map_with_hit' for #<User::ActiveRecord_Relation:0x007fb5a6107260>

Is there any workaround for this scenario? I need to include the Elasticsearch _score for serialization.

Right now I'm forced to remove the includes() call creating a N+1 situation.

Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants