-
Notifications
You must be signed in to change notification settings - Fork 802
Result order is not preserved with records adapters (Mongoid in my case). #99
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 see that you have to call to_a on the records to have it in the order elasticsearch returned. However, each_with_hit function does not call to_a before it gets zipped. So the problem still exist. And should we have to be calling to_a on records to ensure order every time? |
So, paraphrasing if I got you right:
|
I've checked in the broken test. |
Not sure I get the problem. The test in #102 runs fine for me, and when I check it manually, it all sorts out:
|
So, there's indeed a problem with Mongoid:
Notice how the results are flipped on the last line. It doesn't matter, though, in what order Mongoid returns the records, since we reorder them in the adapter: https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/lib/elasticsearch/model/adapters/mongoid.rb#L23 For whatever reason, the reordering is not working as expected. |
This just tripped me up a bit until I found this. Mongoid does not seem to care about the order of the IDs passed into its Might want to update the documentation here: https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model#search-results-as-database-records to be explicit that you should call |
Hi.
Let say i have two records and I'm running a query such that the order of the resulting records are reversed. I'm running a simple function score query and I can see that record #2 has higher score than record #1 (in response.response). But when I get the records, the result order is back to natural order. I tried to show the records' scores with each_with_hit function and saw that the scores are switched. I looked into the code and saw two problems. In the adapter code, it's using $in query for array of ids. I believe mongo returns results in natural order unless sort is defined. It will not return the results with the ids' array order. Then in the records class, this db result ( #1, #2) is zipped with elasticsearch result (#2, #1). Hence the scores get switched.
fyi
https://jira.mongodb.org/browse/SERVER-7528
The text was updated successfully, but these errors were encountered: