mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
replace search api by a first workin implementation
This commit is contained in:
@@ -2,88 +2,175 @@
|
||||
|
||||
namespace Chill\MainBundle\Search;
|
||||
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Search\SearchPersonApiProvider;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Query\ResultSetMappingBuilder;
|
||||
use Chill\MainBundle\Search\SearchProvider;
|
||||
use Symfony\Component\VarDumper\Resources\functions\dump;
|
||||
|
||||
/**
|
||||
* ***Warning*** This is an incomplete implementation ***Warning***
|
||||
*/
|
||||
class SearchApi
|
||||
{
|
||||
private EntityManagerInterface $em;
|
||||
private SearchProvider $search;
|
||||
private PaginatorFactory $paginator;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, SearchProvider $search)
|
||||
private array $providers = [];
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
SearchProvider $search,
|
||||
SearchPersonApiProvider $searchPerson,
|
||||
PaginatorFactory $paginator
|
||||
)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->search = $search;
|
||||
$this->providers[] = $searchPerson;
|
||||
$this->paginator = $paginator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Model/Result[]
|
||||
*/
|
||||
public function getResults(string $query, int $offset, int $maxResult): array
|
||||
public function getResults(string $pattern, array $types, array $parameters): Collection
|
||||
{
|
||||
// **warning again**: this is an incomplete implementation
|
||||
$results = [];
|
||||
$queries = $this->findQueries($pattern, $types, $parameters);
|
||||
|
||||
foreach ($this->getPersons($query) as $p) {
|
||||
$results[] = new Model\Result((float)\rand(0, 100) / 100, $p);
|
||||
}
|
||||
foreach ($this->getThirdParties($query) as $t) {
|
||||
$results[] = new Model\Result((float)\rand(0, 100) / 100, $t);
|
||||
}
|
||||
$total = $this->countItems($queries, $types, $parameters);
|
||||
$paginator = $this->paginator->create($total);
|
||||
|
||||
\usort($results, function(Model\Result $a, Model\Result $b) {
|
||||
return ($a->getRelevance() <=> $b->getRelevance()) * -1;
|
||||
});
|
||||
$rawResults = $this->fetchRawResult($queries, $types, $parameters, $paginator);
|
||||
dump($rawResults);
|
||||
|
||||
return $results;
|
||||
$this->prepareProviders($rawResults);
|
||||
$results = $this->buildResults($rawResults);
|
||||
dump($results);
|
||||
|
||||
$collection = new Collection($results, $paginator);
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function countResults(string $query): int
|
||||
private function findQueries($pattern, array $types, array $parameters): array
|
||||
{
|
||||
return 0;
|
||||
return \array_map(
|
||||
fn($p) => $p->provideQuery($pattern, $parameters),
|
||||
$this->findProviders($pattern, $types, $parameters),
|
||||
);
|
||||
}
|
||||
|
||||
private function getThirdParties(string $query)
|
||||
private function findProviders(string $pattern, array $types, array $parameters): array
|
||||
{
|
||||
$thirdPartiesIds = $this->em->createQuery('SELECT t.id FROM '.ThirdParty::class.' t')
|
||||
->getScalarResult();
|
||||
$nbResults = rand(0, 15);
|
||||
|
||||
if ($nbResults === 1) {
|
||||
$nbResults++;
|
||||
} elseif ($nbResults === 0) {
|
||||
return [];
|
||||
}
|
||||
$ids = \array_map(function ($e) use ($thirdPartiesIds) { return $thirdPartiesIds[$e]['id'];},
|
||||
\array_rand($thirdPartiesIds, $nbResults));
|
||||
|
||||
$a = $this->em->getRepository(ThirdParty::class)
|
||||
->findById($ids);
|
||||
return $a;
|
||||
return \array_filter(
|
||||
$this->providers,
|
||||
fn($p) => $p->supportsTypes($pattern, $types, $parameters)
|
||||
);
|
||||
}
|
||||
|
||||
private function getPersons(string $query)
|
||||
private function countItems($providers, $types, $parameters): int
|
||||
{
|
||||
$params = [
|
||||
SearchInterface::SEARCH_PREVIEW_OPTION => false
|
||||
];
|
||||
$search = $this->search->getResultByName($query, 'person_regular', 0, 50, $params, 'json');
|
||||
$ids = \array_map(function($r) { return $r['id']; }, $search['results']);
|
||||
list($countQuery, $parameters) = $this->buildCountQuery($providers, $types, $parameters);
|
||||
$rsmCount = new ResultSetMappingBuilder($this->em);
|
||||
$rsmCount->addScalarResult('count', 'count');
|
||||
$countNq = $this->em->createNativeQuery($countQuery, $rsmCount);
|
||||
$countNq->setParameters($parameters);
|
||||
|
||||
return $countNq->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function buildCountQuery(array $queries, $types, $parameters)
|
||||
{
|
||||
$query = "SELECT COUNT(sq.key) AS count FROM ({union_unordered}) AS sq";
|
||||
$unions = [];
|
||||
$parameters = [];
|
||||
|
||||
if (count($ids) === 0) {
|
||||
return [];
|
||||
foreach ($queries as $q) {
|
||||
$unions[] = $q->buildQuery();
|
||||
$parameters = \array_merge($parameters, $q->buildParameters());
|
||||
}
|
||||
|
||||
return $this->em->getRepository(Person::class)
|
||||
->findById($ids)
|
||||
$unionUnordered = \implode(" UNION ", $unions);
|
||||
|
||||
return [
|
||||
\strtr($query, [ '{union_unordered}' => $unionUnordered ]),
|
||||
$parameters
|
||||
];
|
||||
}
|
||||
|
||||
private function buildUnionQuery(array $queries, $types, $parameters)
|
||||
{
|
||||
$query = "{unions} ORDER BY pertinence DESC";
|
||||
$unions = [];
|
||||
$parameters = [];
|
||||
|
||||
foreach ($queries as $q) {
|
||||
$unions[] = $q->buildQuery();
|
||||
$parameters = \array_merge($parameters, $q->buildParameters());
|
||||
}
|
||||
|
||||
$union = \implode(" UNION ", $unions);
|
||||
|
||||
return [
|
||||
\strtr($query, [ '{unions}' => $union]),
|
||||
$parameters
|
||||
];
|
||||
}
|
||||
|
||||
private function fetchRawResult($queries, $types, $parameters, $paginator): array
|
||||
{
|
||||
list($union, $parameters) = $this->buildUnionQuery($queries, $types, $parameters, $paginator);
|
||||
$rsm = new ResultSetMappingBuilder($this->em);
|
||||
$rsm->addScalarResult('key', 'key', Types::STRING)
|
||||
->addScalarResult('metadata', 'metadata', Types::JSON)
|
||||
->addScalarResult('pertinence', 'pertinence', Types::FLOAT)
|
||||
;
|
||||
|
||||
$nq = $this->em->createNativeQuery($union, $rsm);
|
||||
$nq->setParameters($parameters);
|
||||
|
||||
return $nq->getResult();
|
||||
}
|
||||
|
||||
private function prepareProviders($rawResults)
|
||||
{
|
||||
$metadatas = [];
|
||||
foreach ($rawResults as $r) {
|
||||
foreach ($this->providers as $k => $p) {
|
||||
if ($p->supportsResult($r['key'], $r['metadata'])) {
|
||||
$metadatas[$k][] = $r['metadata'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($metadatas as $k => $m) {
|
||||
$this->providers[$k]->prepare($m);
|
||||
}
|
||||
}
|
||||
|
||||
private function buildResults($rawResults)
|
||||
{
|
||||
foreach ($rawResults as $r) {
|
||||
foreach ($this->providers as $k => $p) {
|
||||
if ($p->supportsResult($r['key'], $r['metadata'])) {
|
||||
$items[] = (new SearchApiResult($r['pertinence']))
|
||||
->setResult(
|
||||
$p->getResult($r['key'], $r['metadata'], $r['pertinence'])
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dump($items);
|
||||
|
||||
return $items ?? [];
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user