Sorting with MongoDB full text search
Had the same problem, managed to solve the problem (pymongo) by using instead of the runCommand stuff the '$meta' operator:
# create text indexdb.collection.ensure_index([("textField", "text")], name = "Text_search_index") # query queryDict = { "$text": { "$search": ""science"}} # cursorcursor = db.collection.find(queryDict, {'score': {'$meta': 'textScore'}, "_id":1}).sort([('score', {'$meta': 'textScore'})]).limit(limit_value)
Looks like this is available only from 2.6 version at least in pymongo
Also in Mongo shell this equivalent to :
db.collection.ensureIndex({"textField":"text"}) queryDict = { "$text": { "$search": "science"}} db.collection.find(queryDict, {'score': {'$meta': 'textScore'}}).sort({'score': {'$meta': 'textScore'}}).limit(100)
Try the following, works with MongoDB 4.4:
collection.find({ $text: { $search: filter, $caseSensitive: false, $diacriticSensitive: true } }) .project({ score: { $meta: "textScore" } }) .sort({score:{$meta:"textScore"}})