定制 vuthaihoc/sqlout 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

vuthaihoc/sqlout

Composer 安装命令:

composer require vuthaihoc/sqlout

包简介

MySQL fulltext driver for Laravel Scout.

README 文档

README

Sqlout is a very simple MySQL driver for Laravel Scout. It indexes the data into a dedicated table of the MySQL database, and uses a fulltext index to search. It is meant for small-sized projects, for which bigger solutions such as ElasticSearch would be an overkill.

Sqlout is different than Scout 9's native Database engine because it indexes data in a separate, dedicated table, and uses a fulltext index. Sqlout has more features such as field weights and word stemming.

Sqlout is compatible with Laravel 5.8+ to 10.x and Scout 7.1+ / 8.x / 9.x / 10.x (credit goes to ikari7789 for Laravel 9 and 10 / Scout 9 and 10 support).

Version Compatibility

Laravel Scout Sqlout
5.8 7.1 / 7.2 1.x / 2.0
6.x 7.1 / 7.2 1.x / 2.0
6.x 8.x 2.0
7.x 8.x 2.0
8.x 8.x 3.x
8.x / 9.x 9.x 4.x
9.x / 10.x 10.x 5.x

Setup

Require the package:

composer require baril/sqlout

Publish the configuration:

php artisan vendor:publish

If you're not using package discovery, manually add the service providers (Scout's and Sqlout's) to your config/app.php file:

return [
    // ...
    'providers' => [
        // ...
        Laravel\Scout\ScoutServiceProvider::class,
        Baril\Sqlout\SqloutServiceProvider::class,
    ],
];

Migrate your database:

php artisan sqlout:make-migration
php artisan migrate

This will create a searchindex table in your database (the table name can be customized in the config file).

If you want to index models that belong to different connections, you need a table for Sqlout on each connection. To create the table on a connection that is not the default connection, you can call the sqlout:make-migration command and pass the name of the connection:

php artisan sqlout:make-migration my_other_connection
php artisan migrate

Making a model searchable

use Baril\Sqlout\Searchable;

class Post extends Model
{
    use Searchable;

    protected $weights = [
        'title' => 4,
        'excerpt' => 2,
    ];

    public function toSearchableArray()
    {
        return [
            'title' => $this->post_title,
            'excerpt' => $this->post_excerpt,
            'body' => $this->post_content,
        ];
    }
}

The example above is similar to what is described in Scout's documentation, with the following differences/additions:

  • You'll notice that the model uses the Baril\Sqlout\Searchable trait instead of Laravel\Scout\Searchable.
  • The $weight property can be used to "boost" some fields. The default value is 1.

Once this is done, you can index your data using Scout's Artisan command:

php artisan scout:import "App\Post"

Your models will also be indexed automatically on save.

Searching

Basics

$results = Post::search('this rug really tied the room together')->get();
$results = Post::search('the dude abides')->withTrashed()->get();

See Scout's documentation for more details.

Sqlout's builder also provides the following additional methods:

// Restrict the search to some fields only:
$builder->only('title');
$builder->only(['title', 'excerpt']);
// (use the same names as in the toSearchableArray method)

// Retrieve the total number of results:
$nbHits = $builder->count();

Using scopes

With Sqlout, you can also use your model scopes on the search builder, as if it was a query builder on the model itself. Similarly, all calls to the where method on the search builder will be forwarded to the model's query builder.

$results = Post::search('you see what happens larry')
    ->published() // the `published` scope is defined in the Post class
    ->where('date', '>', '2010-10-10')
    ->get();

⚠️ Keep in mind that these forwarded scopes will actually be applied to a subquery (the main query here being the one on the searchindex table). This means that for example a scope that adds an order by clause won't have any effect. See below for the proper way to order results.

If the name of your scope collides with the name of a method of the Baril\Sqlout\Builder object, you can wrap your scope into the scope method:

$results = Post::search('ve vant ze money lebowski')
    ->scope(function ($query) {
        $query->within('something');
    })
    ->get();

Search modes

MySQL's fulltext search comes in 3 flavours:

  • natural language mode,
  • natural language mode with query expansion,
  • boolean mode.

Sqlout's default mode is "natural language" (but this can be changed in the config file).

You can also switch between all 3 modes on a per-query basis, by using the following methods:

$builder->inNaturalLanguageMode();
$builder->withQueryExpansion();
$builder->inBooleanMode();

Ordering the results

If no order is specified, the results will be ordered by score (most relevant first). But you can also order the results by any column of your table.

$builder->orderBy('post_status', 'asc')->orderByScore();
// "post_status" is a column of the original table

In the example below, the results will be ordered by status first, and then by descending score.

Filters, tokenizer, stopwords and stemming

In your config file, you can customize the way the indexed content and search terms will be processed:

return [
    // ...
    'sqlout' => [
        // ...
        'filters' => [ // anything callable (function name, closure...)
            'strip_tags',
            'html_entity_decode',
            'mb_strtolower',
            'strip_punctuation', // this helper is provided by Sqlout (see helpers.php)
        ],
        'token_delimiter' => '/[\s]+/',
        'minimum_length' => 2,
        'stopwords' => [
            'est',
            'les',
        ],
        'stemmer' => Wamania\Snowball\Stemmer\French::class,
    ],
];

In the example, the stemmer comes from the package wamania/php-stemmer, but any class with a stem method, or anything callable such as a closure, will do.

vuthaihoc/sqlout 适用场景与选型建议

vuthaihoc/sqlout 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 vuthaihoc/sqlout 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-17