nailfor/elasticsearch
Composer 安装命令:
composer require nailfor/elasticsearch
包简介
Eloquent like library for Elasticsearch
关键字:
README 文档
README
Elasticsearch client for Eloquent ORM
Installation
The preferred way to install this extension is through composer.
Either run
composer require nailfor/elasticsearch
or add
"nailfor/elasticsearch" : "*"
to the require section of your application's composer.json file.
Usage
Add config/app.php
'providers' => [
...
nailfor\Elasticsearch\ElasticsearchServiceProvider::class,
and config/database.php
'connections' => [
...
'elasticsearch' => [ //the name of connection in your models(default)
'driver' => 'elasticearch',
'config' => [
'BasicAuthentication' => [
env('ELASTICSEARCH_LOGIN', ''),
env('ELASTICSEARCH_PASSWORD', ''),
],
'hosts' => [
env('ELASTICSEARCH_HOST', 'localhost:9200'),
],
'retries' => 1,
],
],
Example model
namespace App\Models\db\elastic;
use nailfor\Elasticsearch\Eloquent\Model;
class esSearch extends Model
{
//protected $connection = 'elasticsearch'; //(default)
//your index name
protected $table='index';
}
Example select
esSearch::where('field', 'somedata')
->whereBetween('@timestamp', [$startDate, $endDate])
->whereIn('data.field', [1,3,4])
->whereFiledExist('some.field')
->whereFiledNotExist('another.field')
->get();
!!!ATTENTION!!! After v0.17.0 groups returns the query result!
Example groups
esSearch::where('field.data', 'somedata')
//group name "group" by field "data.field" without subgroups
->groupBy(['group'=>'data.field'])
//group name "some_field" by field "some_field"
->groupBy('some_field')
//subgroup name "another" by field "another" and subgroup name "diff" and field "diff.name"
->groupBy('some_field', ['another', 'diff'=>'field.diff'])
//another style
->groupBy(['grp'=>'field'], ['subgrp'=>'sub.field'])
esSearch::where('field.data', 'somedata')
//group name "group" by field "data.field" without subgroups with limit 10 items
->groupBy(['group' => 'data.field'])
->limit(10)
//supported closure
esSearch::groupBy(['aggregation_name' => 'field.data'], fn ($query) => $query
->groupBy('some_field', fn ($subQuery) => $subQuery
->groupBy(['agg3' => 'another.field'])
)
)
Range aggregations
//group field "price" by 3 group: <1000, 1000-2000 and >2000
->groupByRange('price', ['ranges'=>[['to' => '1000'], ['from' => '1000', 'to' => 2000], ['from'=>2000]]])
Date aggregations
//group "group" by 2 dates: before NOW-1Day and after. This is NOT filter, this is group by condition!
//all groups with name "group" will be merged
->groupByDateRange(['group'=>'date.field'], ['ranges'=>['to' => 'now-1y/d', 'from' => 'now-1y/d']])
Interval aggregations
//group "date.field" by interval "hour"
->groupByInterval(['graph'=>'date.field'], ['interval' => 'hour'])
Average aggregations
//group and calculate average value
->groupBy(['groupName' => 'field_for_group'])
->groupByAverage('groupName', ['field' => 'field.name'])
Min/max aggregations
->groupByMin('groupName', ['field' => 'field.name'])
->groupByMax('groupName', ['field' => 'field.name'])
Sum aggregations
//group and calculate sum
->groupBy(['groupName' => 'field_for_group'])
->groupBySum('groupName', ['field' => 'field.name'])
Nested aggregations
//simple group 'nested_field' same of 'nested_field'
esSearch::groupByNested('nested_field')
//group name 'nested_group' by field 'nested.field'
->groupByNested(['nested_group' => 'nested.field'])
//By closures
->groupByNested(['nested_group' => 'nested.field'], fn ($query) => $query
->groupBy('some_field', fn ($subQuery) => $subQuery
->groupBy(['agg3' => 'another.field'])
)
)
Stats aggregations
esSearch::groupByStats('some_field')
// or
->groupByStats(['group_name' => 'some_field'])
//You can combine them together
esSearch::groupByNested('nested_field', fn($query) =>
$query->groupBy(['group' => 'field.id'], fn($query) => $query
->groupByStats(['values' => 'properties.value'])
)
)
Example fuzziness
$query = esSearch::query($searchString, [
'fuzziness' => 1,
]);
$collection = $query->get();
Example scroll API
$query = esSearch::scroll([
'scroll' => '1m',
]);
$collection = $query->get(); //first 10k(max) records
$collection = $query->get(); //next 10k(max) records...
or
esSearch::scroll([
'scroll' => '1m',
])
->chunk(1000, function ($collection) {
...
});
Example suggest request
//clear
esSeartch::where('model', 'short')
->suggest('my-suggest')
->get();
//closure
esSeartch::suggest('my-clossure', fn ($query) => $query->where('color', 'black'))
->get();
//mix
$query = esSeartch::select([
'brand',
'name',
])
->query($searchString, [
'minimum_should_match'=> '50%',
'fuzziness' => 'auto',
])
->suggest('my-1', fn ($query) => $query->where('size', 'xxl'))
->suggest('my-2', fn ($query) => $query->where('color', 'black'))
->suggest('my-3') //do nothing because there no "where" section
;
$collection = $query->get();
Example nested request
$query = esSeartch::select([])
->where('category.id', 2) //Attention!
->nested('category') //Condition category.id = 2 will be removed from the main body
->where('price', 100) //But this condition will be added to the main body
//also u can combine simple and complicated nested requests
->nested('brand', fn ($subQuery) => $subQuery //$subQuery doesn't consist any conditions from the body
->whereIn('brand.id', ['Nike', 'Sony', 'LG'])
->nested('color', fn($subSubQuery) => $subSubQuery //Also $subSubQuery doesn't consist conditions
->whereIn('color.group', ['red', 'green']) //Both of them doesn't affected the body condition 'price = 100'
)
)
->orNested('property', fn(subQuery) => $subQuery
->where('property.value', 'milked')
)
Example bulk insert
$records = [
[
'_id' => 1, //This is field MUST be into recordset, otherwise will be set uniqid()
'field1' => 'data1',
'field2' => 'data2',
],
[
'_id' => 2,
'field1' => 'some data1',
'field2' => 'some data2',
],
];
esSeartch::insert($records);
Example post_filter
$query = esSearch::groupBy(['group' => 'field.id'])
->postFilter(fn($query) => $query
->where('field', 'value')
->nested('category')
)
Debug
$query = esSearch::query() ->dd(true) //default false ;
Credits
License
The GNU License (GNU). Please see License File for more information.
nailfor/elasticsearch 适用场景与选型建议
nailfor/elasticsearch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.44k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「elasticsearch」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nailfor/elasticsearch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nailfor/elasticsearch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nailfor/elasticsearch 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
simple api library.
Symfony bundle for Elasticsearch integration with round-robin load balancing
统计信息
- 总下载量: 3.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-21