triadev/laravel-elasticsearch-dsl 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Software license Travis Coveralls CodeCov

Scrutinizer Code Quality Code Coverage Build Status

Latest stable Latest development Monthly installs

Supported laravel versions

Laravel 5.5 Laravel 5.6 Laravel 5.7

Supported elasticsearch versions

Elasticsearch 6.0 Elasticsearch 6.1 Elasticsearch 6.2 Elasticsearch 6.3 Elasticsearch 6.4

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.

https://laravel.com/docs/5.7/packages

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

  1. docker-compose -f docker-compose.yml up
  2. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 triadev/laravel-elasticsearch-dsl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 89
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-24