gnahotelsolutions / laravel-elasticsearch
在您的 Laravel 应用程序中轻松使用官方 PHP ElasticSearch 客户端。
Requires
- php: ^7.3|^8.0|^8.1
- ext-json: *
- elasticsearch/elasticsearch: ^7.11
- guzzlehttp/psr7: ^2.0
- illuminate/contracts: ^8.0|^9.0|^10
- illuminate/support: ^8.0|^9.0|^10
- psr/http-message: ^1.0
Requires (Dev)
- limedeck/phpunit-detailed-printer: ^6.0
- mockery/mockery: ^1.4.3
- orchestra/testbench: ^6.5
- phpunit/phpunit: ^9.4
Suggests
- aws/aws-sdk-php: Required to connect to an Elasticsearch host on AWS (^3.80)
- dev-master
- 8.3.0
- 8.2.0
- 8.1.0
- 8.0.7
- 8.0.6
- 8.0.5
- 8.0.4
- 8.0.3
- 8.0.2
- 8.0.1
- 8.0.0
- 4.x-dev
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.x-dev
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.0
- 3.6.0
- 3.5.1
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.0
- 2.1.1
- 2.1.0
- 2.0.x-dev
- 2.0.1
- 2.0.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.0
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- dev-es-5-to-7
- dev-factory-manager
This package is auto-updated.
Last update: 2024-08-29 16:45:11 UTC
README
在您的 Laravel 或 Lumen 应用程序中轻松使用 官方 Elastic Search 客户端。
⚠️ 此包将被废弃
由于几个因素,包括我不再使用 ES,我将停止此包的开发。如果您有兴趣接管此项目,请在此或 Twitter 上联系我:@cviebrock
安装和配置
通过 composer 安装 cviebrock/laravel-elasticsearch
包的当前版本
composer require cviebrock/laravel-elasticsearch
如果您正在使用 ElasticSearch 版本 5,则安装此包的版本 2
composer require cviebrock/laravel-elasticsearch:^2
Laravel
包的服务提供者将自动注册其服务提供者。
发布配置文件
php artisan vendor:publish --provider="GNAHotelSolutions\LaravelElasticsearch\ServiceProvider"
通过 .env 文件进行替代配置方法
根据上述建议发布配置文件后,您可以通过向应用程序的 .env
文件中添加以下内容来配置 ElasticSearch(使用适当的值)
ELASTICSEARCH_HOST=localhost ELASTICSEARCH_PORT=9200 ELASTICSEARCH_SCHEME=http ELASTICSEARCH_USER= ELASTICSEARCH_PASS=
连接到 AWS Elasticsearch 服务
如果您正在连接到 Amazon AWS 上的 ElasticSearch 实例,则还需要 composer require aws/aws-sdk-php:^3.80
并将以下内容添加到您的 .env
文件中
AWS_ELASTICSEARCH_ENABLED=true AWS_REGION=... AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=...
如果您必须使用具有自定义凭据(例如 instanceProfile()
)的另一种身份验证方法,则必须发布配置文件并使用 aws_credentials
<?php // config/elasticsearch.php $provider = \Aws\Credentials\CredentialProvider::instanceProfile(); $memoizedProvider = \Aws\Credentials\CredentialProvider::memoize($provider); $credentials = $memoizedProvider()->wait(); ... 'hosts' => [ [ 'host' => env('ELASTICSEARCH_HOST', 'localhost'), // For local development, the default Elasticsearch port is 9200. // If you are connecting to an Elasticsearch instance on AWS, you probably want to set this to null 'port' => env('ELASTICSEARCH_PORT', 9200), 'scheme' => env('ELASTICSEARCH_SCHEME', null), 'user' => env('ELASTICSEARCH_USER', null), 'pass' => env('ELASTICSEARCH_PASS', null), // If you are connecting to an Elasticsearch instance on AWS, you will need these values as well 'aws' => env('AWS_ELASTICSEARCH_ENABLED', false), 'aws_region' => env('AWS_REGION', ''), 'aws_key' => env('AWS_ACCESS_KEY_ID', ''), 'aws_secret' => env('AWS_SECRET_ACCESS_KEY', '') 'aws_credentials' => $credentials ], ],
如果您有一个在守护进程运行的作业,您必须使用 Closure。这样,凭据将在运行时更新。
<?php // config/elasticsearch.php $provider = \Aws\Credentials\CredentialProvider::instanceProfile(); $memoizedProvider = \Aws\Credentials\CredentialProvider::memoize($provider); ... 'hosts' => [ [ ... 'aws_credentials' => $memoizedProvider ], ],
如果您正在使用 php artisan config:cache
,则不能在您的配置文件中包含 Closure,可以像这样调用它
<?php // config/elasticsearch.php ... 'hosts' => [ [ ... 'aws_credentials' => [\Aws\Credentials\CredentialProvider::class, 'defaultProvider'], ], ],
Lumen
如果您使用 Lumen,请在 bootstrap/app.php
文件中注册服务提供者和配置
$app->register(GNAHotelSolutions\LaravelElasticsearch\ServiceProvider::class); $app->configure('elasticsearch');
手动将配置文件复制到您的应用程序中。
使用方法
Elasticsearch
门面对象只是进入 ES 客户端 的入口,因此您之前可能使用过
use Elasticsearch\ClientBuilder; $data = [ 'body' => [ 'testField' => 'abc' ], 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', ]; $client = ClientBuilder::create()->build(); $return = $client->index($data);
现在,您可以将这两行替换为以下内容
use Elasticsearch; $return = Elasticsearch::index($data);
这将运行默认连接上的命令。您可以在任何连接上运行命令(请参阅配置文件中的 defaultConnection
设置和 connections
数组)。
$return = Elasticsearch::connection('connectionName')->index($data);
希望使用 Facades 的 Lumen 用户可以通过编辑 bootstrap/app.php
文件来执行此操作
$app->withFacades(true, [ ... GNAHotelSolutions\LaravelElasticsearch\Facade::class => 'Elasticsearch', ... ]);
不使用 Facades 的 Lumen 用户需要使用依赖注入或应用程序容器来获取 ES 服务对象
// using injection: public function handle(\GNAHotelSolutions\LaravelElasticsearch\Manager $elasticsearch) { $elasticsearch->ping(); } // using application container: $elasticSearch = $this->app('elasticsearch');
当然,依赖注入和应用程序容器也适用于 Laravel 应用程序。
高级使用
由于此包是围绕官方 Elastic 客户端构建的包装器,因此您可以使用此包做很多事情。您不仅可以执行标准 CRUD 操作,还可以以编程方式监控您的 Elastic 集群的健康状况、备份它或对其进行更改。一些操作是通过 "命名空间" 命令完成的,此包支持这些命令。
获取索引的统计数据
$stats = Elasticsearch::indices()->stats(['index' => 'my_index']); $stats = Elasticsearch::nodes()->stats(); $stats = Elasticsearch::cluster()->stats();
创建和恢复快照(首先阅读有关创建存储库路径和插件的Elastic文档)
$response = Elasticsearch::snapshots()->create($params); $response = Elasticsearch::snapshots()->restore($params);
删除整个索引(请注意!)
$response = Elasticsearch::indices()->delete(['index' => 'my_index']);
请记住,此包是对大量复杂且文档齐全的Elastic功能的薄包装。有关这些功能及其调用方法和参数的信息,请参阅Elastic文档。有关使用这些功能的帮助,请通过Elastic论坛和类似Stack Overflow的网站获得。
控制台命令
此包还提供了一些有用的控制台命令。
检查索引是否存在
php artisan laravel-elasticsearch:utils:index-exists <your_elasticsearch_index_name>
创建索引
php artisan laravel-elasticsearch:utils:index-create <your_elasticsearch_index_name>
删除索引
php artisan laravel-elasticsearch:utils:index-delete <your_elasticsearch_index_name>
创建或更新索引映射
注意:索引映射文件必须包含Elasticsearch期望的有效的JSON映射定义,例如
{ "body": { "_source": { "enabled": true }, "properties": { "id": { "type": "keyword" }, "property_1": { "type": "text" }, "property_2": { "type": "text" } } } }
php artisan laravel-elasticsearch:utils:index-create-or-update-mapping <your_elasticsearch_index_name> <json_mapping_absolute_file_path>
创建别名
php artisan laravel-elasticsearch:utils:alias-create <your_elasticsearch_index_name> <your_elasticsearch_alias_name>
从别名中删除索引
php artisan laravel-elasticsearch:utils:alias-remove-index <your_elasticsearch_index_name> <your_elasticsearch_alias_name>
在别名上切换索引(对于新索引的无停机发布很有用)
php artisan laravel-elasticsearch:utils:alias-switch-index <your_NEW_elasticsearch_index_name> <your_OLD_elasticsearch_index_name> <your_elasticsearch_alias_name>
错误、建议、贡献和支持
感谢所有为这个项目做出贡献的人!
特别感谢JetBrains及其开源许可计划...以及当然,出色的PHPStorm IDE!
请使用Github来报告错误,发表评论或建议。
有关如何贡献更改,请参阅CONTRIBUTING.md。
版权和许可证
laravel-elasticsearch是由Colin Viebrock编写的,并发布在MIT许可下。
版权(c)2015 Colin Viebrock