teamtnt / laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
MIT License
1.09k stars 144 forks source link

Reload Configuration before search #326

Closed mefenlon closed 1 year ago

mefenlon commented 3 years ago

I have a situation similar to #169 where I need to run a boolean search followed by a regular search. After programmatically changing the config, the settings were not being applied in the second search.

                //.env contains
                //SCOUT_DRIVER=tntsearch
                //SCOUT_QUEUE=true
                //TNTSEARCH_BOOLEAN=true

                $illustrations = Illustration::search($search_string)->get(); //Boolean as expected
                $ids = $illustrations->pluck('id');
                $rel_constraints = new Illustration;
                $rel_constraints = $rel_constraints->whereIn('id',$ids);
                config(['scout.tntsearch.searchBoolean' => false]);
                $rel_illustrations = Illustration::search($search_string)->constrain($rel_constraints)->get(); //Still Boolean

I may be mistaken about this, but it seems $tnt->loadConfig($config); is only run once in TNTSearchScoutServiceProvider boot(), so changing the config elsewhere never gets applied.

EgorGruzdev commented 1 year ago

So it may not reload, but give you the opportunity to specify some settings at the time of the search, as for example it is done at Angloia.

use Algolia\AlgoliaSearch\SearchIndex;
use App\Models\Order;

Order::search(
    'Star Trek',
    function (SearchIndex $algolia, string $query, array $options) {
        $options['body']['query']['bool']['filter']['geo_distance'] = [
            'distance' => '1000km',
            'location' => ['lat' => 36, 'lon' => 111],
        ];

        return $algolia->search($query, $options);
    }
)->get();