How to combine fulltext search with other optinals matches in MongoDb?

  aggregate, full-text-search, laravel, mongodb, php

I try to query my collection with only one query and 3 potentials search method : fulltext search, classic search and search regex. This 3 matches can be executed at the same time or just one of them.
The fulltext search is the first stage pipeline as we know. Does this fulltext search can be optional in my aggregate ? Because if my default value of search is "", my query returns any data. And i need datas to perform my other optionals matches.
Here is my Laravel 8 controller :

Product::raw(function ($collection) use($filters, $fullText, $likeKey, $likeValue){
                                    return $collection->aggregate([
                                        [        
                                            '$match' =>
                                            [
                                                '$text' =>['$search' => $fullText],
                                                                        
                                            ],
                                            
                                        ],
                                        [
                                            '$match' => $filters
                                        ],
                                        [   
                                           '$match' => 
                                                [
                                                    $likeKey => 
                                                        [
                                                            '$regex' => $likeValue,
                                                            '$options' => "i"
                                                        ]
                                                ]
                                        ],
                                      
                                        [
                                            '$addFields' => 
                                                [
                                                    'avgReviews' => ['$avg' => '$reviews.ranking'],
                                                    'price' => ['$min' => '$variants.price'],
                                                    'equipmentsList' =>  [
                                                        '$reduce' => [
                                                            'input' => '$equipments.list.list',
                                                            'initialValue' => [],
                                                            'in' => 
                                                                [
                                                                    '$concatArrays' => [
                                                                        '$$value',
                                                                        '$$this'
                                                                    ]
                                                                ]           
                                                        ]
                                                    ]
                                                ],
                                        ]                           
                                    ]);
                                })
                    ->when($operations, function($products) use ($operations){
                        foreach($operations as $key => $operation){                            
                            return $products
                                ->where($operation[0],$operation[1],$operation[2]);
                        }
                    })
                    ->forPage($page,$limit)
                    ->sortBy($sortBy, SORT_REGULAR, $order == 'desc')
                    ->values();

$filters is an array and works fine when it s the only one match. But if i want to use $filters without $text, it returns any data. And with the third match, nothing works. Somebody for helping me please ?

Source: Laravel

Leave a Reply