glodzienski / phenix
AWS Elasticsearch Service for Laravel/Lumen
Requires
- php: >=7.0
- aws/aws-sdk-php: ~3.0
- elasticsearch/elasticsearch: ~6.0
- nesbot/carbon: ^2.29.1
This package is auto-updated.
Last update: 2022-09-17 23:43:34 UTC
README
AWS Elasticsearch Service for Laravel/Lumen
注意:此软件包仅提供搜索、聚合和索引事务。除此之外,您可以参考Elasticsearch官方文档。 注意:此软件包基于elegisandi/aws-elasticsearch-laravel
安装
composer require glodzienski/aws-elasticsearch-laravel
Laravel/Lumen集成
-
将服务提供者添加到您的
config/app.php提供者中glodzienski\AWSElasticsearchService\ElasticSearchServiceProvider::class -
将外观添加到您的
config/app.php别名中'ElasticSearch' => glodzienski\AWSElasticsearchService\Facades\ElasticSearch::class -
在您的
.env文件中设置AWS凭证和Elasticsearch配置AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_REGION ELASTICSEARCH_ENDPOINT ELASTICSEARCH_PORT ELASTICSEARCH_SHARDS ELASTICSEARCH_REPLICAS ELASTICSEARCH_DEFAULT_INDEX ELASTICSEARCH_DEFAULT_TYPE ELASTICSEARCH_DEFAULT_TIME_FILTER_FIELD如果您已经在使用aws elasticsearch服务,请设置
AWS_ELASTICSEARCH_SERVICE=true
如果您想配置Elasticsearch映射、设置和/或默认类型和索引,只需运行
php artisan vendor:publish --provider=glodzienski\\AWSElasticsearchService\\ElasticSearchServiceProvider
对于Lumen
-
将服务提供者注册到您的
bootstrap/app.php$app->register(glodzienski\AWSElasticsearchService\ElasticSearchServiceProvider::class);
基本用法
使用外观
<?php
namespace App;
use ElasticSearch;
public function index() {
extract(ElasticSearch::setSearchParams(request()));
$clicks = [];
$total = 0;
if ($hits = ElasticSearch::search($query, $options, $date_range)) {
$clicks = $hits['hits']['hits'];
$total = $hits['hits']['total'];
}
}
对于Lumen
<?php
namespace App;
public function index() {
extract(app('elasticsearch')->setSearchParams(request()));
$clicks = [];
$total = 0;
if ($hits = app('elasticsearch')->search($query, $options, $date_range)) {
$clicks = $hits['hits']['hits'];
$total = $hits['hits']['total'];
}
}
控制台命令
-
创建索引 (创建默认索引)
php artisan elasticsearch:create-index要重置现有索引,
php artisan elasticsearch:create-index --reset -
更新索引映射 (更新默认索引映射)
php artisan elasticsearch:update-index-mapping仅支持新属性更新。
可用方法
-
aggregations(
array $aggs,array $query = [],array $options = [],$type,$index)$aggs : 必须遵循Elasticsearch文档中指定的结构。
$query : 查看
search方法中的$query参数$options : 查看
search方法中的$options参数返回
Array -
search(
array $query = [],array $options = [],array $range = [],$type,$index)$query : 任何可用属性的键值对数组
$options : 包含以下键值对的数组:
from、size、sort$range : 范围查询的数组表示
返回
Array -
count(
array $query = [],array $range = [],$type,$index)a (语法糖) 方法,当搜索结果为零时使用
返回
Int -
setSearchParams(
Request $request,array $defaults = [],$type)通过查询字符串设置搜索参数的可选且常用方法
$request:一个
\Illuminate\Http\Request的实例,使用中的查询变量range,见getDateRange方法start,一个有效的日期字符串end,一个有效的日期字符串sort,一个映射属性order,值是desc或ascsize,要返回的总结果数(最大10000)
$defaults:以下键值对的数组:
sort, order, size返回
Array -
getDateRange(
$range,$format = null)$range:预定义的日期范围值:
today, yesterday, last-7-days, this-month, last-month, last-2-months, last-3-months$format必须是有效的日期格式,默认为
null,将返回一个DateTime实例返回
Array -
setAggregationDailyDateRanges(
$start,$end,$format = null)$format必须是有效的日期格式,默认为
null,将返回一个DateTime实例返回
Array -
defaultAggregationNames
返回
Array -
defaultIndex
返回
String -
defaultType
返回
String -
defaultTimeFilterField
返回
String -
setSearchQueryFilters(
Collection $query,array $bool_clauses = [],$type = null)返回
Array -
setBoolQueryClause(
Collection $query,array $properties,$context,$occur,callable $callback = null)返回
Array -
getMappingPropertiesByDataType(
Collection $properties,$data_type)返回
Array -
getMappingProperties(
$type = null)返回
Collection -
indexDocument(
array $body,$type = null,$index = null)返回
Array -
getDocument(
$id,$type,$index)返回
Array -
updateDocument(
array $fields,$id,$type = null,$index = null)返回
Array -
deleteDocument(
$id,$type = null,$index = null)返回
Array -
getSettings(
$index = null)返回
Array -
updateSettings(
array $settings,$index)返回
Array -
getMappings(
$index, $type)返回
Array -
updateMappings(
array $properties,$type,$index)返回
Array -
createIndex(
array $mappings,array $settings,$index) -
getIndex($index = null)
返回
Boolean -
deleteIndex(
$index)返回
Array
注意:Elasticsearch 客户端的所有方法现在都已支持。
限制
-
在 search 方法中支持的数据类型有
- keyword
- text
- array
- integer
- boolean
- ip
贡献
首先打开一个 issue 来讨论潜在的变化/添加。