teamtnt / laravel-scout-tntsearch-driver

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

create index with table prefix #239

Closed Codnpix closed 3 months ago

Codnpix commented 5 years ago

Hello, I tried to create the indexes I need for research from my tables. Problem is, all my tables have a prefix, and it doesn't seems to be taken into account : the command ```php artisan tntsearch:import "App\Models\Mymodel" returns an error that my table (whithout prefix) doesn't exist.

However I have the SCOUT_PREFIX=gb_ value entered in my .env

My tntsearch config in scout.php looks like this :

'tntsearch' => [
        'storage'  => storage_path(), //place where the index files will be stored
        'fuzziness' => env('TNTSEARCH_FUZZINESS', false),
        'fuzzy' => [
            'prefix_length' => 2,
            'max_expansions' => 50,
            'distance' => 2
        ],
        'asYouType' => false,
        'searchBoolean' => env('TNTSEARCH_BOOLEAN', false),
    ],

I also tried to add a 'prefix' key like that :

'tntsearch' => [
        'prefix' => 'gb_',
        .....
];

But none of that is working for me. I don't know if I'm doing something wrong or if the driver doesn't provides this feature. I didn't find any explicit documentation about this usecase. Thank you for your help.

nticaric commented 5 years ago

Which version of the driver are you using. The newest version should support table prefixes

Codnpix commented 5 years ago

I think I installed the last version, I just ran composer require teamtnt/laravel-scout-tntsearch-driver (today). My composer.json says : "teamtnt/laravel-scout-tntsearch-driver": "^7.0",

Codnpix commented 5 years ago

Here is a copy of my vendor/teamtnt/laravel-scout-tntsearch-driver/console/ImportCommand.php@handle() function :

public function handle(Dispatcher $events)
    {
        $class = $this->argument('model');

        $model = new $class();
        $tnt = new TNTSearch();        
        $driver = $model->getConnectionName() ?: config('database.default');
        $config = config('scout.tntsearch') + config("database.connections.$driver");

        $tnt->loadConfig($config);
        $tnt->setDatabaseHandle(app('db')->connection($driver)->getPdo());

        $indexer = $tnt->createIndex($model->searchableAs().'.index');
        $indexer->setPrimaryKey($model->getKeyName());

        $availableColumns = \Schema::getColumnListing($model->getTable());
        $desiredColumns = array_keys($model->toSearchableArray());

        $fields = implode(', ', array_intersect($desiredColumns, $availableColumns));

        $query = "{$model->getKeyName()}, $fields";

        if ($fields == '') {
            $query = '*';
        }

        $indexer->query("SELECT $query FROM {$model->getTable()};");

        $indexer->run();
        $this->info('All ['.$class.'] records have been imported.');
    }

I don't see the part that should handle prefix, and yet I see it on the corresponding file in the github repo (branch:master) :

 public function handle(Dispatcher $events)
    {
        $class = $this->argument('model');
        $model = new $class();
        $tnt = new TNTSearch();
        $driver = $model->getConnectionName() ?: config('database.default');
        $config = config('scout.tntsearch') + config("database.connections.$driver");
        $tablePrefix = $config['prefix'] ?? '';
        $tableName = $tablePrefix.$model->getTable();
        $tnt->loadConfig($config);
        $tnt->setDatabaseHandle(app('db')->connection($driver)->getPdo());
        $indexer = $tnt->createIndex($model->searchableAs().'.index');
        $indexer->setPrimaryKey($model->getKeyName());
        $availableColumns = \Schema::getColumnListing($model->getTable());
        $desiredColumns = array_keys($model->toSearchableArray());
        $fields = implode(', ', array_intersect($desiredColumns, $availableColumns));
        $query = "{$model->getKeyName()}, $fields";
        if ($fields == '') {
            $query = '*';
        }
        $indexer->query("SELECT $query FROM {$tableName};");
        $indexer->run();
        $this->info('All ['.$class.'] records have been imported.');
    }

So it looks like the package that I installed is not the same. What should I do ?

EDIT : For now I just copied the functions from your repo that were different from those in my /vendor. It's working. But I'm not sure I should solve the problem like that...