hi019 / laravel-scout-typesense-engine

Typesense Engine for Laravel Scout

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Latest Version on Packagist Postcardware

PHP from Packagist Total Downloads

Laravel Scout Typesense Engine

Typesense engine for laravel/scout https://github.com/typesense/typesense .

laravel-scout-typesense-engine
 socialcard

This package makes it easy to add full text search support to your models with Laravel 7.* to 8.*.

Contents

Installation

You can install the package via composer:

composer require typesense/laravel-typesense

Add the service provider:

// config/app.php
'providers' => [
    // ...
    Typesense\LaravelTypesense\TypesenseServiceProvider::class,
],

Ensure you have Laravel Scout as a provider too otherwise you will get an "unresolvable dependency" error

// config/app.php
'providers' => [
    // ...
    Laravel\Scout\ScoutServiceProvider::class,
],

Add SCOUT_DRIVER=typesense to your .env file

Then you should publish scout.php configuration file to your config directory

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

In your config/scout.php add:

'typesense' => [
    'api_key'         => 'abcd',
    'nodes'           => [
      [
        'host'     => 'localhost',
        'port'     => '8108',
        'path'     => '',
        'protocol' => 'http',
      ],
    ],
    'nearest_node'    => [
        'host'     => 'localhost',
        'port'     => '8108',
        'path'     => '',
        'protocol' => 'http',
    ],
    'connection_timeout_seconds'   => 2,
    'healthcheck_interval_seconds' => 30,    
    'num_retries'                  => 3,
    'retry_interval_seconds'       => 1,
  ],

Usage

After you have installed scout and the Typesense driver, you need to add the Searchable trait to your models that you want to make searchable. Additionaly, define the fields you want to make searchable by defining the toSearchableArray method on the model and implement TypesenseSearch:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Typesense\LaravelTypesense\Interfaces\TypesenseDocument;
use Laravel\Scout\Searchable;

class Post extends Model implements TypesenseDocument
{
    use Searchable;

    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        $array = $this->toArray();

        // Customize array...

        return $array;
    }

    public function getCollectionSchema(): array {
      return [
        'name' => $this->searchableAs(),
        'fields' => [
          [
            'name' => 'title',
            'type' => 'string',
          ],
          [
            'name' => 'created_at',
            'type' => 'int32',
          ],
        ],
        'default_sorting_field' => 'created_at',
      ];
    }

    public function typesenseQueryBy(): array {
      return [
        'name',
      ];
    }
    
}

Then, sync the data with the search service like:

php artisan scout:import App\\Post

After that you can search your models with:

Post::search('Bugs Bunny')->get();

Adding via Query

The searchable() method will chunk the results of the query and add the records to your search index.

$post = Post::find(1);

// You may also add record via collection... $post->searchable();

// OR

$posts = Post::where('year', '>', '2018')->get();

// You may also add records via collections... $posts->searchable();

Migrating from devloopsnet/laravel-typesense

  • Replace devloopsnet/laravel-typesense in your composer.json requirements with typesense/laravel-typesense
  • The Scout driver is now called typesense, instead of typesensesearch. This should be reflected by setting the SCOUT_DRIVER env var to typesense, and changing the config/scout.php config key from typesensesearch to typesense
  • Instead of importing Devloops\LaravelTypesense\*, you should import Typesense\LaravelTypesense\*
  • Instead of models implementing Devloops\LaravelTypesense\Interfaces\TypesenseSearch, they should implement Typesense\LaravelTypesense\Interfaces\TypesenseDocument

Author

License

The MIT License (MIT). Please see License File for more information.

About

Typesense Engine for Laravel Scout


Languages

Language:PHP 100.0%