【问题标题】:Mongodb text search not working with string FlaskMongodb文本搜索不适用于字符串Flask
【发布时间】:2018-12-05 11:22:13
【问题描述】:

我正在尝试使用 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"})])

有效。

【问题讨论】:

    标签: python-3.x mongodb flask


    【解决方案1】:

    变量newText 的内容与硬编码字符串中的内容不同。

    尝试在创建newText的过程中去掉'r',生成一个类似于硬编码字符串的字符串,如下:

    newText= ' '.join('\"'+word+'\"' for word in str.split())
    

    【讨论】:

      猜你喜欢
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 2022-10-22
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      相关资源
      最近更新 更多