yabhq / laravel-scout-mysql-driver

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

shouldBeSearchable not working? #84

Open Jafo232 opened 5 years ago

Jafo232 commented 5 years ago

It seems that no matter what condition I set in shouldBeSearchable(), it doesn't matter. Example:

    public function shouldBeSearchable()
    {
        return $this->approved === 1;
    }

Even when approved is 0 for an item, it is still returned.

iaK commented 5 years ago

Yeah, can confirm it dosen't work. Kinda hard to implement too.

You can't create the fulltext index on only certain rows, all of it has go in there.

The problem is you can't really filter them away after getting them from the db either, that causes trouble if you want to filter och limit the results :/

nanadjei commented 5 years ago

Having the same issue...

nanadjei commented 5 years ago

I actually fixed the issue by setting the is_published column in my database to boolean instead of a string.

freshleafmedia commented 4 years ago

For those who need a workaround you can override the search method and manually tack on where clauses to the Builder instance:

class MyModel extends Model
{
    use Searchable {
        search as traitSearch;
    }

    public static function search($query = '', $callback = null)
    {
        // Work around for https://github.com/yabhq/laravel-scout-mysql-driver/issues/84
        return self::traitSearch($query, $callback)->where('attribute', 0);
    }
}

Be aware of the limitations: https://laravel.com/docs/master/scout#where-clauses

rabol commented 3 years ago

@freshleafmedia the workaround works fine as long as you don't want to do do something like whereNotNull

I got around it by doing this:

class MyModel extends Model
{
    use Searchable {
        search as traitSearch;
    }

    public static function search($query = '', $callback = null)
    {
        // Work around for https://github.com/yabhq/laravel-scout-mysql-driver/issues/84
        return self::traitSearch($query, '\App\Models\MyModel::callBack');
    }

    public static function callBack($query, $engine)
    {
        return $query->whereNotNull('field_name');
    }
}

another option is to simply add a callback when you do the ::search()

$result = MyModel::search('some text', '\App\Models\MyModel::callback')->get()