Laravel Scout (Meilisearch) - Says imported data, but doesn't
Asked Answered
R

4

5

I've installed and configured meilisearch + Laravel Scout package.

My Model:

class Post extends Model
{
    use Searchable;
}

When I run php artisan scout:import 'App\Models\Post' it returns:

Imported [App\Models\Post] models up to ID: 5
All [App\Models\Post] records have been imported.

But when I check the index, it's empty. Why?

The index is being created, but the data doesn't get imported.

The same configuration of meilisearch and Scout package, works for some other models.

Retortion answered 1/8, 2021 at 23:41 Comment(1)
Hi! What does the update status return? docs.meilisearch.com/reference/api/…Atterbury
I
9

I've just run into this issue myself and came across your question. I don't suppose you're specifying what should be stored in the index are you?

I.e. in your model, have you created a toSearchableArray method like the below...

public function toSearchableArray(): array
{
    return [
        'name' => $this->name,
    ];
}

If you have, it turns out that your toSearchableArray method must also return the primary key within the array, otherwise the record does not get indexed.

public function toSearchableArray(): array
{
    return [
        'id'   => $this->getKey(), // this *must* be defined
        'name' => $this->name,
    ];
}
Ijssel answered 27/8, 2021 at 12:35 Comment(3)
Thanks this fixed my issues :)Berzelius
For this particular model I was hiding the primary key, and removing it was spot on. Thanks!Marquisette
That solved my issue as well! without the id, it doesn't get indexed, it's a meilisearch specific thing tho as it is not explicit on laravel's scout doc.Excuse
P
6

You can try to set:

SCOUT_QUEUE=false

To check that there is no issue with your queues and run the import again.

Pisgah answered 4/8, 2021 at 8:27 Comment(1)
I had tried it before, and it didn't work.Retortion
C
4

For index you can try:

php artisan scout:index posts

There is no other issue with your queues and run the import again.

If you have SCOUT_QUEUE=true then please, start your queue using php artisan queue:work --daemon and your data will be start importing.

Candycecandystriped answered 27/8, 2021 at 14:39 Comment(0)
C
0

Make sure you dont have a scope in your model or comment it out: php artisan scout import

doesnt have a session so if your scope is something like user_id == auth()->id will not work.

Cool answered 28/2 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.