Compare commits
5 commits
45bd1b408d
...
850ebff7c0
Author | SHA1 | Date | |
---|---|---|---|
850ebff7c0 | |||
5b4b2b59be | |||
1a4bcf8755 | |||
b11da225fb | |||
138b4f24ee |
|
@ -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:
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue