简体   繁体   English

带有双引号的pymongo文本搜索问题

[英]text search with pymongo trouble with double quotes

I'm trying to do a text search in a python file with pymongo using the code:我正在尝试使用以下代码在带有 pymongo 的 python 文件中进行文本搜索:

mensajes = db.m
mensajes.find({"$text":{"$search": query}},{'message':1}))
query= "origami \"Pasó algo.\"-pingüino"

And the return is [].并且返回是[]。 But if I use the same query in mongo shell it works perfectly.但是,如果我在 mongo shell 中使用相同的查询,它会完美运行。 If I use:如果我使用:

query= 'origami \'Pasó algo.\'-pingüino'

Instead of the same query with double quotes, I get results from the python files, but they are incorrect.我从 python 文件中得到结果,而不是使用双引号的相同查询,但它们不正确。 I don´t understand why the results are different if the query is the same, I´m just using simple quotes.如果查询相同,我不明白为什么结果会有所不同,我只是使用简单的引号。 (The text search I want to do is find the message that contains Pasó algo, doesn´t contains pingüino, and it can contain origami or not) (我要做的文本搜索是找到包含 Pasó algo 的消息,不包含 pingüino,它可以包含或不包含折纸)

Firstly make sure that you have applied text-index to the key.首先确保您已将text-index应用于密钥。

pymongo gets confused when you use Escape-Sequences in text search.当您在文本搜索中使用Escape-Sequences时,pymongo 会感到困惑。

I would suggest you try out Triple-Quotes string declaration or strictly string declaration.我建议您尝试Triple-Quotes字符串声明或严格的字符串声明。

Strict string declaration:严格的字符串声明:

query= r"origami 'Pasó algo.'-pingüino"

Triple quotes declaration三引号声明

query= """origami 'Pasó algo.'-pingüino"""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM