typesense search driver for Statamic

v1.4.1 2024-11-14 09:52 UTC

This package is auto-updated.

Last update: 2024-11-25 14:08:16 UTC


README

This addon provides a Typesense search driver for Statamic sites.

Requirements

  • PHP 8.2+
  • Laravel 10+
  • Statamic 5
  • Typesense 0.2+

Installation

composer require statamic-rad-pack/typesense

Add the following variables to your env file:

TYPESENSE_HOST=http://127.0.0.1
TYPESENSE_API_KEY=

Add the new driver to the statamic/search.php config file:

'drivers' => [

    // other drivers

    'typesense' => [
        'client' => [
    	    '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),
        ],
    ],
],

You can optionally publish the config file for this package using:

php artisan vendor:publish --tag=statamic-typesense-config

Search Settings

Any additional settings you want to define per index can be included in the statamic/search.php config file. The settings will be updated when the index is created.

'articles' => [
    'driver' => 'typesense',
    'searchables' => ['collection:articles'],
    'fields' => ['id', 'title', 'url', 'type', 'content', 'locale'],
    'settings' => [
        'schema' => [
            /*
                Pass an optional schema, see the Typesense documentation for more info:
                https://typesense.org/docs/26.0/api/collections.html#with-pre-defined-schema
            */
            'fields' => [
                [
                  'name'  => 'company_name',
                  'type'  => 'string'
                ],
                [
                  'name'  => 'num_employees',
                  'type'  => 'int32'
                ],
                [
                  'name'  => 'country',
                  'type'  => 'string',
                  'facet' => true
                ], 
            ],
        ],
        /* 
            Pass any of the options from https://typesense.org/docs/26.0/api/search.html#search-parameters
        */
        'search_options' => [
            /* 
                eg Specify a custom sort by order, see the Typesense documentation for more info:
                https://typesense.org/docs/guide/ranking-and-relevance.html#ranking-based-on-relevance-and-popularity
            */
            'sort_by' => '_text_match(buckets: 10):desc,weighted_score:desc',
        ],
    ],
],