Skip to content

Commit

Permalink
增加对min、max的支持,修复一些noticebug
Browse files Browse the repository at this point in the history
  • Loading branch information
jea committed Mar 4, 2022
1 parent d5f24bc commit 55f3688
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 20 deletions.
73 changes: 55 additions & 18 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,25 +345,27 @@ public function whereGeoBox($geoField, $attr, $leftTopLat, $leftTopLon, $rightBo
/**
* 聚合信息, es的叫: aggregations, 单次查询中只允许一次聚合
*
* @param string $field 被聚合的字段
* @param string $order 聚合后的排序字段
* @param string $sort 聚合后的字段排序方式 _count _term 提供第三种: selfChild 根据子聚合的value来排序,详细见test:groupBySum
* @param integer $size 聚合结果集长度
*
* @param string $field 被聚合的字段
* @param string $order 聚合后的排序字段
* @param string $sort 聚合后的字段排序方式 _count _term 提供第三种: selfChild 根据子聚合的value来排序,详细见test:groupBySum
* @param integer $size 聚合结果集长度
* @param array $child 子查询
* @param int $searchWindow 查询窗口大小
* @return $this
*/
public function groupBy($field, $order = '_count', $sort = 'ASC', $from = 0, $size = 10, $child = [])
public function groupBy($field, $order = '_count', $sort = 'ASC', $from = 0, $size = 10, $child = [], $searchWindow = 10000)
{
$key = $this->nestedBegin ? 'nested' : 'normal';

$this->params['aggregations'][$key][] = array(
'field' => $field,
'order' => $order,
'sort' => $sort,
'from' => $from,
'size' => $size,
'type' => parent::AGG_TYPE_AGGS,
'child' => $child,
'field' => $field,
'order' => $order,
'sort' => $sort,
'from' => $from,
'size' => $size,
'type' => parent::AGG_TYPE_AGGS,
'child' => $child,
'searchWindow' => $searchWindow,
);

return $this;
Expand All @@ -372,17 +374,20 @@ public function groupBy($field, $order = '_count', $sort = 'ASC', $from = 0, $si
/**
* 获取聚合后的总个数,去重的,类似于: select distinct(xxx) from table
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html
* @param $field
* @param $field
* @param array $child 子查询
* @param int $precisionThreshold 聚合后参与返回的key数量最大40000
* @return $this
*/
public function cardinality($field, $child = [])
public function cardinality($field, $child = [], $precisionThreshold = 40000)
{
$key = $this->nestedBegin ? 'nested' : 'normal';

$this->params['aggregations'][$key][] = array(
'field' => $field,
'type' => parent::AGG_TYPE_CARDINALITY,
'child' => $child,
'field' => $field,
'type' => parent::AGG_TYPE_CARDINALITY,
'child' => $child,
'precision_threshold' => $precisionThreshold,
);
return $this;
}
Expand All @@ -404,6 +409,38 @@ public function sum($field, $child = [])
return $this;
}

/**
* @param $field
* @return $this
*/
public function max($field, $child = [])
{
$key = $this->nestedBegin ? 'nested' : 'normal';

$this->params['aggregations'][$key][] = array(
'field' => $field,
'type' => parent::AGG_TYPE_MAX,
'child' => $child,
);
return $this;
}

/**
* @param $field
* @return $this
*/
public function min($field, $child = [])
{
$key = $this->nestedBegin ? 'nested' : 'normal';

$this->params['aggregations'][$key][] = array(
'field' => $field,
'type' => parent::AGG_TYPE_MIN,
'child' => $child,
);
return $this;
}

/**
* 按日期聚合
* @param $field
Expand Down
34 changes: 32 additions & 2 deletions src/DSLBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ class DSLBuilder extends ClientBuilder
*/
const AGG_TYPE_HISTOGRAM = 5;

const AGG_TYPE_MIN = 6;
const AGG_TYPE_MAX = 7;

/**
* 按标准时间格式分隔聚合
*/
Expand Down Expand Up @@ -678,6 +681,8 @@ private function checkAggregationParams($params)
{
switch ($params['type']) {
case self::AGG_TYPE_SUM:
case self::AGG_TYPE_MIN:
case self::AGG_TYPE_MAX:
case self::AGG_TYPE_CARDINALITY:
case self::AGG_TYPE_COUNT_VALUE:
if (!isset($params['field'])) {
Expand Down Expand Up @@ -710,6 +715,12 @@ private function getAggKey($type, $fieldName)
case self::AGG_TYPE_SUM:
$key = 'sum_' . $fk;
break;
case self::AGG_TYPE_MIN:
$key = 'min_' . $fk;
break;
case self::AGG_TYPE_MAX:
$key = 'max_' . $fk;
break;
case self::AGG_TYPE_CARDINALITY:
$key = 'cardinality_' . $fk;
break;
Expand Down Expand Up @@ -743,12 +754,29 @@ private function formatAggs($params, $fk)
]
];
break;
case self::AGG_TYPE_MIN:
$aggRes = [
'min' => [
'field' => $params['field']
]
];
break;
case self::AGG_TYPE_MAX:
$aggRes = [
'max' => [
'field' => $params['field']
]
];
break;
case self::AGG_TYPE_CARDINALITY:
$aggRes = [
'cardinality' => [
'field' => $params['field']
'field' => $params['field'],
]
];
if (isset($params['precision_threshold'])) {
$aggRes['cardinality']['precision_threshold'] = (int)$params['precision_threshold'];
}
break;
case self::AGG_TYPE_COUNT_VALUE:
$aggRes = [
Expand Down Expand Up @@ -789,7 +817,7 @@ private function formatAggs($params, $fk)
'terms' => [
'field' => $params['field'],
// 'order' => array($orderKey => $params['sort']),
'size' => 10000,
'size' => isset($params['searchWindow']) ? intval($params['searchWindow']) : 10000,
],
);
break;
Expand Down Expand Up @@ -920,6 +948,8 @@ private function getAggTypeEn($type)
self::AGG_TYPE_COUNT_VALUE => 'count',
self::AGG_TYPE_CARDINALITY => 'cardinality',
self::AGG_TYPE_SUM => 'sum',
self::AGG_TYPE_MAX => 'max',
self::AGG_TYPE_MIN => 'min',
self::AGG_TYPE_HISTOGRAM => 'histogram',
];
return $en[$type];
Expand Down

0 comments on commit 55f3688

Please sign in to comment.