yabhq / laravel-scout-mysql-driver

Laravel Scout MySQL Driver
MIT License
522 stars 111 forks source link

Cache full text index fields #110

Open thekonz opened 4 years ago

thekonz commented 4 years ago

I saw that this package added a lot of queries to my application and at the part that adds the queries, you can see a comment @TODO cache this..

https://github.com/yabhq/laravel-scout-mysql-driver/blob/b777fcbdd90e5d701ae1d31c1910e9d2e0815f54/src/Services/ModelService.php#L50

Same thing in the getAllFields method.

I suggest adding a command scout:mysql-cache.

Is there anything preventing us from caching? Otherwise I'll gladly add a PR.

thewebartisan7 commented 3 years ago

If I understand this extra sql query check if column support fulltext search. I think this check should not even be required but only use toSearchableArray() Is developer's responsibility to make sure that full text search has been created correctly. Maybe just add an extra toSearchableFulltextArray() which will be only columns that are "full textable"

thewebartisan7 commented 3 years ago

I check into code a bit more and seem that this extra query is executed for select and where, because we can't access toSearchableArray().

A possible workaround could be to add a static method or property into model with array of columns searchable and one fulltextable if different from searchable.

Then in ModelService change method:

    public function getFullTextIndexFields()
    {
        return $this->model::$fulltextable;
    }

    public function getSearchableFields()
    {
        return $this->model::$searchable;
    }

And we could also access directly from builder using $builder->model::$searchable