bkstar123/mysql-search 问题修复 & 功能扩展

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

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

bkstar123/mysql-search

Composer 安装命令:

composer require bkstar123/mysql-search

包简介

Enable full-text search and wildcard search for laravel/mysql applications

README 文档

README

A lightweight package to enable full-text search and partial search for laravel/mysql applications

1. Requirements

  • PHP 7.1.3+
  • Laravel 5.5+
  • MySQL 5.6+

2. Installtion

composer require bkstar123/mysql-search  

3. Usage

For example, your database has an articles table with id, title, content, status, user_id, created_at, updated_at columns.

You want to allow users to search for a term/keyword against title, content columns.

In order to do so, just do the following steps:
a) In Article model, import & use the trait Bkstar123\MySqlSearch\Traits\MySqlSearch
b) In Article model, define a public static property named as $mysqlSearchable, for instance:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Bkstar123\MySqlSearch\Traits\MySqlSearch;

class Article extends Model
{
    use MySqlSearch;

    public static $mysqlSearchable = ['title', 'content'];

    //...
}

c) If you want to use MySQL full-text search feature (supported by MyIASM, InnoDB - since MySQL 5.6+ storage engines), the below additional works are to be fulfilled:

-> Create FullTEXT index for articles table:
php artisan mysql-search:init "App\Article"

Note: To remove FULLTEXT index from articles table:
php artisan mysql-search:reset "App\Article"

d) Search query

Full text search (default):

<?php
App\Article::search($searchTerms, true, 'natural')
App\Article::search($searchTerms) // full-text search in natural language mode
App\Article::search($searchTerms, true, 'boolean') // Full-text search in boolean mode
App\Article::search($searchTerms, true, 'query') // Full-text search with query expansion

// Better to use with try...catch as follows
try {
    $articles = App\Article::search($searchText)
                    ->paginate(10)
                    ->appends([
                        'search' => $searchText
                ]);
} catch (Exception $e) {
    $articles = [];
}

By default, the default full-text search mode is NATURAL LANGUAGE. You can change this default value by using FULLTEXT_SEARCH_MODE variable in .env file, possible values: natural, boolean, query

Refer to https://www.w3resource.com/mysql/mysql-full-text-search-functions.php for details on natual language & boolean mode. The query expansion mode is not recommended due to the fact that it creates a lot of noise in the search result.

The biggest problem with full-text indexes is that thay do not play nicely with regular indexes. If you do a full-text search query in combination with using other normal indexes, you are going to have a very inefficient query (bad performance). Refer to https://medium.com/@kirkbackus/using-full-text-index-for-innodb-when-a-search-engine-is-not-feasible-d666830b4000

Partial search (using the operator LIKE %$searchTerms%)

<?php
App\Article::search($searchTerms, false) // partial search with LIKE operator against a wildcard term e.g: %searchTerm% 

bkstar123/mysql-search 适用场景与选型建议

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

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

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

围绕 bkstar123/mysql-search 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 165
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 3
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

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