MongoDB full-text sorting

I am looking for a way to sort results by field value when performing a full-text search with mongodb 2.4.

My text search command looks something like this:

db.books.runCommand( "text", { search: "science" } ) 

What I would like to do is something like:

 db.books.runCommand( "text", { search: "science", "sort": "rating" } ) 

I see in the documentation that there is a limit parameter, but not for sorting results other than default sorting by result.

Re-sorting the results is likely to be ineffective. What is a good way to do this?

+6
source share
2 answers

Had the same problem, managed to solve the problem (pymongo), using the $ meta operator material instead of runCommand:

 # create text index db.collection.ensure_index([("textField", "text")], name = "Text_search_index") # query queryDict = { "$text": { "$search": ""science"}} # cursor cursor = db.collection.find(queryDict, {'score': {'$meta': 'textScore'}, "_id":1}).sort([('score', {'$meta': 'textScore'})]).limit(limit_value) 

It looks like this is only available from version 2.6 , at least in pymongo

Also in the Mongo shell, this is equivalent to:

 db.collection.ensureIndex({"textField":"text"}) queryDict = { "$text": { "$search": "science"}} db.collection.find(queryDict, {'score': {'$meta': 'textScore'}}).sort({'score': {'$met a': 'textScore'}}).limit(100) 
+1
source

It is not possible to sort out of order. You can control the search results with weights , but that is not what you are actually looking for. I suggest you use Sphinx in this case. It goes well with MongoDb and provides truly rich search capabilities.

-1
source

Source: https://habr.com/ru/post/944018/


All Articles