Mattiman's avatar

Typesense\Exceptions\RequestMalformed: Parameter `fields` is required

Have installed Typesets successfully and imported all existing models in an existing database, using scout:import. Typesets searching works.

However, when I migrate:fresh and try to import models, I get the error 'Typesense\Exceptions\RequestMalformed: Parameter fields is required'

0 likes
3 replies
LaryAI's avatar
Level 58
The error "Typesense\Exceptions\RequestMalformed: Parameter `fields` is required" typically means that when Scout (with Typesense driver) is creating a schema in Typesense, it's missing the `fields` definition for your searchable model.

This generally happens if your model's `toSearchableArray()` returns an empty array, or if there are issues determining which fields should be mapped.

**Here's how you can resolve this:**

1. **Check `toSearchableArray()` Implementation:**

   In your Eloquent model (e.g., `App\Models\Post`), ensure you are returning all the searchable fields:

   ```php
   public function toSearchableArray()
   {
       return [
           'id' => $this->id,
           'title' => $this->title,
           'content' => $this->content,
           // add all the fields you want to be indexed here
       ];
   }
  1. Check Data in Database:

    If this happens right after migrate:fresh, your database might be empty, so the first model retrieved by Scout returns an empty array when toSearchableArray() is called.
    To fix this, either:

    • Run your seeders after migration, before running scout:import.
    • Or, manually add a test record to the relevant table.

    Example:

    php artisan migrate:fresh --seed
    php artisan scout:import "App\Models\Post"
    
  2. Scout Driver Configuration:

    Make sure your config/scout.php and Typesense configuration are correct.
    For the official Typesense Scout Driver, your model should have the searchable trait and correctly define toSearchableArray.

  3. Clear Typesense Collections (optional):

    If you changed the fields, you may need to delete existing collections in Typesense before importing again. You can do this via the Typesense dashboard or API.

Summary:
The error is usually caused by having an empty table (no data yet) after migration. The import command can't infer the Typesense fields from missing data. Seed your database before running scout:import, and double check your toSearchableArray() method.

Let me know if you have further questions!

Mattiman's avatar

After running php artisan migrate:fresh --seed of course my database is empty. But I cannot add a record because I get the 'Typesense\Exceptions\RequestMalformed: Parameter fields is required' error. So how can I fix that?>

Mattiman's avatar

Found the solution: there was a different model with the Scout searchable trait, but which was not yet defined in the Typesense fields array. Stupid mistake, but hard to track down.

Please or to participate in this conversation.