Laravel Scout With Typesense Handling Parameter Fields Is Required Error

image
image
image
image
image
image
image
image
Laravel Scout with Typesense: Handling “Parameter Fields is Required” Error

Laravel Scout with Typesense: Handling “Parameter Fields is Required” Error

Laravel Scout is a powerful driver-based full-text search package for Laravel. While it supports Algolia out of the box, developers often integrate it with Typesense, an open-source typo-tolerant search engine known for blazing-fast performance.

However, during integration, many developers encounter the error:

[Typesense\Error\ObjectUnprocessable]: Parameter 'fields' is required.

Let’s understand why this happens and how to resolve it with an example.

Why Typesense Needs the fields Parameter

When you perform a search in Typesense, you must specify which fields to search against in your collection schema. Unlike Algolia, which can auto-search across all indexed fields, Typesense enforces this for performance optimization.

Typical Scenario

Suppose you have a products collection indexed with these fields:

  1. id (int)
  2. name (string)
  3. description (string)
  4. price (float)

When you send a search query without specifying query_by (equivalent to fields), Typesense throws:

[Typesense\Error\ObjectUnprocessable]: Parameter 'query_by' is required.

This indicates that your Laravel Scout query is missing the fields parameter Typesense needs to process the search.

Solution for the above issue is that, you haven't passing the field correctly to scout, go to the scout.php and add your model and field value like the below one I have done,

'model-settings' => [
Product::class => [
'collection-schema' => [
'fields' => [
['name' => 'id', 'type' => 'string'],
['name' => 'name', 'type' => 'string'],
['name' => 'description', 'type' => 'string', 'optional' => true],
['name' => 'price', 'type' => 'float'],
],
'default_sorting_field' => 'price',
],
'search-parameters' => [
'query_by' => 'name'
],
],
],

and on the top you must have to add the model, namespace use App\Models\Product;

so the full code of scout.php looks like the below one

<?php
use App\Models\Product;
return [

/*
|--------------------------------------------------------------------------
| Default Search Engine
|--------------------------------------------------------------------------
|
| This option controls the default search connection that gets used while
| using Laravel Scout. This connection is used when syncing all models
| to the search service. You should adjust this based on your needs.
|
| Supported: "algolia", "meilisearch", "typesense",
| "database", "collection", "null"
|
*/

'driver' => env('SCOUT_DRIVER', 'algolia'),

/*
|--------------------------------------------------------------------------
| Index Prefix
|--------------------------------------------------------------------------
|
| Here you may specify a prefix that will be applied to all search index
| names used by Scout. This prefix may be useful if you have multiple
| "tenants" or applications sharing the same search infrastructure.
|
*/

'prefix' => env('SCOUT_PREFIX', ''),

/*
|--------------------------------------------------------------------------
| Queue Data Syncing
|--------------------------------------------------------------------------
|
| This option allows you to control if the operations that sync your data
| with your search engines are queued. When this is set to "true" then
| all automatic data syncing will get queued for better performance.
|
*/

'queue' => env('SCOUT_QUEUE', false),

/*
|--------------------------------------------------------------------------
| Database Transactions
|--------------------------------------------------------------------------
|
| This configuration option determines if your data will only be synced
| with your search indexes after every open database transaction has
| been committed, thus preventing any discarded data from syncing.
|
*/

'after_commit' => false,

/*
|--------------------------------------------------------------------------
| Chunk Sizes
|--------------------------------------------------------------------------
|
| These options allow you to control the maximum chunk size when you are
| mass importing data into the search engine. This allows you to fine
| tune each of these chunk sizes based on the power of the servers.
|
*/

'chunk' => [
'searchable' => 500,
'unsearchable' => 500,
],

/*
|--------------------------------------------------------------------------
| Soft Deletes
|--------------------------------------------------------------------------
|
| This option allows to control whether to keep soft deleted records in
| the search indexes. Maintaining soft deleted records can be useful
| if your application still needs to search for the records later.
|
*/

'soft_delete' => false,

/*
|--------------------------------------------------------------------------
| Identify User
|--------------------------------------------------------------------------
|
| This option allows you to control whether to notify the search engine
| of the user performing the search. This is sometimes useful if the
| engine supports any analytics based on this application's users.
|
| Supported engines: "algolia"
|
*/

'identify' => env('SCOUT_IDENTIFY', false),

/*
|--------------------------------------------------------------------------
| Algolia Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your Algolia settings. Algolia is a cloud hosted
| search engine which works great with Scout out of the box. Just plug
| in your application ID and admin API key to get started searching.
|
*/

'algolia' => [
'id' => env('ALGOLIA_APP_ID', ''),
'secret' => env('ALGOLIA_SECRET', ''),
'index-settings' => [
// 'users' => [
// 'searchableAttributes' => ['id', 'name', 'email'],
// 'attributesForFaceting'=> ['filterOnly(email)'],
// ],
],
],

/*
|--------------------------------------------------------------------------
| Meilisearch Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your Meilisearch settings. Meilisearch is an open
| source search engine with minimal configuration. Below, you can state
| the host and key information for your own Meilisearch installation.
|
| See: https://www.meilisearch.com/docs/learn/configuration/instance_options#all-instance-options
|
*/

'meilisearch' => [
'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
'key' => env('MEILISEARCH_KEY'),
'index-settings' => [
// 'users' => [
// 'filterableAttributes'=> ['id', 'name', 'email'],
// ],
],
],

/*
|--------------------------------------------------------------------------
| Typesense Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your Typesense settings. Typesense is an open
| source search engine using minimal configuration. Below, you will
| state the host, key, and schema configuration for the instance.
|
*/

'typesense' => [
'client-settings' => [
'api_key' => env('TYPESENSE_API_KEY', 'xyz'),
'nodes' => [
[
'host' => env('TYPESENSE_HOST', 'localhost'),
'port' => env('TYPESENSE_PORT', '8108'),
'path' => env('TYPESENSE_PATH', ''),
'protocol' => env('TYPESENSE_PROTOCOL', 'http'),
],
],
'nearest_node' => [
'host' => env('TYPESENSE_HOST', 'localhost'),
'port' => env('TYPESENSE_PORT', '8108'),
'path' => env('TYPESENSE_PATH', ''),
'protocol' => env('TYPESENSE_PROTOCOL', 'http'),
],
'connection_timeout_seconds' => env('TYPESENSE_CONNECTION_TIMEOUT_SECONDS', 2),
'healthcheck_interval_seconds' => env('TYPESENSE_HEALTHCHECK_INTERVAL_SECONDS', 30),
'num_retries' => env('TYPESENSE_NUM_RETRIES', 3),
'retry_interval_seconds' => env('TYPESENSE_RETRY_INTERVAL_SECONDS', 1),
],
// 'max_total_results' => env('TYPESENSE_MAX_TOTAL_RESULTS', 1000),
'model-settings' => [
Product::class => [
'collection-schema' => [
'fields' => [
['name' => 'id', 'type' => 'string'],
['name' => 'name', 'type' => 'string'],
['name' => 'description', 'type' => 'string', 'optional' => true],
['name' => 'price', 'type' => 'float'],
],
'default_sorting_field' => 'price',
],
'search-parameters' => [
'query_by' => 'name'
],
],
],
],

];

If your issue still not resolved, see this blog for implementing typesense in laravel from scratch -https://www.codehunger.in/blog/laravel-scout-with-typesense-build-a-full-product-search-example

Conclusion

Typesense with Laravel Scout is a performance-optimized alternative to Algolia, giving you data sovereignty and speed. However, understanding Typesense’s strict schema requirements such as the query_by parameter is essential to avoid errors like:

Parameter 'fields' is required.

Always define your searchable fields in your model via typesenseQueryBy() for clean, scalable, and error-free search implementation.