全文搜索和评分排序MongoDB



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

php mongodb全文搜索和排序

编辑:我正在搜索一个标题和描述。如果没有搜索项,它将在字段created_at上排序。但是,如果,则按最相关的排序。

编辑:编辑:我也使用分页插件,如果这很重要。

使用框架的边缘版本,您将能够定义一个名为score的投影字段,并使用它进行排序。

例句:

// MongoDB instance
$db=new DBMongo('localhost:27017','mydb');
// Define a `score` field here
$fields=['score'=>['$meta'=>'textScore']];
// Mapper instance
$mapper=new DBMongoMapper($db,'mycollection',$fields);
// Search text and sort by score
$mapper->load(
  ['$text'=>['$search'=>'search text']],
  ['order'=>['score'=>['$meta'=>'textScore']]]
);

使用聚合框架通过执行 $match 管道操作来获得具有最相关文本分数的文档,该操作将匹配搜索词,然后是 $project 操作符管道,该管道将项目分数字段,然后执行另一个 $order 管道操作返回最相关的项目。"textScore" 元数据可用于 $match 阶段(包括 $text 操作)之后的预测、排序和条件:

db.collection.aggregate([
    { 
        "$match": { 
               "$text": { 
                     "$search": "search text" 
                } 
         } 
    },
    { 
         "$project": { 
               "_id": 0, 
               "score": { 
                     "$meta": "textScore" 
                } 
          } 
     },
     { 
          "$orderby": { 
               "score" : -1 
               }
     }
])

最新更新