ivanomatteo / laravel-scout-fulltext-engine
基于数据库全文索引的Scout数据库驱动程序,将索引数据存储在相关表中
0.1.13
2024-02-15 08:55 UTC
Requires
- php: ^8.0
- illuminate/contracts: ^9.0
- laravel/scout: ^9.4
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-09-15 10:18:34 UTC
README
此包提供了一个基于数据库/全文索引的Laravel/Scout Engine,但其工作方式与默认数据库引擎不同。
您不需要为您的表添加全文索引:用于搜索的数据将存储在具有多态关系的表中。
这提供了几个优点
- 您不需要更改当前表的架构
- 添加元数据很容易
- 索引过程可以延迟到作业中,因此不会减慢数据库的插入/更新操作
安装
您可以通过composer安装此包
composer require ivanomatteo/laravel-scout-fulltext-engine
您可以使用以下命令发布并运行迁移
php artisan vendor:publish --tag="scout-fulltext-engine-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="scout-fulltext-engine-config"
这是发布配置文件的内容
use IvanoMatteo\LaravelScoutFullTextEngine\Parsers\Extractors\CompositeNameExtractor; use IvanoMatteo\LaravelScoutFullTextEngine\Parsers\Query\QueryParserMysqlFullTextBool; return [ 'scout_engine_name' => 'scout-fulltext-engine', 'fulltext_options' => [ 'mode' => 'boolean', /* Note on bind_mode == 'join': it will try to modify selected columns by adding "model_table.*" if no column was selected or adding "model_table." prefix to selected columns. In some cases, for example when using DB::raw(), you must be careful because the query will be a join, and you have to avoid column names collisions */ 'bind_mode' => 'exists', // 'exists' | 'join' /* By default fulltext searches will return records ordered by match score: but in some case you may want the records to be ordered by multiple fields, for example: match_score, name in these cases is necessary to be explicit !!! only supported with bind_mode = 'join' */ 'order_by_score' => false, // this will add a field named 'fulltext_score' to the results. // it can be useful for tuning fulltext searches // !!! only supported with bind_mode = 'join' 'add_select_score' => false, ], 'pre_processing' => [ 'query' => [ // the parser will process the text passed to // search function preparing it for the specific // fulltext query type 'parser' => QueryParserMysqlFullTextBool::class, // Extractors will extrapolate metadata from the query text 'extractors' => [ [ // useful to match dotted words // must be used also in index_data section // "N.A.S.A" --extract--> [ "NASA", "N_A_S_A" ] 'class' => DottedWordsExtractor::class, 'must_match' => false, // true -> will prepend "+", for boolean mode, but depends by the parser class 'starts_with' => true, // true -> will append "*", for boolean mode, but depends by the parser class ], [ // composite name extractor will find words // composed by 1 or 2 characters followed by // a word longer than 3 characters, for example: // from "Robert De Niro" --extract--> [ "De_niro" ] // this is useful to overcome fulltext default words min-length (3 chars) // (but it will work only if used also in index data section) 'class' => CompositeNameExtractor::class, 'must_match' => false, 'starts_with' => true, ] ], ], 'index_data' => [ 'extractors' => [ //this will add extracted metadata to te index DottedWordsExtractor::class, CompositeNameExtractor::class, ], ], ], ];
在不同表中存储索引数据
还有可能使用不同的表来存储索引数据
- 创建另一个与"full_text_entries"结构相同的表
- 模型(应扩展FullTextEntry)
并将此方法添加到您的模型中
public function getFullTextEntryModel() { return FullTextEntry2::class; }
用法
只需配置Laravel Scout以使用此驱动程序即可:(在您的.env文件中)
SCOUT_DRIVER=scout-fulltext-engine
有关标准用法,请参阅 Laravel Scout 文档。
直接搜索模式
此包还提供了一个“直接搜索”模式:您只需将DirectSearchTrait添加到您的模型中
use Laravel\Scout\Searchable; use IvanoMatteo\LaravelScoutFullTextEngine\Concerns\DirectSearch; class MyModel extends Model { use Searchable; use DirectSearch; }
这样,您将获得
- fullTextEntry():与索引表的关联
- directSearch():作用域,您可以用它来代替search()
Scout的 search() 函数返回一个 Laravel\Scout\Builder 实例,其功能有限。
directSearch() 则返回一个 Illuminate\Database\Eloquent\Builder 实例,允许您像往常一样构建查询。
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 变更日志。
贡献
请参阅 贡献指南 了解详细信息。
安全漏洞
请查阅 我们的安全策略 了解如何报告安全漏洞。
致谢
许可协议
MIT许可协议(MIT)。有关更多信息,请参阅 许可文件。