upgrade search engine
This commit is contained in:
parent
b11da225fb
commit
1a4bcf8755
|
@ -54,19 +54,12 @@ class PostRepositoryQuery extends RepositoryQuery
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function filterHandler(string $name, $value)
|
|
||||||
{
|
|
||||||
if ('category' === $name) {
|
|
||||||
$this->inCategory($value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function search(?string $keywords, ?string $tag)
|
public function search(?string $keywords, ?string $tag)
|
||||||
{
|
{
|
||||||
$keywords = explode(' ', $keywords);
|
$keywords = explode(' ', $keywords);
|
||||||
|
|
||||||
$filterWords = fn($keyword) => trim($keyword) !== '' && preg_match('/[a-zA-Z]+/', $keyword) && mb_strlen($keyword) > 2;
|
$filterWords = fn ($keyword) => '' !== trim($keyword) && preg_match('/[a-zA-Z]+/', $keyword);
|
||||||
$filter = fn($keyword) => trim($keyword) !== '' && preg_match('/[a-zA-Z]+/', $keyword) && mb_strlen($keyword) > 2;
|
$filter = fn ($keyword) => '' !== trim($keyword) && preg_match('/[a-zA-Z]+/', $keyword);
|
||||||
|
|
||||||
$keywords = array_filter($keywords, $filter);
|
$keywords = array_filter($keywords, $filter);
|
||||||
|
|
||||||
|
@ -83,7 +76,8 @@ class PostRepositoryQuery extends RepositoryQuery
|
||||||
WHERE
|
WHERE
|
||||||
post.status = 1 AND
|
post.status = 1 AND
|
||||||
post.published_at < :date
|
post.published_at < :date
|
||||||
');
|
'
|
||||||
|
);
|
||||||
|
|
||||||
$statement = $query->execute([
|
$statement = $query->execute([
|
||||||
':date' => (new \DateTime())->format('Y-m-d H:i:s'),
|
':date' => (new \DateTime())->format('Y-m-d H:i:s'),
|
||||||
|
@ -109,12 +103,14 @@ class PostRepositoryQuery extends RepositoryQuery
|
||||||
|
|
||||||
if (isset($matches[$v['id']])) {
|
if (isset($matches[$v['id']])) {
|
||||||
$matches[$v['id']]['similarity'] += $similarity;
|
$matches[$v['id']]['similarity'] += $similarity;
|
||||||
|
++$matches[$v['id']]['count'];
|
||||||
} else {
|
} else {
|
||||||
$matches[$v['id']] = [
|
$matches[$v['id']] = [
|
||||||
'id' => $v['id'],
|
'id' => $v['id'],
|
||||||
'title' => $v['title'],
|
'title' => $v['title'],
|
||||||
'published_at' => $v['published_at'],
|
'published_at' => $v['published_at'],
|
||||||
'similarity' => $similarity,
|
'similarity' => $similarity,
|
||||||
|
'count' => 1,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,12 +121,14 @@ class PostRepositoryQuery extends RepositoryQuery
|
||||||
|
|
||||||
if (isset($matches[$v['id']])) {
|
if (isset($matches[$v['id']])) {
|
||||||
$matches[$v['id']]['similarity'] += $similarity;
|
$matches[$v['id']]['similarity'] += $similarity;
|
||||||
|
++$matches[$v['id']]['count'];
|
||||||
} else {
|
} else {
|
||||||
$matches[$v['id']] = [
|
$matches[$v['id']] = [
|
||||||
'id' => $v['id'],
|
'id' => $v['id'],
|
||||||
'title' => $v['title'],
|
'title' => $v['title'],
|
||||||
'published_at' => $v['published_at'],
|
'published_at' => $v['published_at'],
|
||||||
'similarity' => $similarity,
|
'similarity' => $similarity,
|
||||||
|
'count' => 1,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -146,6 +144,7 @@ class PostRepositoryQuery extends RepositoryQuery
|
||||||
'title' => $v['title'],
|
'title' => $v['title'],
|
||||||
'published_at' => $v['published_at'],
|
'published_at' => $v['published_at'],
|
||||||
'similarity' => $similarity,
|
'similarity' => $similarity,
|
||||||
|
'count' => 1,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,6 +153,10 @@ class PostRepositoryQuery extends RepositoryQuery
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$matches = array_filter($matches, function($match) use ($keywords) {
|
||||||
|
return (100 * $match['count'] / count($keywords)) > 80;
|
||||||
|
});
|
||||||
|
|
||||||
usort($matches, function ($a, $b) {
|
usort($matches, function ($a, $b) {
|
||||||
if ($a['similarity'] > $b['similarity']) {
|
if ($a['similarity'] > $b['similarity']) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -163,7 +166,7 @@ class PostRepositoryQuery extends RepositoryQuery
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ($a['published_at'] <> $b['published_at']) * -1;
|
return ($a['published_at'] != $b['published_at']) * -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
$ids = array_column($matches, 'id');
|
$ids = array_column($matches, 'id');
|
||||||
|
@ -188,4 +191,11 @@ class PostRepositoryQuery extends RepositoryQuery
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function filterHandler(string $name, $value)
|
||||||
|
{
|
||||||
|
if ('category' === $name) {
|
||||||
|
$this->inCategory($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue