Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5520

Criteria ordering fails for non-numeric values (ex: { '$meta' => 'textScore' })

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 8.1.0, 9.0.0, 8.0.4
    • Affects Version/s: None
    • Component/s: None
    • None

      This specifically affects Text Searches within Mongoid, which can limit their utility with associated Gems such as kaminari-mongoid. Note that sorting by Text Search score requires an object as opposed to single field ordering which require an integer to indicate direction.

      For example:

      require 'bundler/inline'
      
      gemfile do
        source 'https://rubygems.org'
      
        gem 'mongoid'
      end
      
      Mongoid.configure do |config|
        config.clients.default = { uri: "mongodb://localhost:27017/test" }
      end
      
      class Band
        include Mongoid::Document
      
        field :name, type: String
        field :description, type: String
      
        index description: 'text'
      end
      
      Band.collection.drop
      Band.create!
      Band.create_indexes
      
      begin
        Band.where('$text' => {'$search' => 'ounces'}).order(score: { '$meta' => 'textScore' })
      rescue => ex
        puts ex
        puts ex.backtrace
      end
      
      # output
      undefined method `to_direction' for {"$meta"=>"textScore"}:Hash
      Did you mean?  to_criteria
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/extensions/hash.rb:121:in `block (2 levels) in __sort_option__'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/extensions/hash.rb:117:in `each_pair'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/extensions/hash.rb:117:in `block in __sort_option__'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/extensions/hash.rb:116:in `tap'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/extensions/hash.rb:116:in `__sort_option__'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/optional.rb:172:in `block (2 levels) in order_by'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/optional.rb:171:in `each'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/optional.rb:171:in `block in order_by'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/optional.rb:340:in `block in option'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/optional.rb:338:in `tap'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/optional.rb:338:in `option'
      /Users/alex/.rvm/gems/ruby-2.7.5/gems/mongoid-8.0.2/lib/mongoid/criteria/queryable/optional.rb:170:in `order_by'
      /Users/alex/.rvm/rubies/ruby-2.7.5/lib/ruby/2.7.0/forwardable.rb:235:in `order'
      text_search.rb:24:in `<main>'
      

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            alex.bevilacqua@mongodb.com Alex Bevilacqua
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: