WBAPadmin's avatar

Laravel Scout with Meilisearch returns results for some models and not others

0

I am trying to use laravel scout with meilisearch to return results from an index for products. It doesn't work for this model however it does for my user model.

I have gone through both to try and find differences that would be causing this and can't seem to find out what the issue is.

when I run

return \App\Models\Product\Product::search('iphone')->raw();

I get the raw results from the meilisearch index.

however when i run

return \App\Models\Product\Product::search('iphone')->get();

no results show.

Anyone have any idea's on what may be causing this?

0 likes
3 replies
LaryAI's avatar
Level 58

It seems like you're able to retrieve the raw results from Meilisearch when using the raw() method, but not when using the get() method. This could be due to a few reasons. Here are some steps you can take to troubleshoot the issue:

  1. Check Model Configuration: Ensure that the Product model is using the Searchable trait and that you've properly defined the toSearchableArray method if necessary.
use Laravel\Scout\Searchable;

class Product extends Model
{
    use Searchable;

    // If you have customized the searchable data, ensure it's correct
    public function toSearchableArray()
    {
        // Customize array as per your needs
        return $this->toArray();
    }
}
  1. Index Synchronization: Make sure that your Product model's records are properly indexed in Meilisearch. You can re-index your model's data using the following Artisan command:
php artisan scout:import "App\Models\Product\Product"
  1. Searchable Keys: Verify that the fields you are searching on are set as searchable in Meilisearch. If you have customized settings for the index, ensure that the field 'iphone' is included in the searchable attributes.

  2. Scout Configuration: Check your config/scout.php configuration file to ensure that everything is set up correctly for Meilisearch.

  3. Model Identifiers: Ensure that your Product model has a primary key that Scout can use to identify each record. Scout uses the model's getKey method to retrieve this identifier.

  4. Scout Key: If you have overridden the getScoutKey method in your Product model, make sure it returns the correct key that Scout should use.

  5. Debugging: You can add logging to see what query Scout is generating and what results it's getting from Meilisearch. This can help you identify if the issue is with the query or with the retrieval of results.

\Log::info(\App\Models\Product\Product::search('iphone')->toSql());
  1. Meilisearch Logs: Check the Meilisearch logs to see if there are any errors when querying the Product index.

  2. Scout Builder: If you have any custom logic in your Scout query builder for the Product model, ensure that it's not filtering out all the results.

  3. Eloquent Relationships: If you're using any Eloquent relationships to filter the results, make sure they are correctly defined and not causing the issue.

If after checking all these points you still face the issue, it might be helpful to post more details about your Product model, any custom Scout configuration, and the version of Laravel Scout and Meilisearch you are using, so that the community can provide more targeted assistance.

WBAPadmin's avatar

more information: meilisearch/meilisearch-php : 1.4.1 laravel/scout: 10.5.1

when using getKey and getScoutKey the correct _id key name is returned (it is the same on the User model where search is working

there are no filtered out results or related models

Product Model

    protected $primaryKey = self::ID;

    public function toSearchableArray()
    {
        $searchable                         = [];
        $searchable[self::ID]               = $this->getAttribute(self::ID);
        $searchable[self::NAME]             = $this->getAttribute(self::NAME);
        $searchable[self::SLUG]             = $this->getAttribute(self::SLUG);
        $searchable[self::MODEL_NAME]       = $this->getAttribute(self::MODEL_NAME);
        $searchable[self::MODEL_SUB]        = $this->getAttribute(self::MODEL_SUB);
        $searchable[self::MODEL_GENERATION] = $this->getAttribute(self::MODEL_GENERATION);
        $searchable[self::MODEL_SIZE]       = $this->getAttribute(self::MODEL_SIZE);
        $searchable[self::MODEL_EXTRA]      = $this->getAttribute(self::MODEL_EXTRA);
        $searchable[self::BRAND_NAME]       = ($this->brand) ? $this->brand->name : 'unknown';
        $searchable[self::DESCRIPTION]      = $this->getAttribute(self::DESCRIPTION);

        return $searchable;
    }
WBAPadmin's avatar
WBAPadmin
OP
Best Answer
Level 2

fixed (at least got it working) by downgrading scout to -> ^9.4 meilisearch/meilisearch-php -> ^0.24.2

Please or to participate in this conversation.