如何使用python在mongodb中启用textsearch?

2024-05-20 14:10:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在下面的脚本中编写了textSearchedEnabled=true,但是出现了语法错误。我不明白如何使用python在mongodb中启用文本搜索。在

 import json
 import pymongo # pip install pymongo
 from bson import json_util # Comes with pymongo
 from pymongo import MongoClient
 --setParameter textSearchEnabled=true
 client = pymongo.MongoClient('mongodb://user:user123@ds033499.mongolab.com:33499/enron')
 db = client.enron
 mbox = db.mbox

 # Create an index if it doesn't already exist
 mbox.ensure_index([("$**", "text")], name="TextIndex")

 # Get the collection stats (collstats) on a collection
 # named "mbox"
 print json.dumps(db.command("collstats", "mbox"), indent=1)

 # Use the db.command method to issue a "text" command
 # on collection "mbox" with parameters, remembering that
 # we need to use json_util to handle serialization of our JSON
 print json.dumps(db.command("text", "mbox",  
                        search="raptor", 
                        limit=1), 
             indent=1, default=json_util.default)

Tags: totextfromimportjsontruedbmongodb