From 99735a32270edddc28aee5cd2bd9846494bcc7f7 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Fri, 31 May 2024 12:41:15 +0300 Subject: [PATCH 1/5] UHF-9962: added timestamp filtering to query and sort the returned results by date --- .../helfi_annif/src/RecommendationManager.php | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/public/modules/custom/helfi_annif/src/RecommendationManager.php b/public/modules/custom/helfi_annif/src/RecommendationManager.php index 8f202b9ad..5456323d9 100644 --- a/public/modules/custom/helfi_annif/src/RecommendationManager.php +++ b/public/modules/custom/helfi_annif/src/RecommendationManager.php @@ -44,27 +44,38 @@ public function getRecommendations(EntityInterface $node): array { // @todo #UHF-9964 exclude unwanted keywords and entities and refactor. $query = " select - n.nid, - count(n.nid) as relevancy + n.nid, + count(n.nid) as relevancy, + nfd.created from node as n left join node__field_annif_keywords as annif on n.nid = annif.entity_id + left join node_field_data as nfd on nfd.nid = n.nid where annif.field_annif_keywords_target_id in - (select - field_annif_keywords_target_id - from node__field_annif_keywords - where entity_id = :nid and - langcode = :langcode) + (select + field_annif_keywords_target_id + from node__field_annif_keywords + where entity_id = :nid and + langcode = :langcode) and n.langcode = :langcode and annif.langcode = :langcode + and nfd.langcode = :langcode and n.nid != :nid + and created > :timestamp group by n.nid order by relevancy DESC - limit 3; + limit 10; "; $response = []; try { - $results = $this->connection->query($query, [':nid' => $node->id(), ':langcode' => $node->language()->getId()])->fetchAll(); + $timestamp = strtotime("-1 year", time()); + $results = $this->connection + ->query($query, [ + ':nid' => $node->id(), + ':langcode' => $node->language()->getId(), + ':timestamp' => $timestamp + ]) + ->fetchAll(); } catch (\Exception $e) { $this->logger->error($e->getMessage()); @@ -75,7 +86,15 @@ public function getRecommendations(EntityInterface $node): array { return $response; } - $nids = array_column($results, 'nid'); + // limit results and sort by created timestamp. + $nids = array_splice($results, 0,3); + usort($nids, function ($a, $b) { + if ($a->created == $b->created) { + return 0; + } + return ($a->created > $b->created) ? -1 : 1; + }); + $nids = array_column($nids, 'nid'); try { $response = $this->entityManager From 7608600f9c82e5dbd3664d8afea4a9080bfa5164 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Fri, 31 May 2024 13:26:13 +0300 Subject: [PATCH 2/5] UHF-9962: code fixes --- .../custom/helfi_annif/src/RecommendationManager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/public/modules/custom/helfi_annif/src/RecommendationManager.php b/public/modules/custom/helfi_annif/src/RecommendationManager.php index 5456323d9..0dba90260 100644 --- a/public/modules/custom/helfi_annif/src/RecommendationManager.php +++ b/public/modules/custom/helfi_annif/src/RecommendationManager.php @@ -71,9 +71,9 @@ public function getRecommendations(EntityInterface $node): array { $timestamp = strtotime("-1 year", time()); $results = $this->connection ->query($query, [ - ':nid' => $node->id(), + ':nid' => $node->id(), ':langcode' => $node->language()->getId(), - ':timestamp' => $timestamp + ':timestamp' => $timestamp, ]) ->fetchAll(); } @@ -86,8 +86,8 @@ public function getRecommendations(EntityInterface $node): array { return $response; } - // limit results and sort by created timestamp. - $nids = array_splice($results, 0,3); + // Limit results and sort by created timestamp. + $nids = array_splice($results, 0, 3); usort($nids, function ($a, $b) { if ($a->created == $b->created) { return 0; From c261f6a5704886e5333889e4815d5d34921ed1bf Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 3 Jun 2024 09:58:34 +0300 Subject: [PATCH 3/5] UHF-9962: remove splice, limit 3 on query --- .../custom/helfi_annif/src/RecommendationManager.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/public/modules/custom/helfi_annif/src/RecommendationManager.php b/public/modules/custom/helfi_annif/src/RecommendationManager.php index 0dba90260..68691efef 100644 --- a/public/modules/custom/helfi_annif/src/RecommendationManager.php +++ b/public/modules/custom/helfi_annif/src/RecommendationManager.php @@ -63,7 +63,7 @@ public function getRecommendations(EntityInterface $node): array { and created > :timestamp group by n.nid order by relevancy DESC - limit 10; + limit 3; "; $response = []; @@ -85,16 +85,14 @@ public function getRecommendations(EntityInterface $node): array { if (!$results || !is_array($results)) { return $response; } - - // Limit results and sort by created timestamp. - $nids = array_splice($results, 0, 3); - usort($nids, function ($a, $b) { + + usort($results, function ($a, $b) { if ($a->created == $b->created) { return 0; } return ($a->created > $b->created) ? -1 : 1; }); - $nids = array_column($nids, 'nid'); + $nids = array_column($results, 'nid'); try { $response = $this->entityManager From 91faa244ab2c9a26852488b118fafe7f6a084301 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 3 Jun 2024 12:41:39 +0300 Subject: [PATCH 4/5] UHF-9962: added table name --- public/modules/custom/helfi_annif/src/RecommendationManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/modules/custom/helfi_annif/src/RecommendationManager.php b/public/modules/custom/helfi_annif/src/RecommendationManager.php index 68691efef..2e29db424 100644 --- a/public/modules/custom/helfi_annif/src/RecommendationManager.php +++ b/public/modules/custom/helfi_annif/src/RecommendationManager.php @@ -60,7 +60,7 @@ public function getRecommendations(EntityInterface $node): array { and annif.langcode = :langcode and nfd.langcode = :langcode and n.nid != :nid - and created > :timestamp + and nfd.created > :timestamp group by n.nid order by relevancy DESC limit 3; From 7b662b8de78a7d3a6b6095f0c738a7407091b82c Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 3 Jun 2024 12:43:46 +0300 Subject: [PATCH 5/5] UHF-9962: remove whitespace --- public/modules/custom/helfi_annif/src/RecommendationManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/modules/custom/helfi_annif/src/RecommendationManager.php b/public/modules/custom/helfi_annif/src/RecommendationManager.php index 2e29db424..c17572b77 100644 --- a/public/modules/custom/helfi_annif/src/RecommendationManager.php +++ b/public/modules/custom/helfi_annif/src/RecommendationManager.php @@ -85,7 +85,7 @@ public function getRecommendations(EntityInterface $node): array { if (!$results || !is_array($results)) { return $response; } - + usort($results, function ($a, $b) { if ($a->created == $b->created) { return 0;