ivanomatteo/laravel-scout-fulltext-engine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ivanomatteo/laravel-scout-fulltext-engine

Composer 安装命令:

composer require ivanomatteo/laravel-scout-fulltext-engine

包简介

A scout DB fulltext-based driver that store index data in related tables

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub PhpStan Action Status GitHub Code Style Action Status Total Downloads

This package provide a Laravel/Scout Engine based on database/fulltext only, but work in a different way compared to the default database Engine.

You don't need to add fulltext indexes to your tables: the data used for search will be stored in a table with a polymorphic relation.

This provide several advantages:

  • you don't need to change current tables's schema
  • it's easy to add metadata
  • indexing process can be deferred in jobs, so it will not slow down database inserts/updates

Installation

You can install the package via composer:

composer require ivanomatteo/laravel-scout-fulltext-engine

You can publish and run the migrations with:

php artisan vendor:publish --tag="scout-fulltext-engine-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="scout-fulltext-engine-config"

This is the contents of the published config file:

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,
            ],
        ],
    ],


];

Storing indexed data in different tables

It's also possible use different tables to store indexed data:

  • creating another table with the same structure of "full_text_entries"
  • the model (that should extend FullTextEntry)

and adding this method to your models:

public function getFullTextEntryModel()
{
    return FullTextEntry2::class;
}

Usage

Simply configure Laravel Scout to use this driver: (in your .env file)

SCOUT_DRIVER=scout-fulltext-engine

and refer to laravel scout documentation for standard usage.

Direct Search Mode

This package also provide a "direct search" mode: you just need to add DirectSearch Trait to your Model:

use Laravel\Scout\Searchable;
use IvanoMatteo\LaravelScoutFullTextEngine\Concerns\DirectSearch;

class MyModel extends Model
{
    use Searchable;
    use DirectSearch;

}

In this way you will get:

  • fullTextEntry(): relation to indexed table
  • directSearch(): scope, that you can use intead of search()

Scout's search() function, returns an instance of Laravel\Scout\Builder that has limited functionalities.

directSearch() instead, will return an instance of Illuminate\Database\Eloquent\Builder that allow you to build your query as usual.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

ivanomatteo/laravel-scout-fulltext-engine 适用场景与选型建议

ivanomatteo/laravel-scout-fulltext-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.15k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2022 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「ivanomatteo」 「laravel-scout-fulltext-engine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ivanomatteo/laravel-scout-fulltext-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ivanomatteo/laravel-scout-fulltext-engine 我们能提供哪些服务?
定制开发 / 二次开发

基于 ivanomatteo/laravel-scout-fulltext-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-12