定制 provision/searchable 二次开发

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

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

provision/searchable

Composer 安装命令:

composer require provision/searchable

包简介

Searchable indexing with MySQL full text for Laravel

README 文档

README

Build Status

Laravel MySQL fulltext search

This package creates a MySQL fulltext index for models and enables you to search through those.

Requirements

  • Laravel >= 5.7
  • MySQL >= 5.6 / MariaDB >= 10.0.15

Important!

In config/database.php set

'mysql' => [
...
    'strict' => false,
...
]

Install

  1. Install with composer composer require provision/searchable.
  2. Publish migrations and config php artisan vendor:publish --tag=searchable
  3. Migrate the database php artisan migrate

Usage

The package uses a model observer to update the index when models change. If you want to run a full index you can use the console commands.

Models

Add the SearchableTrait trait to the model you want to have indexed and define the columns you'd like to index as title and content.

Example

class Clients extends Model
{

    use \ProVision\Searchable\Traits\SearchableTrait;

    /**
     * @inheritDoc
     */
    protected function getSearchableTitleColumns(): array
    {
        return [
            'name'
        ];
    }

    /**
     * @inheritDoc
     */
    protected function getSearchableContentColumns(): array
    {
        return [
            'description',
            'address',
            'vat_number',
            'contacts.value',
            'contactPersons.first_name',
            'contactPersons.last_name',
            'contactPersons.contacts.value',
        ];
    }

}

You can use a dot notation to query relationships for the model, like contacts.value.

Relation model indexing

On related model for indexing use SearchableRelationTrait and method getSearchableRelationName to return relation name.

Listen for changes on relation and update parent model

Example

class Contact extends Model
{
    use \ProVision\Searchable\Traits\SearchableRelationTrait;

     /**
     * @return MorphTo
     */
    public function contactable()
    {
        return $this->morphTo();
    }

    /**
     * @inheritDoc
     */
    static function getSearchableRelationName(): string
    {
        return 'contactable';
    }
}

Searching

You can search using the search method.

$clientsCollection = Clients::search('John Doe')->paginate();

Search with specific fulltext search mode

use ProVision\Searchable\SearchableModes;
---
$clientsCollection = Clients::search('John Doe', SearchableModes::Boolean)->paginate();

Available modes

  • NaturalLanguage - IN NATURAL LANGUAGE MODE
  • NaturalLanguageWithQueryExpression - IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
  • Boolean - IN BOOLEAN MODE
  • QueryExpression - WITH QUERY EXPANSION

MySQL fulltext search documentation: https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html

Search with relations & additional wheres

$clientsCollection = Clients::search('John Doe')->where('active', 1)->with(['contacts'])->paginate();

Order searchable score

$clientsCollection = Clients::search('John Doe')->searchableOrder('asc')->paginate();

Available options:

  • ASC
  • DESC

Commands

searchable:index

Index all models for a certain class

 php artisan  searchable:index
 
Usage:
  searchable:index <model_class> {id?}

Arguments:
  model_class           Classname of the model to index
  id                    Model id to index (optional)

Example
  • Indexing all clients

php artisan searchable:index "\App\Models\Client"

  • Indexing specific client by id

php artisan searchable:index "\App\Models\Client" 1

searchable:unindex

UnIndex all models for a certain class

 php artisan  searchable:unindex
 
Usage:
  searchable:unindex <model_class> {id?}

Arguments:
  model_class           Classname of the model to index
  id                    Model id to unindex (optional)

Example
  • UnIndexing all clients

php artisan searchable:unindex "\App\Models\Client"

  • UnIndexing specific client by id

php artisan searchable:unindex "\App\Models\Client" 1

Config options

db_connection

Choose the database connection to use, defaults to the default database connection. When you are NOT using the default database connection, this MUST be set before running the migration to work correctly.

table_name

Table name of index

command_prefix

Prefix of commands

weight.title, weight.content

Results on title or content are weighted in the results. Search result score is multiplied by the weight in this config

cleaners

Clean searching keywords for prevent breaking the MySQL query.

Testing

$ composer test

provision/searchable 适用场景与选型建议

provision/searchable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.42k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2019 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 8.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 22
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

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