Skip to content

mongoid sorting bug with .records #909

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
ajsharp opened this issue Sep 9, 2019 · 2 comments
Closed

mongoid sorting bug with .records #909

ajsharp opened this issue Sep 9, 2019 · 2 comments
Labels

Comments

@ajsharp
Copy link

ajsharp commented Sep 9, 2019

The mongoid integration has the same sorting bug as documented in #546 and others. The records. I'm using elasticsearch-model 7.0.

I have a field in my mapping called score (separate from the elasticsearch _score). I'm running a search query like this:

def do_search
  search(
    query: {
      bool: {
        must: [
          {term: {account_id: '123'}}
        ]
      }
    },
    sort: [ {score: order: 'desc'}} ]
  )

end

I've confirmed that the results come back in the proper order from elasticsearch by visually inspecting the results like this: Model.search.map(&:score). However, when I call .records on the results, the scores get jumbled, and I have to re-sort them manually in ruby.

I see that there's some code trying to solve this here, because mongoid doesn't return results in id order: https://github.com/elastic/elasticsearch-rails/blob/v7.0.0/elasticsearch-model/lib/elasticsearch/model/adapters/mongoid.rb#L40.

It looks like you can call a mongoid scope method like order(sort: :desc) on the object records returns to get the proper order, but the .to_a method doesn't seem to be working for some reason.

@stale
Copy link

stale bot commented Aug 31, 2020

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.

@stale stale bot added the stale label Aug 31, 2020
@stale stale bot closed this as completed Sep 7, 2020
@vanboom
Copy link

vanboom commented Apr 26, 2023

Confirmed also with 8.7.0, calling results returns the records in the proper order, while calling records does not match up or appear to apply any ordering. We are simply attempting to use the default ranking by _score.

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

No branches or pull requests

2 participants