diff --git a/README.md b/README.md index 0ad02b2..eaae337 100644 --- a/README.md +++ b/README.md @@ -900,9 +900,10 @@ The package is developed and tested under Elasticsearch ``v6.*``. It should be a | Name | Required | Type | Default | Description | |:--------:|:--------:|:-----------------------:|:---------:|:-----------------------------------------------------:| - | column | Y | ``string`` | | | + | column | | ``string`` | ``Empty`` | | |custom_name| | ``string`` | ``null`` | | |missing_value| | ``numeric`` | ``null`` | This value will be used to replace null values | + |script | | ``array`` | ``null`` | Check official Doc as reference | * Output ``self`` diff --git a/src/LaravelElasticsearchQueryBuilder.php b/src/LaravelElasticsearchQueryBuilder.php index 9a54997..84e0c5f 100644 --- a/src/LaravelElasticsearchQueryBuilder.php +++ b/src/LaravelElasticsearchQueryBuilder.php @@ -853,24 +853,26 @@ public function max($column, $agg_name = null) { } /** - * @param $column + * @param null|string $column * @param null|string $agg_name * @param null|float|int $missing_value * @return $this */ - public function sum($column, $agg_name = null, $missing_value = null) { - $prepended_column = $this->prepended_path ? ($this->prepended_path . '.' . $column) : $column; - list($prepended_column) = $this->getMappingProperty($prepended_column); - if($missing_value !== null) { - $this->aggs[$agg_name ?? 'sum_' . $column] = ['sum' => [ - 'field' => $prepended_column, - 'missing' => $missing_value - ]]; - } else { + public function sum($column = '', $agg_name = null, $missing_value = null, $script = null) { + if($script) { $this->aggs[$agg_name ?? 'sum_' . $column] = ['sum' => [ - 'field' => $prepended_column + 'script' => $script ]]; } + if($column) { + $prepended_column = $this->prepended_path ? ($this->prepended_path . '.' . $column) : $column; + list($prepended_column) = $this->getMappingProperty($prepended_column); + $this->aggs[$agg_name ?? 'sum_' . $column]['sum']['field'] = $prepended_column; + } + + if($missing_value) { + $this->aggs[$agg_name ?? 'sum_' . $column]['sum']['missing'] = $missing_value; + } return $this; }