triadev/laravel-elasticsearch-dsl
Composer 安装命令:
composer require triadev/laravel-elasticsearch-dsl
包简介
A service provider for laravel with a fluent elasticsearch query and aggregation dsl.
关键字:
README 文档
README
A service provider for laravel with a fluent elasticsearch query and aggregation dsl.
Supported laravel versions
Supported elasticsearch versions
Main features
- Query (TermLevel, Fulltext, Geo, Compound, Joining, Specialized, InnerHit)
- Aggregation (Bucketing, Metric, Pipeline)
- Suggestion
Installation
Composer
composer require triadev/laravel-elasticsearch-dsl
Application
The package is registered through the package discovery of laravel and Composer.
Once installed you can now publish your config file and set your correct configuration for using the package.
php artisan vendor:publish --provider="Triadev\Es\Dsl\Provider\ServiceProvider" --tag="config"
This will create a file config/laravel-elasticsearch-dsl.php.
Configuration
| Key | Env | Value | Default |
|---|---|---|---|
| index | LARAVEL_ELASTICSEARCH_DSL_INDEX | STRING | default_index |
| metrics.enabled | LARAVEL_ELASTICSEARCH_DSL_METRICS | BOOL | false |
| metrics.buckets.search | --- | ARRAY | array of buckets in milliseconds |
| metrics.buckets.suggest | --- | ARRAY | array of buckets in milliseconds |
Metrics
Metrics are generated with the package: LaravelPrometheusExporter
Detailed configuration options are documented in the readme of the package.
The following metrics are generated as long as metrics.enabled = true:
Namespace: triadev_laravel_elasticsearch_dsl
Histogram
| Name | Handler | Description |
|---|---|---|
| query_duration_milliseconds | search | execution time of search query |
| query_duration_milliseconds | suggest | execution time of suggestion query |
Usage
This package offers a dsl for elasticsearch. The entry point for each query / aggregation is a facade.
Triadev\Es\Dsl\Facade\ElasticDsl;
Each query / aggregation returns an object containing the search result and aggregation.
Triadev\Es\Dsl\Model\SearchResult
int: time needed to execute the query $result->took(); bool $result->timedOut(); float $result->maxScore(); int: number of matched documents $result->totalHits(); Illuminate\Support\Collection: collection of searchable eloquent models $result->hits(); array|null $result->aggregation();
Bool
For every query that is based on bool, the bool status can be changed.
Default bool state: must
ElasticDsl::search()->termLevel() ->must() ->term('FIELD', 'VALUE') ->mustNot() ->term('FIELD', 'VALUE') ->should() ->term('FIELD', 'VALUE') ->filter() ->term('FIELD', 'VALUE') })->get()
Nested bool query
A nested query is realized with bool(\Closure $closure).
ElasticDsl::search() ->termLevel() ->term('FIELD', 'VALUE') ->bool(function (Search $search) { $search->termLevel() ->term('FIELD', 'VALUE') ->bool(function (Search $search) { $search->fulltext() ->match('FIELD1', 'QUERY1') ->matchPhrase('FIELD2', 'QUERY2'); }); }) ->prefix('FIELD', 'VALUE') ->get(); -------------------------------------------------- [ "query" => [ "bool" => [ "must" => [ [ "term" => [ "FIELD" => "VALUE" ] ], [ "bool" => [ "must" => [ [...], [...] ] ] ], [ "prefix" => [ "FIELD" => [ "value" => "VALUE" ] ] ] ] ] ] ]
TermLevel
matchAll, exists, fuzzy, ids, prefix, range, regexp, term, terms, type, wildcard
ElasticDsl::search()->termLevel()->filter()->term('FIELD', 'VALUE')->get();
Fulltext
match, matchPhrase, matchPhrasePrefix, multiMatch, queryString, simpleQueryString, commonTerms
ElasticDsl::search()->fulltext()->must()->match('FIELD', 'QUERY')->get();
Geo
geoBoundingBox, geoDistance, geoPolygon, geoShape
ElasticDsl::search()->geo()->filter()->geoDistance('FIELD','10km', new Location(1, 2))->get();
Compound
functionScore, constantScore, boosting, disMax
ElasticDsl::search()->compound()->functionScore( function (Search $search) { $search->termLevel()->term('FIELD1', 'VALUE1'); }, function (FunctionScore $functionScore) { $functionScore->simple([]); } )->get();
Joining
nested, hasChild, hasParent
ElasticDsl::search()->joining()->nested('PATH', function (Search $search) { $search->termLevel()->filter()->term('FIELD', 'VALUE'); })->get();
Specialized
moreLikeThis
ElasticDsl::search()->specialized()->moreLikeThis('LIKE')->toDsl();
InnerHit
nestedInnerHit, parentInnerHit
ElasticDsl::search()->nestedInnerHit('NAME', 'PATH', function (Search $search) { $search->termLevel()->term('FIELD', 'VALUE'); })->get();
Individual index and type
To set an individual index or type per query you have two overwrite methods.
ElasticDsl::search() ->overwriteIndex('INDEX') ->overwriteType('TYPE') ->termLevel() ->matchAll() ->get();
Aggregation
Bucketing, Metric, Pipeline
ElasticDsl::search()->aggregation(function (Aggregation $aggregation) { $aggregation->metric(function (Aggregation\Metric $metric) { ... }); })->get(); ElasticDsl::search()->aggregation(function (Aggregation $aggregation) { $aggregation->bucketing(function (Aggregation\Bucketing $metric) { ... }); })->get(); ElasticDsl::search()->aggregation(function (Aggregation $aggregation) { $aggregation->pipeline(function (Aggregation\Pipeline $metric) { ... }); })->get();
Suggestions
term, phrase, completion
ElasticDsl::suggest()->term('NAME', 'TEXT', 'FIELD')->get();
Reporting Issues
If you do find an issue, please feel free to report it with GitHub's bug tracker for this project.
Alternatively, fork the project and make a pull request. :)
Testing
- docker-compose -f docker-compose.yml up
- composer test
Contributing
Please see CONTRIBUTING for details.
Credits
Other
Project related links
License
The code for laravel-elasticsearch-dsl is distributed under the terms of the MIT license (see LICENSE).
triadev/laravel-elasticsearch-dsl 适用场景与选型建议
triadev/laravel-elasticsearch-dsl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 89 次下载、GitHub Stars 达 6, 最近一次更新时间为 2018 年 12 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「DSL」 「elasticsearch」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 triadev/laravel-elasticsearch-dsl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 triadev/laravel-elasticsearch-dsl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 triadev/laravel-elasticsearch-dsl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Feature complete, object oriented, composable, extendable Elasticsearch query DSL builder for PHP.
Elasticsearch DSL library
simple api library.
Expression executor, which allow to implement domain-specific language
DSL for writing sniffs for PHP_CodeSniffer
Symfony bundle for Elasticsearch integration with round-robin load balancing
统计信息
- 总下载量: 89
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-24