Scout Elasticsearch Driver 使用教程
1. 项目介绍
Scout Elasticsearch Driver
是一个为 Laravel Scout 提供的 Elasticsearch 驱动扩展包。它允许你在 Laravel 应用中使用 Elasticsearch 进行高级搜索和数据过滤。该包提供了丰富的功能,包括索引配置、搜索规则、零停机迁移等,使得在 Laravel 项目中集成 Elasticsearch 变得更加简单和高效。
2. 项目快速启动
2.1 安装
首先,使用 Composer 安装 Scout Elasticsearch Driver
:
composer require babenkoivan/scout-elasticsearch-driver
2.2 配置
安装完成后,需要发布 Scout 和 Scout Elasticsearch Driver
的配置文件:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
php artisan vendor:publish --provider="ScoutElastic\ScoutElasticServiceProvider"
然后,在 config/scout.php
文件中设置驱动为 elastic
:
'driver' => env('SCOUT_DRIVER', 'elastic'),
2.3 创建索引配置器
使用以下命令创建一个新的索引配置器:
php artisan make:index-configurator MyIndexConfigurator
在生成的 MyIndexConfigurator.php
文件中,你可以配置索引名称和设置:
namespace App;
use ScoutElastic\IndexConfigurator;
class MyIndexConfigurator extends IndexConfigurator
{
protected $name = 'my_index';
protected $settings = [
'analysis' => [
'analyzer' => [
'es_std' => [
'type' => 'standard',
'stopwords' => '_spanish_',
],
],
],
];
}
2.4 创建可搜索模型
使用以下命令创建一个新的可搜索模型:
php artisan make:searchable-model MyModel --index-configurator=MyIndexConfigurator
在生成的 MyModel.php
文件中,配置模型的索引配置器和映射:
namespace App;
use ScoutElastic\Searchable;
use Illuminate\Database\Eloquent\Model;
class MyModel extends Model
{
use Searchable;
protected $indexConfigurator = MyIndexConfigurator::class;
protected $searchRules = [];
protected $mapping = [
'properties' => [
'title' => [
'type' => 'text',
'fields' => [
'raw' => [
'type' => 'keyword',
],
],
],
],
];
}
2.5 创建和更新索引
创建索引:
php artisan elastic:create-index "App\MyIndexConfigurator"
更新模型映射:
php artisan elastic:update-mapping "App\MyModel"
2.6 基本搜索
现在你可以使用 Elasticsearch 进行搜索了:
$results = App\MyModel::search('phone')
->select(['title', 'price'])
->where('color', 'red')
->orderBy('price', 'asc')
->collapse('brand')
->from(0)
->take(10)
->get();
3. 应用案例和最佳实践
3.1 高级搜索
Scout Elasticsearch Driver
支持多种高级搜索功能,如布尔查询、地理距离过滤、正则表达式过滤等。以下是一个使用正则表达式过滤的示例:
$results = App\MyModel::search('*')
->whereRegexp('name.raw', 'A.+')
->get();
3.2 零停机迁移
在需要更改索引映射时,可以使用零停机迁移功能。首先,确保你的索引配置器使用了 ScoutElastic\Migratable
特性:
namespace App;
use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;
class MyIndexConfigurator extends IndexConfigurator
{
use Migratable;
protected $name = 'my_index';
}
然后,运行迁移命令:
php artisan elastic:migrate-model "App\MyModel" my_index_v2
4. 典型生态项目
4.1 Elastic Scout Driver
Elastic Scout Driver
是一个通用的 Elasticsearch 驱动,适用于需要简单搜索功能的 Laravel 应用。
4.2 Elastic Scout Driver Plus
Elastic Scout Driver Plus
是 Elastic Scout Driver
的扩展,提供了更多高级功能,如布尔查询、高亮显示等。
4.3 Elastic Migrations
Elastic Migrations
是一个用于管理 Elasticsearch 索引模式的工具,类似于 Laravel 的数据库迁移功能。
通过这些生态项目,你可以构建一个完整的 Elasticsearch 搜索解决方案,满足各种复杂搜索需求。
Hunyuan3D-Part
腾讯混元3D-Part00Hunyuan3D-Omni
腾讯混元3D-Omni:3D版ControlNet突破多模态控制,实现高精度3D资产生成00GitCode-文心大模型-智源研究院AI应用开发大赛
GitCode&文心大模型&智源研究院强强联合,发起的AI应用开发大赛;总奖池8W,单人最高可得价值3W奖励。快来参加吧~0274community
本项目是CANN开源社区的核心管理仓库,包含社区的治理章程、治理组织、通用操作指引及流程规范等基础信息011Hunyuan3D-2
Hunyuan3D 2.0:高分辨率三维生成系统,支持精准形状建模与生动纹理合成,简化资产再创作流程。Python00Spark-Chemistry-X1-13B
科大讯飞星火化学-X1-13B (iFLYTEK Spark Chemistry-X1-13B) 是一款专为化学领域优化的大语言模型。它由星火-X1 (Spark-X1) 基础模型微调而来,在化学知识问答、分子性质预测、化学名称转换和科学推理方面展现出强大的能力,同时保持了强大的通用语言理解与生成能力。Python00GOT-OCR-2.0-hf
阶跃星辰StepFun推出的GOT-OCR-2.0-hf是一款强大的多语言OCR开源模型,支持从普通文档到复杂场景的文字识别。它能精准处理表格、图表、数学公式、几何图形甚至乐谱等特殊内容,输出结果可通过第三方工具渲染成多种格式。模型支持1024×1024高分辨率输入,具备多页批量处理、动态分块识别和交互式区域选择等创新功能,用户可通过坐标或颜色指定识别区域。基于Apache 2.0协议开源,提供Hugging Face演示和完整代码,适用于学术研究到工业应用的广泛场景,为OCR领域带来突破性解决方案。00- HHowToCook程序员在家做饭方法指南。Programmer's guide about how to cook at home (Chinese only).Dockerfile09
- PpathwayPathway is an open framework for high-throughput and low-latency real-time data processing.Python00
热门内容推荐
最新内容推荐
项目优选









