typesense/laravel-scout-typesense-driver
Composer 安装命令:
composer require typesense/laravel-scout-typesense-driver
包简介
Laravel Scout Driver for Typesense
关键字:
README 文档
README
This package makes it easy to add full text search support to your models with Laravel 10.* to 12.*.
Important
The features from the Scout driver in this repo have been merged upstream into Laravel Scout natively.
So we've temporarily paused development in this repo and plan to instead address any issues or improvements in the native Laravel Scout driver instead.
If there are any Typesense-specific features that would be hard to implement in Laravel Scout natively (since we need to maintain consistency with all the other drivers), then at that point we plan to add those features into this driver and maintain it as a "Scout Extended Driver" of sorts. But it's too early to tell if we'd want to do this, so we're in a holding pattern on this repo for now.
In the meantime, we recommend switching to the native Laravel Scout driver and report any issues in the Laravel Scout repo.
Contents
Development
This repository includes a Nix flake with PHP 8.3 and Composer:
nix develop composer update --prefer-dist --no-interaction --no-progress
You can also run the Composer install helper directly:
nix run .#install
The package tests expect Typesense to be reachable at http://localhost:8108 with API key xyz, matching the CI workflow. Once Typesense is running:
nix run .#test
Installation
The Typesense PHP SDK uses httplug to interface with various PHP HTTP libraries through a single API.
First, install the correct httplug adapter based on your guzzlehttp/guzzle version. For example, if you're on
Laravel 10, which includes Guzzle 7, then run this:
composer require php-http/guzzle7-adapter
Then install the driver:
composer require typesense/laravel-scout-typesense-driver
And 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
If you are unfamiliar with Laravel Scout, we suggest reading it's documentation first.
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 Todo extends Model implements TypesenseDocument { use Searchable; /** * Get the indexable data array for the model. * * @return array */ public function toSearchableArray() { return array_merge( $this->toArray(), [ // Cast id to string and turn created_at into an int32 timestamp // in order to maintain compatibility with the Typesense index definition below 'id' => (string) $this->id, 'created_at' => $this->created_at->timestamp, ] ); } /** * The Typesense schema to be created. * * @return array */ public function getCollectionSchema(): array { return [ 'name' => $this->searchableAs(), 'fields' => [ [ 'name' => 'id', 'type' => 'string', ], [ 'name' => 'name', 'type' => 'string', ], [ 'name' => 'created_at', 'type' => 'int64', ], ], 'default_sorting_field' => 'created_at', ]; } /** * The fields to be queried against. See https://typesense.org/docs/0.24.0/api/search.html. * * @return array */ public function typesenseQueryBy(): array { return [ 'name', ]; } }
Then, sync the data with the search service like:
php artisan scout:import App\\Models\\Todo
After that you can search your models with:
Todo::search('Test')->get();
Adding via Query
The searchable() method will chunk the results of the query and add the records to your search index. Examples:
$todo = Todo::find(1); $todo->searchable(); $todos = Todo::where('created_at', '<', now())->get(); $todos->searchable();
Multi Search
You can send multiple search requests in a single HTTP request, using the Multi-Search feature.
$searchRequests = [ [ 'collection' => 'todo', 'q' => 'todo' ], [ 'collection' => 'todo', 'q' => 'foo' ] ]; Todo::search('')->searchMulti($searchRequests)->paginateRaw();
Generate Scoped Search Key
You can generate scoped search API keys that have embedded search parameters in them. This is useful in a few different scenarios:
- You can index data from multiple users/customers in a single Typesense collection (aka multi-tenancy) and create scoped search keys with embedded
filter_byparameters that only allow users access to their own subset of data. - You can embed any search parameters (for eg:
exclude_fieldsorlimit_hits) to prevent users from being able to modify it client-side.
When you use these scoped search keys in a search API call, the parameters you embedded in them will be automatically applied by Typesense and users will not be able to override them.
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; use Typesense\LaravelTypesense\Concerns\HasScopedApiKey; use Typesense\LaravelTypesense\Interfaces\TypesenseDocument; class Todo extends Model implements TypesenseDocument { use Searchable, HasScopedApiKey; }
Usage
Todo::setScopedApiKey('xyz')->search('todo')->get();
Migrating from devloopsnet/laravel-typesense
- Replace
devloopsnet/laravel-typesensein your composer.json requirements withtypesense/laravel-scout-typesense-driver - The Scout driver is now called
typesense, instead oftypesensesearch. This should be reflected by setting the SCOUT_DRIVER env var totypesense, and changing the config/scout.php config key fromtypesensesearchtotypesense - Instead of importing
Devloops\LaravelTypesense\*, you should importTypesense\LaravelTypesense\* - Instead of models implementing
Devloops\LaravelTypesense\Interfaces\TypesenseSearch, they should implementTypesense\LaravelTypesense\Interfaces\TypesenseDocument
Authors
This package was originally authored by Abdullah Al-Faqeir and his company DevLoops: https://github.com/devloopsnet/laravel-scout-typesense-engine. It has since been adopted into the Typesense Github org.
Other key contributors include:
License
The MIT License (MIT). Please see the License File for more information.
typesense/laravel-scout-typesense-driver 适用场景与选型建议
typesense/laravel-scout-typesense-driver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 681.85k 次下载、GitHub Stars 达 146, 最近一次更新时间为 2021 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「laravel」 「typesense」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 typesense/laravel-scout-typesense-driver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 typesense/laravel-scout-typesense-driver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 typesense/laravel-scout-typesense-driver 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Search Engine Abstraction Layer
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
typesense search driver for Statamic
Abstraction Layer to index and search entities
Integration service classes to connect garethhudson07/api resources with a Typesense index
统计信息
- 总下载量: 681.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 146
- 点击次数: 17
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-09