inkime/elasticsearch-query-builder
Composer 安装命令:
composer require inkime/elasticsearch-query-builder
包简介
a query builder component of elasticsearch
README 文档
README
提供了更贴近Yii2模型操作的API来查询ES网关数据
Composer安装:
composer require inkime/elasticsearch-query-builder
支持API如下:
- query
- one
- all
- count
- exists
- index
- select / addSelect
- aggs / addAggs
- aggregations
- indexBy
- dsl
- map
- addMap
- highlight
- collapse
- where
- andWhere
- orWhere
- filterWhere
- andFilterWhere
- orFilterWhere
- orderBy / addOrderBy
- offset
- limit
自定义日志操作:
<?php
namespace webapi\es;
use inkime\elasticsearch\ActiveRecord;
use inkime\elasticsearch\Query;
use webapi\services\LogService;
class SdModel extends ActiveRecord
{
public static $gateway = 'xxx';
public static $authorization = 'xxx';
public function logRecord($request, $response)
{
// 使用1,记录Log日志
$logFile = \Yii::getAlias('@runtime/logs/es.log');
file_put_contents($logFile, var_export($request, true) . PHP_EOL, FILE_APPEND);
file_put_contents($logFile, var_export($response, true) . PHP_EOL, FILE_APPEND);
// 使用2,持久化存DB
(new LogService())->saveSysLog(var_export($request, true));
}
}
常规查询
$result = EsModel::find()->index('wx')->query();
$count = $result['count'];
$list = $result['list'];
单条记录
EsModel::find()->index('wx')->one();
多条记录
EsModel::find()->index('wx')->offset(0)->limit(10)->all();
获取总数
EsModel::find()->index('weibo')->offset(0)->limit(5)->count();
exists
EsModel::find()->index('wx')->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])->exists();
index
可任意切换索引库,如wx/weibo等
EsModel::find()->index('wx')->one();
select / addSelect
指定查询字段,支持追加操作,参数支持数组或字符串 #逗号分割
EsModel::find()->index('wx')->select('news_uuid')->addSelect(['news_title', 'news_sim_hash'])->one();
aggs / addAggs
注意:只能用query查询
EsModel::find()->index('wx')
->select('news_uuid,news_title,news_posttime,platform')
->aggs('platformCount', 'terms', ['field' => 'platform', 'size' => 3, 'order' => ['_count' => 'asc']])
->addAggs('dateDayCount', 'date_histogram',
['field' => 'news_posttime', 'interval' => 'day', 'format' => 'yyyy-MM-dd', 'min_doc_count' => 0])
->limit(5)
->query();
aggregations
自定义聚合查询
EsModel::find()->index('wx')->select('news_is_origin')
->addSelect(['news_reposts_count', 'news_comment_count', 'news_like_count'])
->aggregations([
'group' => [
"terms" => ["field" => 'news_is_origin']
],
'aggs' => [
'news_reposts_count' => ['sum' => ['field' => 'news_reposts_count']],
'news_comment_count' => ['sum' => ['field' => 'news_comment_count']],
'news_like_count' => ['sum' => ['field' => 'news_like_count']],
]
])
->limit(2)
->query();
indexBy
EsModel::find()->index('wx')->select('news_uuid')->indexBy('news_uuid')->limit(5)->all();
支持匿名函数
$field = 'news_uuid';
$secretStr = 'qingbo#$%t2000';
EsModel::find()->index('wx')->select($field)->indexBy(function($v) use ($field, $secretStr) {
// $v 表示每条记录
return $v[$field] . $secretStr;
})->limit(5)->all();
dsl
输出DSL语句,支持json/array两种格式,默认json
EsModel::find()->index('wx')->select('news_uuid')
->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])->dsl()
->one();
map
自定义DSL语句,map或者addMap操作仅仅支持bool查询,bool键名支持省略
例如:['must' => [['match' => ['news_title' => '补贴']]]]
系统会补全:['bool' => ['must' => [['match' => ['news_title' => '补贴']]]]]
使用1:
EsModel::find()->index('wx')->select('news_is_origin')->addSelect('news_uuid')
->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])
->map([
'bool' => [
'filter' => [
'exists' => ['field' => 'news_is_origin']
]
]
]) // 自定义DSL
->one();
使用2,支持匿名函数:
$esModel->andWhere(['not', ['media_name' => '']])->map(function () use ($index) {
return ['must' => [['term' => ['news_uuid' => $index . '0adb6d83ab00b225c370c0e957f658a8']]]];
})
addMap
自定义DSL语句
EsModel::find()->index('wx')->select('news_title,news_uuid')
->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])
->addMap(['must' => [['match' => ['news_title' => '补贴']]]])
->query();
highlight
自定义高亮配置
public static function highLight($fields = [], $pre_tags = '<em>', $post_tags = '</em>')
{
$newFieldsArr = [];
foreach ($fields as $field) {
$newFieldsArr[$field] = ['number_of_fragments' => 0];
}
return [
"require_field_match" => false,
"pre_tags" => [
$pre_tags
],
"post_tags" => [
$post_tags
],
"fields" => $newFieldsArr
];
}
EsModel::find()->index('wx')->select('news_title')->addSelect('news_uuid')
->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])
->highlight(EsModel::highLight([$field])) // 高亮配置
->query(); // 仅支持query查询
collapse
EsModel::find()->index('wx')->select('news_uuid')->collapse('news_title')
->where(['media_name' => '城镇城镇交费'])->all();
where查询
EsModel::find()->index('wx')->select('news_uuid')
->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])->one();
whereNot
EsModel::find()->index('wx')->select('news_uuid')
->where([
'not',
['news_uuid' => ['b15e02a0bddacc0ee61d51d36d0022eb', 'e698094102c12deb978e35617e72b633']]
])->one();
whereAnd
EsModel::find()->index('wx')->select('news_uuid,media_name')
->where(['and', ['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb','media_name' => '城镇城镇交费']])
->one();
whereOr
EsModel::find()->index('wx')->select('news_uuid')
->where(['or', ['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'], ['media_name' => '城镇城镇交费']])
->one();
whereBetween
EsModel::find()->index('wx')->select('news_is_origin')->where(['between', 'news_is_origin', 0, 1])
->one();
whereNotBetween
EsModel::find()->index('wx')->select('news_is_origin')
->where(['not between', 'news_comment_count', 10, 100])->one();
whereIn
EsModel::find()->index('wx')->select('media_name')
->where(['in', 'media_name', ['爆笑短片', '智慧人生', '', null]])->one();
whereNotIn
EsModel::find()->index('wx')->select('media_name')
->where(['not in', 'media_name', ['爆笑短片', '智慧人生', '', null]])->one();
whereRange
EsModel::find()->index('wx')->select('news_postweek_day')->where(['<', 'news_postweek_day', 3])
->orderBy('news_postweek_day desc')->one();
这里操作符支持:<、lt、<=、lte、>、gt、>=、gte
andWhere
EsModel::find()->index('wx')->select('news_uuid,media_name')
->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])
->andWhere(['media_name' => '城镇城镇交费'])->one();
orWhere
EsModel::find()->index('wx')->select('news_uuid,media_name')
->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])
->orWhere(['media_name' => '城镇城镇交费'])->one();
filterWhere
EsModel::find()->index('wx')->select('news_title')
->filterWhere(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb', 'news_title' => ''])
->one();
andFilterWhere
EsModel::find()->index('wx')->select('news_title')
->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])
->andFilterWhere(['and', ['media_name' => '城镇城镇交费', 'news_title' => '']])
->one();
orFilterWhere
EsModel::find()->index('wx')->select('news_title')
->where(['news_uuid' => 'b15e02a0bddacc0ee61d51d36d0022eb'])
->orFilterWhere(['and', ['media_name' => '城镇城镇交费', 'news_posttime' => '2021-09-01 19:00:00']])
->one();
inkime/elasticsearch-query-builder 适用场景与选型建议
inkime/elasticsearch-query-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 8, 最近一次更新时间为 2021 年 10 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「elasticsearch」 「querybuilder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 inkime/elasticsearch-query-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 inkime/elasticsearch-query-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 inkime/elasticsearch-query-builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Parse JSON coming from jQuery QueryBuilder, into database queries.
Lightweight, fast query-builder for PHP based on Laravel Eloquent but with less overhead.
The Query Builder may be used to generate SQL queries in an object orientated fashion.
simple api library.
Doctrine DBAL adapter for Rekapager pagination library
Parse JSON coming from jQuery QueryBuilder, into database queries.
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-07