From ffa596f4bbc31d1396b027221ed045b0ca4c11f5 Mon Sep 17 00:00:00 2001 From: John van Breda Date: Thu, 3 Aug 2023 15:49:30 +0100 Subject: [PATCH 1/2] Coalesce function ES special field handler. --- .../libraries/RestApiElasticsearch.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/modules/rest_api/libraries/RestApiElasticsearch.php b/modules/rest_api/libraries/RestApiElasticsearch.php index 6b34f2133..1d23fea16 100644 --- a/modules/rest_api/libraries/RestApiElasticsearch.php +++ b/modules/rest_api/libraries/RestApiElasticsearch.php @@ -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. * @@ -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. From aa75ba34e23c6856942db358a4f195407f6b10ef Mon Sep 17 00:00:00 2001 From: John van Breda Date: Thu, 3 Aug 2023 15:51:08 +0100 Subject: [PATCH 2/2] Changelog and version --- CHANGELOG.md | 6 ++++++ application/config/version.php | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba65877eb..850304adf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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* diff --git a/application/config/version.php b/application/config/version.php index fe9b8135a..5d71ae7e6 100644 --- a/application/config/version.php +++ b/application/config/version.php @@ -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.