全文搜索和分数排序MongoDB

时间:2016-10-16 16:04:23

标签: php mongodb fat-free-framework

我如何在F3中使用这样的答案?

php mongodb full-text search and sort

编辑: 我正在搜索标题和描述。如果没有searchterm,它将对字段created_at进行排序。如果 是一个搜索项,那么:对它进行最相关的排序。

编辑:编辑: 如果重要的话,我也在使用分页插件。

2 个答案:

答案 0 :(得分:3)

使用框架的edge版本,您将能够定义名为score的投影字段并将其用于排序。

E.g:

// MongoDB instance
$db=new DB\Mongo('localhost:27017','mydb');

// Define a `score` field here
$fields=['score'=>['$meta'=>'textScore']];

// Mapper instance
$mapper=new DB\Mongo\Mapper($db,'mycollection',$fields);

// Search text and sort by score
$mapper->load(
  ['$text'=>['$search'=>'search text']],
  ['order'=>['score'=>['$meta'=>'textScore']]]
);

答案 1 :(得分:1)

使用 aggregation framework 通过 $match 管道操作来获取与相关文本得分最相关的文档,该操作将与搜索字词匹配,然后是 $project 运算符管道,用于投影得分字段,然后执行另一个 $order 管道操作以返回最相关的项目。 “textScore” metadata 可用于包含 {{1}的 $match 阶段后的预测,排序和条件} 操作:

$text
相关问题