sawmainek / laravel-scout-tntsearch-driver
基于 https://github.com/teamtnt/tntsearch 的 Laravel Scout 搜索包驱动程序
Requires
- php: >=7.1
- illuminate/bus: ~5.4|^6.0|^7.0
- illuminate/contracts: ~5.4|^6.0|^7.0
- illuminate/database: ~5.4|^6.0|^7.0
- illuminate/pagination: ~5.4|^6.0|^7.0
- illuminate/queue: ~5.4|^6.0|^7.0
- illuminate/support: ~5.4|^6.0|^7.0
- laravel/scout: 7.*|^8.0
- teamtnt/tntsearch: 2.*
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~5.0
Suggests
- teamtnt/tntsearch: Required to use the TNTSearch engine.
- v8.4.1
- v8.4.0
- v8.3.0
- v8.2.0
- v8.1.0
- v8.0.0
- v7.2.0
- v7.1.0
- v7.0.0
- v6.1
- v3.3.0
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.0
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- dev-master / 1.0.x-dev
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-analysis-86331W
- dev-analysis-8LmWkw
- dev-analysis-q1kD5w
- dev-analysis-qJD9xn
- dev-analysis-zY69Qj
- dev-analysis-8Pe7EL
- dev-analysis-z3GJ69
This package is auto-updated.
Last update: 2024-08-28 07:46:14 UTC
README
此包使得在 Laravel 5.3 到 7.0 中轻松为模型添加全文搜索支持。
在 Open Collective 支持我们
内容
安装
您可以通过 composer 安装此包
composer require sawmainek/laravel-scout-tntsearch-driver
添加服务提供者
// config/app.php 'providers' => [ // ... TeamTNT\Scout\TNTSearchScoutServiceProvider::class, ],
确保您已将 Laravel Scout 作为提供者添加,否则您将收到 "无法解析的依赖项" 错误
// config/app.php 'providers' => [ // ... Laravel\Scout\ScoutServiceProvider::class, ],
将 SCOUT_DRIVER=tntsearch
添加到您的 .env
文件
然后,您应该将 scout.php
配置文件发布到您的配置目录
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
在您的 config/scout.php
中添加
'tntsearch' => [ 'storage' => storage_path(), //place where the index files will be stored 'fuzziness' => env('TNTSEARCH_FUZZINESS', false), 'fuzzy' => [ 'prefix_length' => 2, 'max_expansions' => 50, 'distance' => 2 ], 'asYouType' => false, 'searchBoolean' => env('TNTSEARCH_BOOLEAN', false), ],
为了防止您的搜索索引提交到您的项目仓库,请将以下行添加到您的 .gitignore
文件。
/storage/*.index
可以按模型设置 asYouType
选项,下面是示例。
用法
安装 scout 和 TNTSearch 驱动程序后,您需要将 Searchable
特性添加到您想要使其可搜索的模型中。此外,通过在模型上定义 toSearchableArray
方法来定义您想要使其可搜索的字段
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; class Post extends Model { use Searchable; public $asYouType = true; /** * Get the indexable data array for the model. * * @return array */ public function toSearchableArray() { $array = $this->toArray(); // Customize array... return $array; } }
然后,像这样与搜索服务同步数据
php artisan scout:import App\\Post
如果您有大量记录并且想加快速度,您可以运行(注意,使用此方法后,您将无法在 toSearchableArray()
中使用模型关系)
php artisan tntsearch:import App\\Post
之后,您可以像这样搜索您的模型
Post::search('Bugs Bunny')->get();
Scout 状态
php artisan scout:status
使用此简单命令,您将快速了解您的搜索索引。
或者,您可以通过传递一个可搜索的模型参数
php artisan scout:status "App\Models\Post"
约束
除了 where()
表达式作为条件之外,您还可以使用 Eloquent 查询来约束您的搜索。这允许您考虑关系。
如果您使用此功能,则必须在控制器中定义所有查询之后调用搜索命令。
您已经知道的 where()
表达式可以在任何地方应用。
namespace App\Http\Controllers; use App\Post; class PostController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $post = new Post; // filter out posts to which the given topic is assigned if($request->topic) { $post = $post->whereNotIn('id', function($query){ $query->select('assigned_to')->from('comments')->where('topic','=', request()->input('topic')); }); } // only posts from people that are no moderators $post = $post->byRole('moderator','!='); // when user is not admin filter out internal posts if(!auth()->user()->hasRole('admin')) { $post= $post->where('internal_post', false); } if ($request->searchTerm) { $constraints = $post; // not necessary but for better readability $post = Department::search($request->searchTerm)->constrain($constraints); } $post->where('deleted', false); $post->orderBy('updated_at', 'asc'); $paginator = $post->paginate(10); $posts = $paginator->getCollection(); // return posts } }
通过查询添加
searchable()
方法将分块查询的结果添加到您的搜索索引中。
$post = Post::find(1); // You may also add record via collection... $post->searchable(); // OR $posts = Post::where('year', '>', '2018')->get(); // You may also add records via collections... $posts->searchable();
当使用约束时,在将约束添加到查询之后应用它,如上述示例所示。
排序
现在可以像对 where()
表达式一样对搜索查询应用 orderBy()
表达式。
当使用约束时,在将约束添加到查询之后应用它,如上述示例所示。
赞助商
成为赞助商,将您的标志放在我们的 Github README 上,并带有指向您网站的链接。[成为赞助商]
致谢
贡献者
支持者
感谢所有支持者! 🙏 [成为支持者]
赞助商
通过成为赞助商来支持此项目。您的标志将在这里显示,并附有您网站的链接。[成为赞助商]
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。