Selected columns and some correction
sanjayojha opened this issue · 2 comments
I wanted to use this package so that I can use the benefit of Laravel Scout
with MySql FULLTEXT. Instead of using raw full text query I used this package so that in future I can easily change the driver and if needed can use more efficient Search driver like elasticsearch
, TNTSearch
or algolia
.
I found many issue while installing and implementing it. But in the end I was able to install it successfully. For someone who may need this-
First, DamianTW\MySQLScout\Providers\MySQLScoutServiceProvider::class,
did not work for me it was showing error on running command. So I changes it to
Yab\MySQLScout\Providers\MySQLScoutServiceProvider::class,
The command php artisan scout:mysql-index App\\Post
didn't work for me. I changed it to
php artisan scout:mysql-index App\Post
. May be I am using windows ?
Now to allow only few selected table columns you should override toSearchableArray()
method in your model before running above command.
public function toSearchableArray()
{
// Customize array...
return [
'title' => $this->title,
'body' => $this->body,
];
}
Make sure your column is of type text
or varchar
To name the index name override searchableAs()
method in your model before running above command
public function searchableAs()
{
return 'search_index';
}
Note if you already have FULLTEXT
defined in your table then you can match the above value in searchableAs()
and toSearchableArray()
methods by providing exact same values.
Hope this help someone.
Let me know if it can be improved further or I need to make any correction
@sanjayojha This still helps a lot. :)
To create a custom index, use:
DB::statement('ALTER TABLE posts ADD FULLTEXT INDEX posts (title, body)');
@sanjayojha, @damiantw, I did configure the toSearchableArray()
with the specific fields I want to be searchable. Is there any reason why the package still needs to make queries to information_schema
and SHOW FIELDS
before running the search question on the model when the searchable columns are set?