Skip to content

Commit

Permalink
Merge pull request #5 from greezen/add-group
Browse files Browse the repository at this point in the history
feat:add group by support
  • Loading branch information
assert6 authored Oct 29, 2024
2 parents 1497642 + 567f228 commit af6c322
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
use Hyperf\Collection\Collection;
use Hyperf\Context\ApplicationContext;
use Hyperf\Logger\LoggerFactory;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Container\ContainerExceptionInterface;
use stdClass;
use TypeError;

use function Hyperf\Collection\collect;
use function Hyperf\Support\call;
Expand Down Expand Up @@ -60,6 +63,12 @@ class Builder

protected array $source = [];

/**
* aggs condition.
* @var array
*/
protected array $aggs = [];

/**
* 分页.
*/
Expand Down Expand Up @@ -305,6 +314,74 @@ public function orderByField(string $field, array $fieldValues): Builder
return $this;
}

/**
* group by field.
* @param string $field
* @param string $name
* @param null|int $size
* @return Builder
*/
public function groupBy(string $field, string $name, ?int $size = null): Builder
{
$this->aggs[$name] = [
'terms' => [
'field' => $field,
...(is_null($size) ? [] : ['size' => $size]),
],
];

return $this;
}

/**
* get aggregations result.
* @param null|array $aggs
* @return Collection
* @throws TypeError
* @throws NotFoundExceptionInterface
* @throws ContainerExceptionInterface
*/
public function getAggregations(?array $aggs = null): Collection
{
$this->sql = [
'from' => 0,
'size' => 0,
'track_total_hits' => true,
'index' => $this->model->getIndex(),
'body' => [
'query' => $this->query,
'highlight' => $this->highlight,
'sort' => $this->sort,
'_source' => $this->source,
'aggs' => $this->aggs,
],
];

if (empty($this->query)) {
$this->sql = [
'from' => 0,
'size' => 0,
'track_total_hits' => true,
'index' => $this->model->getIndex(),
'body' => [
'query' => [
'match_all' => new stdClass(),
],
],
];
}
$result = $this->run('search', $this->sql);
$aggsResult = $result['aggregations'] ?? [];

/* @phpstan-ignore-next-line */
return Collection::make($aggsResult)->filter(function ($value, $key) use ($aggs) {
if (is_null($aggs)) {
return true;
}

return in_array($key, $aggs);
});
}
/**
* insert.
*
Expand Down

0 comments on commit af6c322

Please sign in to comment.