add search engine
This commit is contained in:
parent
c3c49d89cd
commit
120dcdaa44
8 changed files with 117 additions and 25 deletions
|
|
@ -4,7 +4,6 @@ namespace App\Repository\Blog;
|
|||
|
||||
use App\Core\Repository\RepositoryQuery;
|
||||
use App\Entity\Blog\Category;
|
||||
use App\Entity\User;
|
||||
use Knp\Component\Pager\PaginatorInterface;
|
||||
|
||||
/**
|
||||
|
|
@ -37,13 +36,14 @@ class PostRepositoryQuery extends RepositoryQuery
|
|||
return $this
|
||||
->andWhere('.status = 1')
|
||||
->andWhere('.publishedAt <= :now')
|
||||
->setParameter(':now', (new \DateTime('now'))->format('Y-m-d H:i:s'));
|
||||
->setParameter(':now', (new \DateTime('now'))->format('Y-m-d H:i:s'))
|
||||
;
|
||||
}
|
||||
|
||||
public function useFilters(array $filters)
|
||||
{
|
||||
foreach ($filters as $name => $value) {
|
||||
if ($value === null) {
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -54,7 +54,7 @@ class PostRepositoryQuery extends RepositoryQuery
|
|||
$this->andWhere('.'.$name.' LIKE :'.$name);
|
||||
$this->setParameter(':'.$name, '%'.$value.'%');
|
||||
} else {
|
||||
if ($name === 'category') {
|
||||
if ('category' === $name) {
|
||||
$this->inCategory($value);
|
||||
}
|
||||
}
|
||||
|
|
@ -62,4 +62,60 @@ class PostRepositoryQuery extends RepositoryQuery
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function search(string $keywords)
|
||||
{
|
||||
$conn = $this->repository->getEm()->getConnection();
|
||||
|
||||
$statement = $conn->prepare(
|
||||
'SELECT
|
||||
post.id,
|
||||
post.title,
|
||||
MATCH(post.title) AGAINST(:search) AS MATCH_TITLE,
|
||||
MATCH(post.content) AGAINST(:search) AS MATCH_CONTENT
|
||||
FROM post
|
||||
WHERE
|
||||
post.status = 1 AND
|
||||
post.published_at < :date
|
||||
ORDER BY
|
||||
MATCH_TITLE DESC,
|
||||
MATCH_CONTENT DESC
|
||||
');
|
||||
|
||||
$statement->execute([
|
||||
':search' => $keywords,
|
||||
':date' => (new \DateTime())->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
$results = $statement->fetchAll();
|
||||
$ids = [];
|
||||
|
||||
foreach ($results as $k => $v) {
|
||||
$rate = ($v['MATCH_TITLE'] * 2) + $v['MATCH_CONTENT'];
|
||||
|
||||
if ($rate >= 7) {
|
||||
$ids[] = $v['id'];
|
||||
}
|
||||
}
|
||||
|
||||
if (0 == count($ids)) {
|
||||
foreach ($results as $k => $v) {
|
||||
$rate = ($v['MATCH_TITLE'] * 2) + $v['MATCH_CONTENT'];
|
||||
|
||||
if ($rate >= 6) {
|
||||
$ids[] = $v['id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$ids) {
|
||||
$ids = [-1];
|
||||
}
|
||||
|
||||
return $this
|
||||
->orderBy('FIELD(p.id, :ids)')
|
||||
->andWhere('.id IN(:ids)')
|
||||
->setParameter(':ids', $ids)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue