-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 7.0.2, 6.4.2
-
Component/s: Query
-
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>
- is related to
-
MONGOID-4698 Warn about attempts to define text_search scope (and others?)
- Closed