我已经为db创建了一个索引。
res = yield dbEmail["emails"].create_index([("subject", pymongo.TEXT)])
现在我正试图通过
搜索创建的索引dbEmail["emails"].find({$text: {$search: "dogs"}}, {score: {$meta: "textScore"}}).sort({score: {$meta: "textScore"}})
我找到的不起作用。电机文档说db.collection.find(filter)中的filter参数是一个SON对象。当我在我的集合上尝试list_indexes()时,它们也是SON对象。但是接下来
for index in (yield dbEmail["emails"].list_indexes()):
searchResults = yield dbEmail["emails"].find(index)
也会导致错误。我们如何用电机对索引进行文本搜索?
编辑:回复Niel
我的初始数据库集合如下所示:
emails = {"_id" : ObjectId("xxcxcdsfas"),"subject": "Joe owns a dog", "content": "Dogs are man's best friend", "likes": 60, "year": 2015,"language": "english"},
{"_id" : ObjectId("xxcxcdsfas"),"subject": "Dogs eat cats and dog eats pigeons too", "content": "Cats are not evil", "likes": 30, "year": 2015,"language": "english"},
{"_id" : ObjectId("xxcxcdsfas"),"subject": "Cats eat rats", "content": "Rats do not cook food", "likes": 55, "year": 2014, "language": "english"},
{"_id" : ObjectId("xxcxcdsfas"), "subject": "Rats eat Joe", "content": "Joe ate a rat", "likes": 75, "year": 2014, "language": "english"}
在创建索引(正在创建,我已经验证)之后,我需要的最终结果应该是这样的,
{ "_id" : ObjectId("xxcxcdsfas"), "subject" : "Dogs eat cats and dog eats pigeons too", "content" : "Cats are not evil", "likes" : 30, "year" : 2015, "language" : "english", "score" : 1 }
{ "_id" : ObjectId("xxcxcdsfas"), "subject" : "Joe owns a dog", "content" : "Dogs are man's best friend", "likes" : 60, "year" : 2015, "language" : "english", "score" : 0.6666666666666666 }
第一个得分较高,因为狗在受试者中出现两次,而在第二次中则出现一次。
我使用以下代码检查索引是否有效:
for item in (yield dbEmail["emails"].index_information()).items():
print(item)
回复或索引如下:
('_id_', {'v': 2, 'key': [('_id', 1)], 'ns': 'emailInfo.emails'})
('subject_text', {'v': 2, 'key': [('_fts', 'text'), ('_ftsx', 1)], 'ns': 'emailInfo.emails', 'weights': SON([('subject', 1)]), 'default_language': 'english', 'language_override': 'language', 'textIndexVersion': 3})
答案 0 :(得分:0)
最后解决了,命令“db.collection.command()”使它工作,
searchResults = yield dbEmail.command({"find": "emails",
"filter": {"$text": { "$search": "dogs"}}})