MongoDB - $ text运算符搜索短语OR word(s)

时间:2022-09-13 11:01:48

I'm doing a full text search that requires phrase(s) OR word(s).

我正在进行全文搜索,需要短语或单词。

As per the documentation:

根据文件:

If the $search string includes a phrase and individual terms, text search will only match the documents that include the phrase. More specifically, the search performs a logical AND of the phrase with the individual terms in the search string.

如果$ search字符串包含短语和单个术语,则文本搜索将仅匹配包含该短语的文档。更具体地,搜索执行短语与搜索字符串中的各个词的逻辑AND。

For example, passed a $search string:

例如,传递$ search字符串:

"\"ssl certificate\" authority key"

The $text operator searches for the phrase "ssl certificate" and ("authority" or "key" or "ssl" or "certificate" ).

$ text运算符搜索短语“ssl certificate”和(“authority”或“key”或“ssl”或“c​​ertificate”)。

Is it possible to do this natively via MongoDB? Only other way I see now is to use Elastic search on top.

是否可以通过MongoDB本地执行此操作?我现在看到的另一种方式是在顶部使用弹性搜索。

Update - the following search does not work and will cause a BadValue Too many text expressions error:

更新 - 以下搜索不起作用,将导致BadValue文本表达式错误太多:

{
    $or: [{
        $text: {
            $search: "\"ssl certificate\""
        }
    }, {
        $text: {
            $search: "authority key"
        }
    }]
}

1 个解决方案

#1


The only way to do this natively is by performing multiple queries - one for each phrase and one for all stand-alone words together - and then merge the results on the application layer.

本机执行此操作的唯一方法是执行多个查询 - 每个短语一个查询,一个所有独立单词一起查询 - 然后将结果合并到应用程序层。

When relevance ranking is important for your use-case, have mongodb also give you the text score and make use of it when merging your results.

当相关性排名对您的用例很重要时,mongodb还会为您提供文本分数,并在合并结果时使用它。

#1


The only way to do this natively is by performing multiple queries - one for each phrase and one for all stand-alone words together - and then merge the results on the application layer.

本机执行此操作的唯一方法是执行多个查询 - 每个短语一个查询,一个所有独立单词一起查询 - 然后将结果合并到应用程序层。

When relevance ranking is important for your use-case, have mongodb also give you the text score and make use of it when merging your results.

当相关性排名对您的用例很重要时,mongodb还会为您提供文本分数,并在合并结果时使用它。