Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Selected columns and some correction #44

Open
sanjayojha opened this issue Aug 31, 2017 · 2 comments
Open

Selected columns and some correction #44

sanjayojha opened this issue Aug 31, 2017 · 2 comments

Comments

@sanjayojha
Copy link

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

@francoism90
Copy link

francoism90 commented Jul 22, 2019

@sanjayojha This still helps a lot. :)

To create a custom index, use:
DB::statement('ALTER TABLE posts ADD FULLTEXT INDEX posts (title, body)');

@abishekrsrikaanth
Copy link

abishekrsrikaanth commented Dec 9, 2019

@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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants