Skip to content

Commit

Permalink
Merge pull request #635 from City-of-Helsinki/UHF-9962_v2
Browse files Browse the repository at this point in the history
UHF-9962: refined recommendation query
  • Loading branch information
rpnykanen authored Jun 3, 2024
2 parents 02962b1 + 7b662b8 commit 4becc94
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions public/modules/custom/helfi_annif/src/RecommendationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 nfd.created > :timestamp
group by n.nid
order by relevancy DESC
limit 3;
";

$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());
Expand All @@ -75,6 +86,12 @@ public function getRecommendations(EntityInterface $node): array {
return $response;
}

usort($results, function ($a, $b) {
if ($a->created == $b->created) {
return 0;
}
return ($a->created > $b->created) ? -1 : 1;
});
$nids = array_column($results, 'nid');

try {
Expand Down

0 comments on commit 4becc94

Please sign in to comment.