MongoDB - $text 运算符搜索短语或单词 [英] MongoDB - $text operator search for phrase OR word(s)

查看:22
本文介绍了MongoDB - $text 运算符搜索短语或单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行全文搜索,需要词组字词.

根据文档:

<块引用>

如果 $search 字符串包含一个短语和单个术语,文本搜索将只匹配包含该短语的文档.更具体地说,搜索将短语与搜索字符串中的各个术语进行逻辑与运算.

例如,传递一个 $search 字符串:

\"ssl 证书\"权限密钥"

<块引用>

$text 操作符搜索短语ssl certificate";(权限"或密钥"或ssl"或证书").

是否可以通过 MongoDB 本地执行此操作?我现在看到的唯一其他方法是在顶部使用弹性搜索.

更新 - 以下搜索不起作用,将导致 BadValue Too many text expressions 错误:

<代码>{$或:[{$文本:{$search: "\"ssl 证书\""}}, {$文本:{$search:权限密钥"}}]}

解决方案

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

当相关性排名对您的用例很重要时,让 mongodb 也给你文本分数 并在合并你的结果时使用它.

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.

For example, passed a $search string:

"\"ssl certificate\" authority key"

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

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

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

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

解决方案

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 - $text 运算符搜索短语或单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆