laravel-scout-mysql-driver icon indicating copy to clipboard operation
laravel-scout-mysql-driver copied to clipboard

shouldBeSearchable not working?

Open Jafo232 opened this issue 5 years ago • 5 comments

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.

Jafo232 avatar Apr 23 '19 13:04 Jafo232

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 :/

iaK avatar Sep 21 '19 16:09 iaK

Having the same issue...

nanadjei avatar Oct 07 '19 15:10 nanadjei

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

nanadjei avatar Oct 09 '19 09:10 nanadjei

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

freshleafmedia avatar Oct 21 '20 13:10 freshleafmedia

@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()

rabol avatar Feb 19 '21 13:02 rabol