0

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

任何人都可以帮忙吗?我已经没有想法了。

4

1 回答 1

0

由于没有人看过这个,而且似乎没有人使用 mongodb 的 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;
  }
}
于 2015-12-21T13:22:32.210 回答