laravel / scout

Laravel Scout provides a driver based solution to searching your Eloquent models.
https://laravel.com/docs/scout
MIT License
1.54k stars 327 forks source link

Database Engine: Support for doing compound full text search #833

Closed MorganGonzales closed 3 months ago

MorganGonzales commented 3 months ago

Laravel: v10 Database: MySQL 8.0.33 Laravel Scout: v10

I do have a table that has a compound full-text index.

Schema::table('profiles', function (Blueprint $table) {
    $table->id();
    $table->string('first_name);
    $table->string('last_name);
    $table->string('email')->unique();

    $table->fullText(['first_name', 'last_name', 'email']);
});

And below is how the Profile model looks like

class Profile extends Model
{
    use Searchable;

    //....

    #[SearchUsingFullText(['first_name', 'last_name', 'email'])]
    public function toSearchableArray(): array
    {
        return [
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'email' => $this->email,
        ];
    }   
}

When executing this code

$searchResult = Profile::search('something')->get();

I see that behind the scenes, it executes a query like

SELECT *
FROM `profiles`
WHERE (match(`profiles`.`first_name`) against ("%something%" IN NATURAL LANGUAGE mode)
    OR match(`profiles`.`last_name`) against ("%something%" IN NATURAL LANGUAGE mode)
    OR match(`profiles`.`email`) against ("%something%" IN NATURAL LANGUAGE mode)
);

Instead of how I want the query to look like (to utilize the full-text index that I created)

SELECT *
FROM profiles
WHERE MATCH(first_name, last_name, email) AGAINST("%something%" IN NATURAL LANGUAGE MODE);

I'm aware the my desired query statement could be easily done by whereFullText, but I wish there's a way for Laravel Scout to also do this.

driesvints commented 3 months ago

Heya. Not sure we'll get this into Scout when there's an easy way to do this using the query builder sorry.

I'm actually wondering what the big difference is between your query and the one in scout? Is the latter a statement for "AND" between the columns?