【问题标题】:How can I query several words in indexed fields in pymongo?如何在 pymongo 的索引字段中查询多个单词?
【发布时间】:2016-02-06 05:04:45
【问题描述】:

当我想执行索引文本搜索时,我使用以下命令:

text_results = db.command('text', 'foo', search=query)

我现在想知道如何查询几个单词。我已经尝试将查询设置为query = ['word1', 'word2'],但这不起作用。

【问题讨论】:

    标签: mongodb indexing pymongo text-search multi-query


    【解决方案1】:

    使用搜索字符串"word1 word2" 搜索术语word1 或术语word2

    text_results = db.command('text', 'foo', search='word1 word2')
    

    另外,这里引用 docs 的一句话:

    如果搜索字符串包含短语,则搜索执行 AND 与 搜索字符串中的任何其他术语;例如搜索“\”闪烁 twinkle\" little star" 搜索 "twinkle twinkle" 和 ("little" or “星”)。

    所以要搜索该字段包含"word1" AND "word2" 的位置,请选择

    text_results = db.command('text', 'foo', search="\"word1\" \"word2\"")
    

    【讨论】:

    • 我正在寻找一个查询,该查询将返回字段包含“word1”和“word2”的结果
    • 所以查询看起来像这样打印:"\word1\ word2\"?
    猜你喜欢
    • 2015-08-09
    • 1970-01-01
    • 2012-03-12
    • 2020-10-11
    • 2015-11-02
    • 1970-01-01
    • 2019-05-22
    • 2015-08-20
    • 1970-01-01
    相关资源
    最近更新 更多