Skip to content

mongoid sorting bug with .records #909

@ajsharp

Description

@ajsharp

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions