Skip to content

Commit

Permalink
Merge branch 'hotfix-coalesce'
Browse files Browse the repository at this point in the history
  • Loading branch information
johnvanbreda committed Aug 3, 2023
2 parents 94d1dd9 + aa75ba3 commit a20704b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Version 8.22.0
*2023-08-03*

* Adds a special field handler for Elasticsearch data that supports a coalesce function, returning
the value of the first field that has a non-empty value, selected from a list of provided fields.

# Version 8.21.0
*2023-06-28*

Expand Down
4 changes: 2 additions & 2 deletions application/config/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
*
* @var string
*/
$config['version'] = '8.21.9';
$config['version'] = '8.22.0';

/**
* Version release date.
*
* @var string
*/
$config['release_date'] = '2023-08-01';
$config['release_date'] = '2023-08-03';

/**
* Link to the code repository downloads page.
Expand Down
31 changes: 31 additions & 0 deletions modules/rest_api/libraries/RestApiElasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,34 @@ private function esGetSpecialFieldAttrValue(array $doc, array $params) {
return implode('; ', $r);
}

/**
* Special field handler returns the value for the first non-empty field.
*
* Provide a comma-separated list of field names as the parameter. The value
* of the first field in the list to have a non-empty value is returned.
*
* @param array $doc
* Elasticsearch document.
* @param array $params
* Parameters defined for the special field.
*
* @return string
* Field value.
*/
private function esGetSpecialFieldCoalesce(array $doc, array $params) {
if (count($params) !== 1) {
return 'Incorrect params for coalesce field';
}
$fields = explode(',', $params[0]);
foreach ($fields as $field) {
$value = $this->getRawEsFieldValue($doc, $field);
if ($value !== '') {
return $value;
}
}
return '';
}

/**
* Special field handler which returns a constant value.
*
Expand Down Expand Up @@ -1918,6 +1946,9 @@ private function getEsPostData($postObj, $format, $file, $isSearch) {
elseif (preg_match('/^#query(.*)#$/', $field)) {
$fields[] = 'identification.query';
}
elseif (preg_match('/^#coalesce:(.*)#$/', $field, $matches)) {
$fields = array_merge($fields, explode(',', $matches[1]));
}
elseif (preg_match('/^#attr_value:(event|sample|parent_event|occurrence):(\d+)#$/', $field, $matches)) {
$key = $matches[1] === 'parent_event' ? 'parent_attributes' : 'attributes';
// Tolerate sample or event for entity parameter.
Expand Down

0 comments on commit a20704b

Please sign in to comment.