Using this command directly on the database I get the results I am after
db.products.find({$text: {$search: "some product"}}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})
But using it in MongoMapper I am getting errors. The search without the score field doesn't produce any errors
@products = Product.where(
'$text' => {'$search' => @search_string}
)
However, when I try to add the sort field is where I get issues
@products = Product.where(
'$text' => {'$search' => @search_string}, :score => {'$meta' => "textScore"}
)
Mongo::OperationFailure at /search unknown operator: $meta
It also fails using the raw query method
@products = MongoMapper.database['products'].find(
'$text' => {'$search' => @search_string}, :score => {'$meta' => "textScore"}
)
The full query I am trying to run is
@products = Product.where(
'$text' => {'$search' => @search_string},:score => {'$meta' => "textScore"}
).sort(:score => {'$meta' => "textScore"}).limit(5)
This gives the error
Mongo::OperationFailure at /search must have $meta projection for all $meta sort keys
Does anyone have suggestions of where I am going wrong? I assume I am using it wrong.
Installed version. mongo_mapper (0.14.0, 0.13.1)
A user on the MongoMapper Google group has pointed me in the right direction
@products = Product.where(
'$text' => {'$search' => @search_string}).fields(:score => {'$meta' => "textScore"})