Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Scout: only search in specific fields

Laravel Scout: Is there a way that I search in only a specific field?

At the moment this line is working fine:

    $es = Element::search($q)->get();

But it searches title, shortdescription and description fields. I need it to only search in title field.

like image 342
user7432810 Avatar asked Sep 01 '17 16:09

user7432810


People also ask

What is scout in Laravel?

Laravel Scout renders a simple, driver-based solution for implementing a full-text search to your Eloquent models. Utilizing model observers, Scout automatically retains your search indexes in sync with your Eloquent records.

How to implement Instant Search in Laravel?

After the app is created, get into the app’s folder: Next, you have to place the database name, username, and password into the .env configuration file. Now, we need to install the laravel scout and algolia search dependencies using the composer tool; these packages are imperative in order to implement instant search work in laravel.

What is Laravel 8 full-text search?

Linux Vs Windows - Which is Better? Laravel 8 scout full-text search tutorial; In a web application, a Full-text search feature is exorbitantly helpful for site users to traverse through content-rich web and mobile applications.

Is the post model searchable in Laravel?

That makes the Post model searchable; Laravel synchronizes post records with the Algolia index every time the post record is added, updated, or deleted. With that, the Post model is search-friendly!


2 Answers

for meilisearch this worked

Element::search('test',
    function ($searchEngine, string $query, array $options) use ($filter) {
        $searchEngine->resetSearchableAttributes();
        $searchEngine->updateSearchableAttributes(['field_name']);
        return $searchEngine->search($query, $options);
    }
)
like image 99
Ahmed Aboud Avatar answered Oct 25 '22 05:10

Ahmed Aboud


You just need to change your toSearchableArray method from your model by adding the following code:

/**
 * Get the indexable data array for the model.
 *
 * @return array
 */
public function toSearchableArray()
{
    $array = $this->only('title', 'description');

    $related = $this->user->only('name', 'email');

    // Customize array...

    return array_merge($array, $related);
}

Then call php artisan scout:import "App\YourModel" to reindex the new records. 

Note:

  • $this->only('title', 'description') will search only for its title and description fields
  • $this->user->only('name', 'email') will also search for its name and email from a related Model

So you can retrieve the related data by adding ->load('user') in your search method like the following code:

public function search(Request $request)
{
    $query = $request->get('q');

    return Task::search($query)->get()->load('user');
}

UPDATE

If you're trying to retrieve the data using ->paginate() method, you must need to load the relations separately:

...
$tasks = Task::search($query)->paginate($request->get('per_page'));

$tasks->load('user');

return $tasks;

Enjoy!

like image 21
Renan Coelho Avatar answered Oct 25 '22 05:10

Renan Coelho