MongoDB - $text 运算符搜索短语或单词 [英] MongoDB - $text operator search for phrase OR word(s)
问题描述
我正在进行全文搜索,需要词组或字词.
根据文档:
<块引用>如果 $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屋!