全文搜索,文本分数为C++;MongoDB的驱动程序 如何用C++驱动程序执行MunGDB全文搜索?我觉得我离解决方案很近了,但我遗漏了一些琐碎的东西
这是我的密码:全文搜索,文本分数为C++;MongoDB的驱动程序 如何用C++驱动程序执行MunGDB全文搜索?我觉得我离解决方案很近了,但我遗漏了一些琐碎的东西,c++,mongodb,full-text-search,text-search,C++,Mongodb,Full Text Search,Text Search,这是我的密码: 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\": \"su
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);
}
(该查询与MongoDB shell中的查询不完全相同-它应该是两个独立的JSON对象)
错误:
{ $err: "Can't canonicalize query: BadValue Unsupported projection option: $text: { $search: "success" }", code: 17287 }
我也尝试过(同样的错误):
和(同样的错误):
还有这个(同样的错误):
有人能帮忙吗?我已经没有了主意。因为没有人看这个,而且似乎没有人甚至用MunGDB的C++驱动程序,我必须自己去弄明白。 我很高兴我尝试使用C++驱动程序,因为它比PyMongo快。 以下脚本查询input_file.txt中的短语,大小为文件中短语的数量 要执行带评分的文本查询,可以执行以下操作:
void run() {
// input file
ifstream phrases_file("input_file.txt");
// read input file
int size;
phrases_file >> size;
string phrases[size];
if(phrases_file.is_open()){
for(int i = 0; i < size; ++i){
getline(phrases_file, phrases[i]);
trim_right(phrases[i]);
}
}
// connect to the db
mongo::DBClientConnection c;
c.connect("localhost");
// score query
mongo::BSONObj sort_score = mongo::fromjson("{ \"score\": {\"$meta\": \"textScore\"}}");
// sequentially build queries
for(int i = 0; i < size; ++i){
mongo::BSONObjBuilder oB;
stringstream json_text_query_stream;
json_text_query_stream << "{ \"$text\" : {\"$search\": " << "\"" << phrases[i] << "\"" << "}}";
string json_text_query = json_text_query_stream.str();
mongo::BSONObj text_query = mongo::fromjson(json_text_query);
std::auto_ptr<mongo::DBClientCursor> cursor = c.query("wiki.categories", mongo::Query(text_query), 0, 0, &sort_score);
cout << phrases[i] << endl;
while (cursor->more())
std::cout << cursor->next().toString() << std::endl;
std::cout << '\t' << std::endl;
}
}
void run(){
//输入文件
IFU文件(“input_file.txt”);
//读取输入文件
整数大小;
短语\u文件>>大小;
字符串短语[大小];
如果(短语\u file.is\u open()){
对于(int i=0;imongo::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 }
void run() {
// input file
ifstream phrases_file("input_file.txt");
// read input file
int size;
phrases_file >> size;
string phrases[size];
if(phrases_file.is_open()){
for(int i = 0; i < size; ++i){
getline(phrases_file, phrases[i]);
trim_right(phrases[i]);
}
}
// connect to the db
mongo::DBClientConnection c;
c.connect("localhost");
// score query
mongo::BSONObj sort_score = mongo::fromjson("{ \"score\": {\"$meta\": \"textScore\"}}");
// sequentially build queries
for(int i = 0; i < size; ++i){
mongo::BSONObjBuilder oB;
stringstream json_text_query_stream;
json_text_query_stream << "{ \"$text\" : {\"$search\": " << "\"" << phrases[i] << "\"" << "}}";
string json_text_query = json_text_query_stream.str();
mongo::BSONObj text_query = mongo::fromjson(json_text_query);
std::auto_ptr<mongo::DBClientCursor> cursor = c.query("wiki.categories", mongo::Query(text_query), 0, 0, &sort_score);
cout << phrases[i] << endl;
while (cursor->more())
std::cout << cursor->next().toString() << std::endl;
std::cout << '\t' << std::endl;
}
}