Laravel Scout Documentation

repository·11.x·Indexed 23 days ago

https://github.com/laravel/scout

A driver-based solution for adding full-text search to Eloquent models, providing automatic synchronization of model changes to search indexes. Supports multiple search engines including Algolia, Meilisearch, Typesense, as well as Database, Collection, and Null drivers. Includes features for filtering results with constraints, sorting, pagination, and handling soft-deleted records.

Tokens
4.7K
Snippets
6
Records
41
Agent score
82%

What's inside Laravel Scout

  1. Introduction to Laravel Scout

    11.x
    Laravel Scout is a driver-based solution for adding full-text search capabilities to your Eloquent models. It automates the synchronization of your model changes (such as creating, updating, or deleting records) with your search indexes, ensuring your search data remains consistent with your database.
  2. Algolia Driver: Update dependencies and handle exception changes (Scout 7.x)

    11.x

    When using the Algolia driver with Scout 7.x, ensure you update your algolia/algoliasearch-client-php dependency to ^2.2 in composer.json.

    Key changes in the Algolia client update:

    • The AlgoliaSearch\AlgoliaException class was renamed to Algolia\AlgoliaSearch\Exceptions\AlgoliaException.
    • If passing a callback to the search method, the first argument is now an instance of Algolia/AlgoliaSearch/SearchIndex.
  3. Upgrade Meilisearch PHP SDK for Scout 10.x

    11.x

    Scout 10.x requires Meilisearch PHP 1.0 or higher. When upgrading, update your composer.json to use ^1.0.

    Important: In the 1.0 SDK, all namespace and class references changed from MeiliSearch (capital S) to Meilisearch (lowercase s). You must update your code to reflect this change in capitalization.

    "meilisearch/meilisearch-php": "^1.0"
  4. How MeilisearchEngine handles search queries and filters

    11.x

    The MeilisearchEngine translates Scout's Builder queries into Meilisearch-compatible parameters.

    Supported Filter Conversions:

    • Standard where clauses: Converts operators like =, !=, >, <, etc., into Meilisearch filter syntax.
    • Booleans: Converts PHP booleans to Meilisearch true/false literals.
    • Nulls: Converts null values to IS NULL or IS NOT NULL syntax.
    • Enums: Supports BackedEnum values by using their underlying value.
    • whereIns and whereNotIns: Converts these to Meilisearch IN and NOT IN syntax using array brackets (e.g., field IN [val1, val2]).

    Sorting:

    Scout orderBy clauses are converted to the column:direction format required by Meilisearch (e.g., price:desc).

  5. Configure Prefix Search using DatabaseEngine

    11.x
    To enable prefix searching (e.g., searching for term% instead of %term%) within the DatabaseEngine, apply the SearchUsingPrefix attribute to the columns in your model's toSearchableArray method. This instructs the engine to use a LIKE operator with a trailing wildcard for those specific columns.
  6. Use the CollectionEngine for local searching

    11.x

    The CollectionEngine is a search engine driver that uses local database collections for searching instead of an external service. It is useful as a fallback or for simple use cases where a full-text search engine like Algolia or Meilisearch is not required.

    When using this engine, Scout performs searches by querying the underlying Eloquent model and then filtering the results based on the toSearchableArray() output of the models. It performs a case-insensitive partial match against the values returned by toSearchableArray().

  7. Configure Full-Text Search using DatabaseEngine

    11.x

    When using the DatabaseEngine, you can leverage your database's native full-text search capabilities by applying the SearchUsingFullText attribute to your model's toSearchableArray method. This allows Scout to use whereFullText queries instead of standard LIKE queries for specific columns.

    Additionally, you can configure search options (like language or mode) directly via the attribute arguments.

  8. Handle the new `wheres` property format in custom engines (Scout 11.x)

    11.x

    In Scout 11.x, the wheres property on the Builder instance has changed from a simple key/value associative array to an array of arrays. Each entry in the array now contains field, operator, and value keys. This change enables support for comparison operators like >, <, >=, <=, and != via the where method.

    If you are implementing a custom engine and accessing the wheres property directly, you must update your logic to iterate over the new structure.

    // New usage pattern for comparison operators
    User::search('*')->where('age', '>', 30)->get();
    
    // How to access the property in a custom engine
    foreach ($builder->wheres as $where) {
        $field = $where['field'];
        $operator = $where['operator'];
        $value = $where['value'];
    
        // ...
    }
  9. Override `getScoutKeyName` with unqualified names (Scout 10.x)

    11.x

    In Scout 10.x, the getScoutKeyName method returns the unqualified key name and no longer qualifies it with the table name. If you are overriding this method in your models, ensure you return only the unqualified name (e.g., 'id' instead of 'posts.id').

    Note: The getUnqualifiedScoutKeyName method was removed in 10.x as it is no longer necessary.

    public function getScoutKeyName()
    {
        return 'id';
    }
  10. Configure Typesense search parameters in Scout

    11.x

    You can customize the search behavior for specific models by defining settings in your Scout configuration. The TypesenseEngine looks for model-specific settings under the scout.typesense.model-settings configuration key.

    Supported configuration keys per model:

    • search-parameters.query_by: Defines which fields Typesense should search through.
    • search-parameters.prefix: A boolean determining if prefix search is enabled (defaults to true).
    • collection-schema: The schema used to automatically create the Typesense collection when indexing objects.