承接 php-module/elastic-php-based-official 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

php-module/elastic-php-based-official

Composer 安装命令:

composer require php-module/elastic-php-based-official

包简介

README 文档

README

为什么写

由于php版本es操作依赖httpAPI来操作数据, 并且官方的API需要仔细研读方可使用,  这里出现了二封版本的MySQL 操作类

详细使用方法见

  • tests/test.php

支持的API

filter{geo where and or in "not in" > >= < <= between not_between = != geo geoBox}  各种where条件
search 直接走了multi_match; 
        搜索类型: 
            phrase: 短语匹配, 
            phrase_prefix: 前缀匹配
aggregations  聚合支持嵌套聚合, 支持多聚合

不支持的API

  • 目暂不支持function_score, bucket, scroll
  • 全部的查询都将被转换为: boolQuery
  • 本类适合进行筛选, 不合适做评分

使用:

项目内: composer install

引入: composer require php-module/elastic-php-based-official

use PhpES\EsClient\Client;
use PhpEs\EsClient\DSLBuilder;

$es = new Client();
/**
 * matchType 可选项:
 * phrase 短语匹配
 * phrase_prefix 前缀匹配
 * cross_fields(出现在越多的字段得分越高)
 * best_fields(匹配度越高得分越高)
 * most_fields(出现频率越高得分越高)
*/
$res = $es
    ->select(array('field', 'field2'))
    ->from('index')
    ->where('field', DSLBuilder::OPERATOR_EQ, 'value')
    ->whereGeo('geo_point', $lat, $lon, $distance, $minDistance, $unit, $distanceType, $type)
    ->orWhere('field', DSLBuilder::OPERATOR_NE, value8)
    ->andWhereBegin()
    ->orWhere('field', DSLBuilder::OPERATOR_EQ, value)
    ->orWhere('field', DSLBuilder::OPERATOR_NE, value)
    ->orWhere('field', DSLBuilder::OPERATOR_NE, value)
    ->match(要匹配的字段(单个字段或数组), "关键词", $matchType = 'phrase', $type = 'must')
    ->andWhereEnd()
    ->orderBy('field', 'value')
    ->offset($offset)
    ->limit($limit)
    ->debug() //if need
    ->search()
    ->getFormat();

聚合

  • 现在聚合支持多聚合了~
  • 多重聚合被支持了
  • 嵌套nested被支持了
$res = $es
    ->select(array('field', 'field2'))
    ->from('index')
     ->beginNested()
    ->setNested('parentField') // 标记嵌套文档聚合开始
    ->where('parentField.field', '!=', 0)
    ->dateHistogram('parentField.filed1', 'year', \PhpES\EsClient\DSLBuilder::AGG_HISTOGRAM_TYPE_CALENDAR, $child)
    ->endNested() // 标记嵌套文档结束
    ->groupBy('field') // 普通聚合 返回值中将自动拼接上 agg_字段名 
    ->sum(field) // 获取参与聚合的文档数量 自动拼接 count_字段名
    ->cardinality('field') // 获取不重复的符合条件的总数 自动拼接 cardinality_字段名
    ->sum('field') // 获取sum
    ->search()
    ->getFormat();

单例模式中不使用连接池

# 继承Client, 重写getClient, 实例化Client时用: setHandler来设置链接数量
class classNmae extends Client{

    /**
     * @throws \Exception
     */
    public function getClient()
    {
        if (empty($this->hosts)) {
            throw new \Exception('using getClient , You must set host before');
        }
        if ($this->client == null) {
            $this->client = self::create()
                ->setHosts($this->hosts)
                ->setHandler(new CurlHandler(['max_handles' => 0]))
                ->build();
        }
        return $this->client;
    }
}

使用连接池,从这里复制了一份

https://github.com/hyperf-ext/elasticsearch

    <?php
        use PhpES\EsClient\Client;
        use Hyperf\Utils\ApplicationContext;
        $client = ApplicationContext::getContainer()->get(ClientFactory::class)->create();
        $info = $client->someFunction();

不使用hyperf

  • 检出此项目,在compose.json中将第四行替换为:
elasticsearch/elasticsearch:7.15.0, 或你对应的版本
  • 然后如下使用
    $c  = [
        'hosts'   => [
            'http://host:port',
        ],
        'retries' => 1,
    ];
    
    $es = new Client();
    $es->setHost($c);
    $res = $es->from('test')
        ->sum('field')
        ->debug()
        ->search();

php-module/elastic-php-based-official 适用场景与选型建议

php-module/elastic-php-based-official 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.19k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 php-module/elastic-php-based-official 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-10-19