我正在尝试使用 Flask 进行文本搜索。
对于一个词它有效,但是当我传递一个包含多个词的字符串时它不起作用。
但是当我以硬编码方式传递该字符串时,它起作用了:
假设字符串是这样的:
str = "SOME TEXT HERE"
如果我像这样将它作为变量传递:
newText= ' '.join(r'\"'+word+r'\"' for word in str.split())
result = app.data.driver.db[endpoint].find({"$text":{"$search":newText }}, {"score": {"$meta":"textScore"}}).sort([("score", {"$meta": "textScore"})])
它不起作用。
但是如果我像这样硬编码传递它:
result = app.data.driver.db[endpoint].find({"$text":{"$search":" \"SOME\" \"TEXT\" \"HERE\" " }}, {"score": {"$meta":"textScore"}}).sort([("score", {"$meta": "textScore"})])
有效。
最佳答案
变量 newText
的内容与硬编码字符串中的内容不同。
尝试在创建 newText
期间删除 'r' 以生成类似于硬编码字符串的字符串,如下所示:
newText= ' '.join('\"'+word+'\"' for word in str.split())
关于python-3.x - Mongodb 文本搜索不适用于字符串 Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53631164/