Laravel-MongoDB,如何通过相关的订单结果

时间:2016-05-13 13:35:18

标签: mongodb laravel

我使用jenssegers / laravel-mongodb。我制作范围

public function scopeWhereFullText($query, $search)
    {
        return $query->whereRaw(['$text' => ['$search' => $search]],['score'=>['$meta'=>'textScore']]);
    }

如何按照MongoDB js示例中的分数字段进行排序:

db.products.find({$text:{$search:"SomeText"}},{score:{$meta:'textScore'}}).sort({score:{$meta:'textScore'}})

2 个答案:

答案 0 :(得分:0)

没有拐杖的解决方案是什么:

public function scopeWhereFullText($query, $search)
{
    $query->getQuery()->projections = ['score'=>['$meta'=>'textScore']];

    return $query->whereRaw(['$text' => ['$search' => $search]]);

}

和结果

$products = Product::whereFullText($request->get('q',''))
                    ->orderBy('score',['$meta'=>'textScore'])->get();

$max = $products->max('score');
$min = $products->min('score');

$products = $products->filter(function($item) use($max,$min){
    return $item->score > ($max+$min)/2;
});

答案 1 :(得分:0)

使用

$results = DB::connection()->collection('_User')->whereRaw(['$text' => ['$search' => 'SEARCH QUERY']])->project(['score'=>['$meta'=>'textScore']])->orderBy('score', ['$meta' => "textScore"])->limit(10)->get();