-
Notifications
You must be signed in to change notification settings - Fork 802
ActiveRecord collection not (obviously) sorted by score #608
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
Comments
I also upgraded from Rails 4.2 to Rails 5 and ran into the same sorting issue. I explicitly had a sort term in the ES query using the elasticsearch dsl. The ES response was correctly sorted, but when using |
As a stop gap, I have been using this custom rows = ElasticRelation.new Row.search(params).records It essentially sorts in memory as originally implemented here: def sorted_by_score
hits = records.response.response.hits.hits
records.sort_by do |record|
hits.index { |hit| hit['_id'].to_s == record.id.to_s }
end
end Figuring out how to plug this into Rails 5 AR is the fun part. |
AR 5 delegates array methods on records method: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation.rb#L257 I guess the name of the singleton method redefined by elasticsearch-models should depends on ActiveRecord version |
Confirmed about the bug happening for Rails 5 upgrading from Rails 4.2 |
Any progress on this? |
I think it has been merged. Read the comments on the referenced PR. |
First of all, my apologies, @alexceder, for not responding to this issue sooner, and many thanks for the care you took in providing all the details in the issue description. Also, many thanks for everyone responding on this thread! As @johanlunds says, the fix by @inkstak has indeed been merged through pull request #618. To reply to the original question by @alexceder, I do agree that the whole story about needing to call @alexceder, @sr3d, can you check your code with the current |
I'm not sure if this was fixed for ActiveRecord, but I have exactly the same issue with Mongoid |
I'm also having this issue with Mongoid ;/ |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
All this came about when updating from Rails 4.2 to Rails 5.
I have hade some trouble with Elasticsearch not returning ActiveRecord models in order of score.
Reviewing the response, I can see that they are correctly returned in order by score so that:
However this is not what I want:
And this is where you might thing.. well you need to call
to_a
on the records. And yes, that works.Just as the README states, it runs the corresponding SQL with only a
WHILE
statement. This confused me, where do you sort!?https://github.com/elastic/elasticsearch-rails/blob/master/elasticsearch-model/README.md#search-results-as-database-records
The sorting is done when explicitly calling
to_a
. This is of course fine, but this isn't the big thing that confused me or where my errors appeared.I first used this solution, but I thought it was rather ugly. This way I get the records sorted properly from the database, on primary key and not undefined behaviour.
But scrap that, I don't want to write custom SQL for which should happen automagically.
So why am I writing this loooong wall of text.. Well, it seems like Rails has changed how it uses implicit convertions.. or something.. so.. This is probably where something happens (or more likely doesn't happen):
Because if I explicitly convert to array, all is well.
This might be more of a gotcha, but it was a pain for me, and might be for others.
Is there a better way of doing this? Some other intended use? Is this dumb? Could the "default" order be done like I did it? Ideas? Dumb?
The text was updated successfully, but these errors were encountered: