如何使用 MongoDB 的 C++ 驱动程序执行全文搜索?我觉得我非常接近这里的解决方案,但缺少一些微不足道的东西。
这是我的代码:
void run() {
mongo::DBClientConnection db;
db.connect("localhost");
cout << "count:" << db.count("wiki.categories") << endl;
mongo::BSONObjBuilder oB;
mongo::BSONObj query_1 = mongo::fromjson("{\"$search\": \"success\"}");
mongo::BSONObj criteria_1 = mongo::fromjson("{\"$meta\": \"textScore\"}");
cout << query_1 << endl;
cout << criteria_1 << endl;
oB.append("$text", query_1);
oB.append("score", criteria_1);
BSONObj query = oB.obj();
cout << query << endl;
// mongo::BSONObj query = mongo::fromjson("{'$text' : {'$search': 'success'}}");
auto_ptr<DBClientCursor> cursor = db.query("wiki.categories", mongo::Query(), 0, 0, &query);
//db_wiki.categories.find({ "$text" : { "$search": AND_phrase } },{ "score": { "$meta": "textScore" } })
while (cursor->more())
cout << cursor->next().toString() << endl;
//session->get().runCommand("ide", BSON("text"<<"sems"<<"search"<< value), result);
}
输出:
{ $search: "success" }
{ $meta: "textScore" }
{ $text: { $search: "success" }, score: { $meta: "textScore" } }
(查询与 MongoDB shell 中的查询不完全一样——它应该是两个单独的 JSON 对象)
错误:
{ $err: "Can't canonicalize query: BadValue Unsupported projection option: $text: { $search: "success" }", code: 17287 }
我也试过(同样的错误):
mongo::BSONObj query = mongo::fromjson("{ \"$text\" : { \"$search\": \"success\" } },{ \"score\": { \"$meta\": \"textScore\" } }");
{ $err: "Can't canonicalize query: BadValue Unsupported projection option: 0: { $text: { $search: "success" } }", code: 17287 }
和(同样的错误):
mongo::BSONObj query = mongo::fromjson("{{ \"$text\" : { \"$search\": \"success\" } },{ \"score\": { \"$meta\": \"textScore\" } }}");
{ $err: "Can't canonicalize query: BadValue Unsupported projection option: 0: { $text: { $search: "success" } }", code: 17287 }
这(同样的错误):
mongo::BSONObj query = mongo::fromjson("[{ \"$text\" : { \"$search\": \"success\" } },{ \"score\": { \"$meta\": \"textScore\" } }]");
{ 0: { $text: { $search: "success" } }, 1: { score: { $meta: "textScore" } } }
{ $err: "Can't canonicalize query: BadValue Unsupported projection option: 0: { $text: { $search: "success" } }", code: 17287 }
任何人都可以帮忙吗?我已经没有想法了。