toanld / laravel-scout-tntsearch-driver
基于 https://github.com/teamtnt/tntsearch 的 Laravel Scout 搜索包的驱动程序
Requires
- php: >=7.1|^8
- illuminate/bus: ~5.4|^6.0|^7.0|^8.0
- illuminate/contracts: ~5.4|^6.0|^7.0|^8.0
- illuminate/pagination: ~5.4|^6.0|^7.0|^8.0
- illuminate/queue: ~5.4|^6.0|^7.0|^8.0
- illuminate/support: ~5.4|^6.0|^7.0|^8.0
- laravel/scout: 7.*|^8.0|^8.3|^9.0
- teamtnt/tntsearch: ^2.8
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
Suggests
- teamtnt/tntsearch: Required to use the TNTSearch engine.
- v11.6.0
- v11.5.0
- v11.4.0
- v11.3.0
- v11.2.0
- v11.1.0
- v11.0.0
- v10.0.0
- v9.0.0
- v8.3.0
- v8.2.0
- v8.1.0
- v8.0.0
- v7.2.0
- v7.1.0
- v7.0.0
- v6.1.0
- 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-09-15 23:12:39 UTC
README
此包使您能够轻松将全文搜索功能添加到您的模型中,适用于 Laravel 5.3 到 8.0。
高级产品
如果您认为 TNT Search 是您宝贵的资产之一,请查看我们的高级产品之一
在 Open Collective 上支持我们
内容
安装
您可以通过 composer 安装此包
composer require teamtnt/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), 'maxDocs' => env('TNTSEARCH_MAX_DOCS', 500), ],
为了防止您的搜索索引提交到您的项目仓库中,请将以下行添加到您的 .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 = Post::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 中,附带链接到您的网站。 [成为赞助商]
致谢
贡献者
支持者
感谢所有我们的支持者!🙏 [成为支持者]