正如@Henri 指出的那样,自从提交[enhance] DbGen: add case insensitive regex operator =~ 以来,Opa 中就有正则表达式搜索支持,这非常好。
请注意,它使用的是 $regex 运算符,而不是全文索引,它可能会导致一些性能损失 :( 由于 MongoDB documentation says $regex 运算符以有限的方式使用索引 - 仅用于前缀搜索:模式 @987654327 @. 在文本中的任何位置搜索Jean 都需要完整扫描。
就个人而言,我正在使用 Mongo 的 full-text index 功能和 Opa 的“低级”API 来执行 $text 命令,如下所示:
function list({float score, Article.id id}) textSearch(string query) {
function onfailure(failure) {
cat.error("textSearch({{~query}}): {failure}");
[];
}
function onsuccess(success) {
function aux(~{name,value}) {
name == "results";
}
match (List.filter(aux, success)) {
| [] :
// `results` field not found - error
onfailure(success);
| results:
cat.debug("textSearch({~{query}}): {results}");
function ({~score, obj: ~{id}}) {
~{score, id}
}
|> List.map(_, Bson.doc2opa(results) ? []);
}
}
opts = [H.str("search", query), H.doc("project", [H.i32("_id",0), H.i32("id",1)])];
// { search: query, project: {_id:0, id:1}, }
// |> Bson.opa2doc
outcome = MongoCommands.simple_str_command_opts(ll_db, db_name, "text", coll_name, opts);
MongoCommon.outcome_map(outcome, onsuccess, onfailure)
}
从 2.4 开始,Mongo 中的功能是实验性的(您必须通过特殊的配置选项将其打开),在 2.6 中作为稳定版(默认打开)。