Compare commits

...

5 commits

Author SHA1 Message Date
Simon Vieille 850ebff7c0
fix ci syntax
Some checks are pending
ci/woodpecker/push/build Pipeline is pending
2023-09-29 16:02:54 +02:00
Simon Vieille 5b4b2b59be
refactoring 2023-09-28 21:29:51 +02:00
Simon Vieille 1a4bcf8755
upgrade search engine
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/deployment/deploy Pipeline was successful
2023-09-24 19:35:27 +02:00
Simon Vieille b11da225fb
upgrade search engine
Some checks failed
ci/woodpecker/push/build Pipeline failed
2023-09-24 19:28:27 +02:00
Simon Vieille 138b4f24ee
upgrade search engine
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/deployment/deploy Pipeline failed
2023-09-24 18:59:33 +02:00
3 changed files with 95 additions and 26 deletions

View file

@ -7,7 +7,7 @@ when:
event: [push, pull_request, tag, manual]
branch: [master, master-*, develop, develop-*, feature/*]
pipeline:
steps:
db-wait:
image: gitnet.fr/deblan/timeout:latest
commands:
@ -72,7 +72,7 @@ pipeline:
commands:
- apt-get update && apt-get -y install rsync
- cd /data/deblan/deblan.io-murph/
- rsync -avz "$CI_WORKSPACE/" "$CI_COMMIT_SHA"
- rsync -az "$CI_WORKSPACE/" "$CI_COMMIT_SHA"
services:
db:

View file

@ -5,7 +5,7 @@ variables:
when:
event: [deployment]
pipeline:
steps:
app-deploy:
image: deblan/php:8.1
secrets: [ssh_user, ssh_host, ssh_priv_key, app_directory]

View file

@ -54,15 +54,13 @@ class PostRepositoryQuery extends RepositoryQuery
;
}
protected function filterHandler(string $name, $value)
{
if ('category' === $name) {
$this->inCategory($value);
}
}
public function search(?string $keywords, ?string $tag)
{
$keywords = explode(' ', $keywords);
$filterWords = fn ($keyword) => '' !== trim($keyword) && preg_match('/[a-zA-Z]+/', $keyword);
$keywords = array_filter($keywords, $filterWords);
if ($keywords) {
$conn = $this->repository->getEm()->getConnection();
@ -70,43 +68,107 @@ class PostRepositoryQuery extends RepositoryQuery
'SELECT
post.id,
post.title,
MATCH(post.title) AGAINST(:search) AS MATCH_TITLE,
MATCH(post.content) AGAINST(:search) AS MATCH_CONTENT
post.content,
post.published_at
FROM post
WHERE
post.status = 1 AND
post.published_at < :date
ORDER BY
MATCH_TITLE DESC,
MATCH_CONTENT DESC
');
'
);
$statement = $query->execute([
':search' => $keywords,
':date' => (new \DateTime())->format('Y-m-d H:i:s'),
]);
$results = $statement->fetchAll();
$ids = [];
$matches = [];
foreach ($results as $k => $v) {
$rate = ($v['MATCH_TITLE'] * 2) + $v['MATCH_CONTENT'];
$initWords = explode(' ', $v['title']);
$words = [];
if ($rate >= 7) {
$ids[] = $v['id'];
foreach ($initWords as $initWord) {
$words = array_merge($words, preg_split('/[:_\'-]+/', $initWord));
}
}
if (0 == count($ids)) {
foreach ($results as $k => $v) {
$rate = ($v['MATCH_TITLE'] * 2) + $v['MATCH_CONTENT'];
$words = array_filter($words, $filterWords);
if ($rate >= 6) {
$ids[] = $v['id'];
foreach ($keywords as $keyword) {
if (str_contains(mb_strtolower($v['content']), mb_strtolower($keyword))) {
$similarity = 99;
if (isset($matches[$v['id']])) {
$matches[$v['id']]['similarity'] += $similarity;
++$matches[$v['id']]['count'];
} else {
$matches[$v['id']] = [
'id' => $v['id'],
'title' => $v['title'],
'published_at' => $v['published_at'],
'similarity' => $similarity,
'count' => 1,
];
}
}
foreach ($words as $word) {
if (str_contains(mb_strtolower($word), mb_strtolower($keyword))) {
$similarity = 150;
if (isset($matches[$v['id']])) {
$matches[$v['id']]['similarity'] += $similarity;
++$matches[$v['id']]['count'];
} else {
$matches[$v['id']] = [
'id' => $v['id'],
'title' => $v['title'],
'published_at' => $v['published_at'],
'similarity' => $similarity,
'count' => 1,
];
}
} else {
$lev = levenshtein($word, $keyword);
$similarity = 100 - ($lev * 100 / mb_strlen($word));
if ($similarity > 70) {
if (isset($matches[$v['id']])) {
$matches[$v['id']]['similarity'] += $similarity;
} else {
$matches[$v['id']] = [
'id' => $v['id'],
'title' => $v['title'],
'published_at' => $v['published_at'],
'similarity' => $similarity,
'count' => 1,
];
}
}
}
}
}
}
$matches = array_filter($matches, function($match) use ($keywords) {
return (100 * $match['count'] / count($keywords)) > 80;
});
usort($matches, function ($a, $b) {
if ($a['similarity'] > $b['similarity']) {
return -1;
}
if ($b['similarity'] > $a['similarity']) {
return 1;
}
return ($a['published_at'] != $b['published_at']) * -1;
});
$ids = array_column($matches, 'id');
if (!$ids) {
$ids = [-1];
}
@ -127,4 +189,11 @@ class PostRepositoryQuery extends RepositoryQuery
return $this;
}
protected function filterHandler(string $name, $value)
{
if ('category' === $name) {
$this->inCategory($value);
}
}
}