uch*_*cha 0 php full-text-search projection mongodb
我正在从旧的 PHP MongoDB 驱动程序升级我的代码: http //php.net/manual/en/class.mongoclient.php
到新的 MongoDB 驱动程序: http //php.net/manual/en/set.mongodb.php
在以前的版本中,我有这个:
$db->collection->find([
'$text' => [ '$search' => "stackoverflow" ]
],
[
'score' => [ '$meta' => 'textScore' ]
])->sort([ 'sort' => [ 'score' => [ '$meta' => 'textScore' ] ] ]);
Run Code Online (Sandbox Code Playgroud)
在新版本中,游标上不再有排序功能,您必须将其作为选项传递。所以新代码是这样的:
$db->collection->find([
'$text' => [ '$search' => 'stackoverflow' ]
],
[
'score' => [ '$meta' => 'textScore' ],
'sort' => [ 'score' => [ '$meta' => 'textScore' ] ]
]);
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:“所有 $meta 排序键的 BadValue 都必须有 $meta 投影”
这是因为,分数预测不再发生。如果您只是删除排序选项并记录结果,您将看到结果数组中没有分数。根本没有关于它的文档。
有谁知道如何解决这个问题?
谢谢
如果要使用新的 PHP 驱动程序搜索 Mongo 文本索引并在 textScore 字段上排序,则必须使用 Query 类,在那里添加过滤器和选项,然后使用 Manager 类执行。在你的情况下,它会是这样的:
$filter = [
'$text' => ['$search' => 'stackoverflow']];
$options = [
'projection' => [
'score' => ['$meta' => 'textScore']
],
'sort' => [
'score' => ['$meta' => 'textScore']
]
];
$mng = new MongoDB\Driver\Manager("mongodb://yourdbserver:27017");
$mongoQuery = new MongoDB\Driver\Query($filter, $options);
$cursor = $mng->executeQuery('db_name.collection_name', $mongoQuery);
Run Code Online (Sandbox Code Playgroud)
请参阅Query 类的文档页面,第一个注释似乎非常有用。
| 归档时间: |
|
| 查看次数: |
1232 次 |
| 最近记录: |