Skip to content

Commit

Permalink
add script as an option to the sum function
Browse files Browse the repository at this point in the history
  • Loading branch information
ShisunXia committed Sep 3, 2019
1 parent cb57214 commit dac2c75
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
24 changes: 13 additions & 11 deletions src/LaravelElasticsearchQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit dac2c75

Please sign in to comment.