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

时间:2014-03-04 04:48:30

标签: python mongodb ipython-notebook

我在以下脚本中编写了textSearchedEnabled=true,但发生了语法错误。我无法理解如何使用python在mongo db中启用文本搜索。

 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)

1 个答案:

答案 0 :(得分:1)

通常通过mongodb.conf或启动mongodb启用textSearch。请参阅:Enable Text Search文档。

如果用户具有管理员权限,您也可以通过admin命令运行它:

client.admin.command('setParameter', textSearchEnabled=True)