Skip to content

Commit dac2c75

Browse files
committed
add script as an option to the sum function
1 parent cb57214 commit dac2c75

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,10 @@ The package is developed and tested under Elasticsearch ``v6.*``. It should be a
900900
901901
| Name | Required | Type | Default | Description |
902902
|:--------:|:--------:|:-----------------------:|:---------:|:-----------------------------------------------------:|
903-
| column | Y | ``string`` | | |
903+
| column | | ``string`` | ``Empty`` | |
904904
|custom_name| | ``string`` | ``null`` | |
905905
|missing_value| | ``numeric`` | ``null`` | This value will be used to replace null values |
906+
|script | | ``array`` | ``null`` | Check official Doc as reference |
906907
* Output
907908
908909
``self``

src/LaravelElasticsearchQueryBuilder.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -853,24 +853,26 @@ public function max($column, $agg_name = null) {
853853
}
854854

855855
/**
856-
* @param $column
856+
* @param null|string $column
857857
* @param null|string $agg_name
858858
* @param null|float|int $missing_value
859859
* @return $this
860860
*/
861-
public function sum($column, $agg_name = null, $missing_value = null) {
862-
$prepended_column = $this->prepended_path ? ($this->prepended_path . '.' . $column) : $column;
863-
list($prepended_column) = $this->getMappingProperty($prepended_column);
864-
if($missing_value !== null) {
865-
$this->aggs[$agg_name ?? 'sum_' . $column] = ['sum' => [
866-
'field' => $prepended_column,
867-
'missing' => $missing_value
868-
]];
869-
} else {
861+
public function sum($column = '', $agg_name = null, $missing_value = null, $script = null) {
862+
if($script) {
870863
$this->aggs[$agg_name ?? 'sum_' . $column] = ['sum' => [
871-
'field' => $prepended_column
864+
'script' => $script
872865
]];
873866
}
867+
if($column) {
868+
$prepended_column = $this->prepended_path ? ($this->prepended_path . '.' . $column) : $column;
869+
list($prepended_column) = $this->getMappingProperty($prepended_column);
870+
$this->aggs[$agg_name ?? 'sum_' . $column]['sum']['field'] = $prepended_column;
871+
}
872+
873+
if($missing_value) {
874+
$this->aggs[$agg_name ?? 'sum_' . $column]['sum']['missing'] = $missing_value;
875+
}
874876
return $this;
875877
}
876878

0 commit comments

Comments
 (0)