全文搜索和分数排序 MongoDB
Full text search and score sorting MongoDB
我该如何在 F3 中使用这样的答案?
php mongodb full-text search and sort
编辑:
我正在搜索标题和描述。如果没有搜索项,它将在字段 created_at
上排序。但是,如果 是 搜索词:按最相关的排序。
编辑:编辑:
如果重要的话,我也在使用分页插件。
使用 aggregation framework to get the documents that have most relevant text score by doing a $match
pipeline operation which will match the search term, followed by a $project
operator pipeline which projects the score fields, and then do another $order
pipeline operation to return most relevant item. The "textScore" metadata 可用于 $match
阶段之后的预测、排序和条件包括 $text
操作:
db.collection.aggregate([
{
"$match": {
"$text": {
"$search": "search text"
}
}
},
{
"$project": {
"_id": 0,
"score": {
"$meta": "textScore"
}
}
},
{
"$orderby": {
"score" : -1
}
}
])
使用框架的 edge 版本,您将能够定义一个名为 score
的投影字段并将其用于排序。
例如:
// 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']]]
);
我该如何在 F3 中使用这样的答案?
php mongodb full-text search and sort
编辑:
我正在搜索标题和描述。如果没有搜索项,它将在字段 created_at
上排序。但是,如果 是 搜索词:按最相关的排序。
编辑:编辑: 如果重要的话,我也在使用分页插件。
使用 aggregation framework to get the documents that have most relevant text score by doing a $match
pipeline operation which will match the search term, followed by a $project
operator pipeline which projects the score fields, and then do another $order
pipeline operation to return most relevant item. The "textScore" metadata 可用于 $match
阶段之后的预测、排序和条件包括 $text
操作:
db.collection.aggregate([
{
"$match": {
"$text": {
"$search": "search text"
}
}
},
{
"$project": {
"_id": 0,
"score": {
"$meta": "textScore"
}
}
},
{
"$orderby": {
"score" : -1
}
}
])
使用框架的 edge 版本,您将能够定义一个名为 score
的投影字段并将其用于排序。
例如:
// 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']]]
);