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

scoped text searches wipe all previous criteria

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.1.0.rc0, 7.0.3, 6.4.4
    • Affects Version/s: 7.0.2, 6.4.2
    • Component/s: Query
    • Labels:
      None
    • Environment:
      Ubuntu/Alpine - ruby 2.4.1

      Greetings,

      A scoped text search will clear all previous criteria, producing wrong output.

      Here is the demonstration code:

      class MyDocuments
        include Mongoid::Document
      
        scope :text_search, ->(search) {
          where(
            {
              '$text' => { '$search' => search }
            }
          )
        }
      
        field :category, type: Symbol, default: nil
      
        field :body, type: String, default: ''
      
        index({
          body: 'text'
        })
      end
      
      ::Mongoid::Tasks::Database.create_indexes
      
      MyDocuments.delete_all
      
      MyDocuments.create(category: :fruit, body: 'Apple - I love apples from Washington state')
      MyDocuments.create(category: :fruit, body: 'Eggplant - Eggplants are batonically a fruit')
      
      batonically = 'batonically'
      
      puts "Criteria 1"
      puts MyDocuments.where(category: :fruit).text_search(batonically).criteria.inspect
      puts
      puts "Criteria 2"
      puts MyDocuments.text_search(batonically).where(category: :fruit).criteria.inspect
      puts
      puts "Criteria 3"
      puts MyDocuments.where(category: :fruit).where('$text' => { '$search' => batonically }).criteria.inspect
      puts
      puts "Criteria 4"
      puts MyDocuments.where('$text' => { '$search' => batonically }).where(category: :fruit).criteria.inspect
      

      Output

      **Note: The first criteria which is scoped, is missing the category: : fruit criteria

      All others are correct.

      Criteria 1
      #<Mongoid::Criteria
        selector: {:$text=>{:$search=>"batonically"}}
        options:  {}
        class:    MyDocuments
        embedded: false>
      
      Criteria 2
      #<Mongoid::Criteria
        selector: {"$text"=>{"$search"=>"batonically"}, "category"=>:fruit}
        options:  {}
        class:    MyDocuments
        embedded: false>
      
      Criteria 3
      #<Mongoid::Criteria
        selector: {"category"=>:fruit, "$text"=>{"$search"=>"batonically"}}
        options:  {}
        class:    MyDocuments
        embedded: false>
      
      Criteria 4
      #<Mongoid::Criteria
        selector: {"$text"=>{"$search"=>"batonically"}, "category"=>:fruit}
        options:  {}
        class:    MyDocuments
        embedded: false>
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            wilburlo123 Daniel Lo
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: