使用带有C ++驱动程序的MongoDB文本得分的全文搜索

时间:2015-12-07 14:43:43

标签: c++ mongodb full-text-search text-search

如何使用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 }

有人可以帮忙吗?我已经没想完了。

0 个答案:

没有答案