定制 payamjafari/elasticute 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

payamjafari/elasticute

Composer 安装命令:

composer require payamjafari/elasticute

包简介

README 文档

README

By using this builder you can easily make your database queries without writing raw queries.

Installation

composer require payamjafari/elasticute

Usage

Just call method "query" in QueryBuilder class and you are ready.

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$cards = QueryBuilder::query()->index( 'cards' )->get();

// use $cards for your needs

Filter query

For filtering your query you can use several methods such as where, whereNot, orWhere, ....

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$cards = QueryBuilder::query()
    ->index( 'cards' )
    ->whereEqual( 'name', 'foo' )
    ->whereNotEqual( 'count', 10 )
    ->get(); // limit documents count by giving a number to "get". Example: get(10);

// use $cards for your needs

Filter Methods

Name Description
where(string $name, $value, string $operator, array $extra) Matches values that is not a specified value.
whereNot(string $name, $value, string $operator, array $extra) Matches values that is a specified value.
whereContains( string $name, $value ) Matches values that are contains a specified value.
whereNotContains( string $name, $value ) Matches values that are not contains a specified value.
whereEqual( string $name, $value ) Matches values that are equal to a specified value.
whereNotEqual( string $name, $value ) Matches all values that are not equal to a specified value.
whereExists( string $name ) Matches documents that have the specified field.
whereNotExists( string $name ) Matches documents that dont have the specified field.

Group Filter

You can set your filters as a group by just adding a closure to "where" method.

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$cards = QueryBuilder::query()
    ->index( 'cards' )
    ->groupShould( function( QueryBuilder $builder ){
        $builder->whereEqual( 'name', 'payam' );
        $builder->whereNotEqual( 'name', 'kourosh' );
        $builder->groupMust( function( QueryBuilder $builder ){
            $builder->whereExists( 'lastname' );
            $builder->whereTextContains( 'name', 'kourosh2' );
        } );
    } )
    ->whereTextNotContains( 'lastname', 'weber' )
    ->get();

// use $cards for your needs

Group Filter Methods

Name Description
groupShould( callable $filters ) See Official Documentation
groupMust( callable $filters ) See Official Documentation
groupMustNot( callable $filters ) See Official Documentation
groupFilter( callable $filters ) See Official Documentation

Aggregations

Aggregations are so important and could be used anywhere. So you can use it very easy. Take a look at the example.

<?php

use ElastiCute\ElastiCute\Aggregation\AggregationBuilder;
use ElastiCute\ElastiCute\QueryBuilder;

$cards = QueryBuilder::query()
    ->index( 'cards' )
    ->aggregate( function( AggregationBuilder $query ){
        $query->make('like_count')->avg()->field('like_count');
        $query->make('dislike_count')->avg()->field('dislike_count');
    } )
    ->whereTextNotContains( 'lastname', 'weber' )
    ->get();

// response will include $cards['aggregations']

Aggregation methods

Name Description
avg() Aggregate based on 'avg' type
terms() Aggregate based on 'terms' type
histogram() Aggregate based on 'histogram' type
max() Aggregate based on 'max' type
min() Aggregate based on 'min' type
Note: More aggregations will be added over time :)

Response

Each Request has a response, then it must be processed.

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$cards = QueryBuilder::query()
    ->index( 'cards' )
    ->get()
    ->toArray();

$cards = QueryBuilder::query()
    ->index( 'cards' )
    ->get()
    ->map( function( $value ) {
        // Do some stuff with $value
    } );

// use $cards for your needs

Response Methods

Name Description
toArray() Returns as array.
toJson() Returns as json.
toList() Returns as array list (if its a search query).
map() Map through the results(if its a search query)

Sort / OrderBy

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$cards = QueryBuilder::query()
    ->index( 'cards' )
    ->sort( [
        'name' => [
            'order' => 'desc'
        ]
    ] )
    ->get();

// use $cards for your needs

Paginate documents

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$cards = QueryBuilder::query()
    ->index( 'cards' )
    ->paginate( 10, 1 ); // paginate( int $document_per_page = 10, int $current_page = 1 )

// use $cards for your needs

Select specific fields

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$cards = QueryBuilder::query()
    ->index( 'cards' )
    ->select( [ 'name', 'size' ] )
    ->get();

// use $cards for your needs

Select index at runtime

You can select your index at the query by calling "index" method.

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$cards = QueryBuilder::query()
    ->index( 'cards' )
    ->whereEqual( 'name', 'foo' ) // your filters come here before delete
    ->get();

Find by id

By calling method "find", you can retrieve the document based on id.

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$card = QueryBuilder::query()
    ->index( 'cards' )
    ->find( 'wDCoxXUBA0stnoJxvwdR', true ); // find( $id, bool $get_only_source = true )

Get index mapping

By calling method "mapping", you can retrieve the index mapping.

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$card = QueryBuilder::query()
    ->index( 'cards' )
    ->mapping();

Pro tip :)

You dont have to call "query" method first. you can directly call index right at the beginning.

<?php

use ElastiCute\ElastiCute\QueryBuilder;

$card = QueryBuilder::index( 'cards' )
    ->find( 'wDCoxXUBA0stnoJxvwdR', true ); // find( $id, bool $get_only_source = true )

$cards = QueryBuilder::index( 'cards' )
    ->whereEqual( 'name', 'foo' )
    ->get();

payamjafari/elasticute 适用场景与选型建议

payamjafari/elasticute 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 226 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「nosql」 「elasticsearch」 「querybuilder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 payamjafari/elasticute 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 payamjafari/elasticute 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-14