mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Merge branch 'features/api-search' into 'master'
Features/api search See merge request Chill-Projet/chill-bundles!98
This commit is contained in:
commit
85835c8b0d
@ -36,6 +36,7 @@ use Chill\MainBundle\Search\SearchProvider;
|
|||||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||||
use Chill\MainBundle\Search\SearchApi;
|
use Chill\MainBundle\Search\SearchApi;
|
||||||
|
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SearchController
|
* Class SearchController
|
||||||
@ -151,11 +152,14 @@ class SearchController extends AbstractController
|
|||||||
{
|
{
|
||||||
//TODO this is an incomplete implementation
|
//TODO this is an incomplete implementation
|
||||||
$query = $request->query->get('q', '');
|
$query = $request->query->get('q', '');
|
||||||
|
$types = $request->query->get('type', []);
|
||||||
|
|
||||||
$results = $this->searchApi->getResults($query, 0, 150);
|
if (count($types) === 0) {
|
||||||
$paginator = $this->paginatorFactory->create(count($results));
|
throw new BadRequestException("The request must contains at "
|
||||||
|
." one type");
|
||||||
|
}
|
||||||
|
|
||||||
$collection = new Collection($results, $paginator);
|
$collection = $this->searchApi->getResults($query, $types, []);
|
||||||
|
|
||||||
return $this->json($collection);
|
return $this->json($collection);
|
||||||
}
|
}
|
||||||
|
@ -2,88 +2,169 @@
|
|||||||
|
|
||||||
namespace Chill\MainBundle\Search;
|
namespace Chill\MainBundle\Search;
|
||||||
|
|
||||||
use Chill\PersonBundle\Entity\Person;
|
use Chill\MainBundle\Serializer\Model\Collection;
|
||||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||||
|
use Chill\PersonBundle\Search\SearchPersonApiProvider;
|
||||||
|
use Chill\ThirdPartyBundle\Search\ThirdPartyApiSearch;
|
||||||
|
use Doctrine\DBAL\Types\Types;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Doctrine\ORM\Query\ResultSetMappingBuilder;
|
||||||
use Chill\MainBundle\Search\SearchProvider;
|
use Chill\MainBundle\Search\SearchProvider;
|
||||||
use Symfony\Component\VarDumper\Resources\functions\dump;
|
use Symfony\Component\VarDumper\Resources\functions\dump;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ***Warning*** This is an incomplete implementation ***Warning***
|
|
||||||
*/
|
*/
|
||||||
class SearchApi
|
class SearchApi
|
||||||
{
|
{
|
||||||
private EntityManagerInterface $em;
|
private EntityManagerInterface $em;
|
||||||
private SearchProvider $search;
|
private PaginatorFactory $paginator;
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $em, SearchProvider $search)
|
private array $providers = [];
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
EntityManagerInterface $em,
|
||||||
|
SearchPersonApiProvider $searchPerson,
|
||||||
|
ThirdPartyApiSearch $thirdPartyApiSearch,
|
||||||
|
PaginatorFactory $paginator
|
||||||
|
)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->search = $search;
|
$this->providers[] = $searchPerson;
|
||||||
|
$this->providers[] = $thirdPartyApiSearch;
|
||||||
|
$this->paginator = $paginator;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Model/Result[]
|
* @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
|
$queries = $this->findQueries($pattern, $types, $parameters);
|
||||||
$results = [];
|
|
||||||
|
|
||||||
foreach ($this->getPersons($query) as $p) {
|
$total = $this->countItems($queries, $types, $parameters);
|
||||||
$results[] = new Model\Result((float)\rand(0, 100) / 100, $p);
|
$paginator = $this->paginator->create($total);
|
||||||
}
|
|
||||||
foreach ($this->getThirdParties($query) as $t) {
|
|
||||||
$results[] = new Model\Result((float)\rand(0, 100) / 100, $t);
|
|
||||||
}
|
|
||||||
|
|
||||||
\usort($results, function(Model\Result $a, Model\Result $b) {
|
$rawResults = $this->fetchRawResult($queries, $types, $parameters, $paginator);
|
||||||
return ($a->getRelevance() <=> $b->getRelevance()) * -1;
|
|
||||||
});
|
|
||||||
|
|
||||||
return $results;
|
$this->prepareProviders($rawResults);
|
||||||
|
$results = $this->buildResults($rawResults);
|
||||||
|
|
||||||
|
$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')
|
return \array_filter(
|
||||||
->getScalarResult();
|
$this->providers,
|
||||||
$nbResults = rand(0, 15);
|
fn($p) => $p->supportsTypes($pattern, $types, $parameters)
|
||||||
|
);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getPersons(string $query)
|
private function countItems($providers, $types, $parameters): int
|
||||||
{
|
{
|
||||||
$params = [
|
list($countQuery, $parameters) = $this->buildCountQuery($providers, $types, $parameters);
|
||||||
SearchInterface::SEARCH_PREVIEW_OPTION => false
|
$rsmCount = new ResultSetMappingBuilder($this->em);
|
||||||
];
|
$rsmCount->addScalarResult('count', 'count');
|
||||||
$search = $this->search->getResultByName($query, 'person_regular', 0, 50, $params, 'json');
|
$countNq = $this->em->createNativeQuery($countQuery, $rsmCount);
|
||||||
$ids = \array_map(function($r) { return $r['id']; }, $search['results']);
|
$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) {
|
foreach ($queries as $q) {
|
||||||
return [];
|
$unions[] = $q->buildQuery();
|
||||||
|
$parameters = \array_merge($parameters, $q->buildParameters());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->em->getRepository(Person::class)
|
$unionUnordered = \implode(" UNION ", $unions);
|
||||||
->findById($ids)
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $items ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
18
src/Bundle/ChillMainBundle/Search/SearchApiInterface.php
Normal file
18
src/Bundle/ChillMainBundle/Search/SearchApiInterface.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Search;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Search\SearchApiQuery;
|
||||||
|
|
||||||
|
interface SearchApiInterface
|
||||||
|
{
|
||||||
|
public function provideQuery(string $pattern, array $parameters): SearchApiQuery;
|
||||||
|
|
||||||
|
public function supportsTypes(string $pattern, array $types, array $parameters): bool;
|
||||||
|
|
||||||
|
public function prepare(array $metadatas): void;
|
||||||
|
|
||||||
|
public function supportsResult(string $key, array $metadatas): bool;
|
||||||
|
|
||||||
|
public function getResult(string $key, array $metadata, float $pertinence);
|
||||||
|
}
|
85
src/Bundle/ChillMainBundle/Search/SearchApiQuery.php
Normal file
85
src/Bundle/ChillMainBundle/Search/SearchApiQuery.php
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Search;
|
||||||
|
|
||||||
|
class SearchApiQuery
|
||||||
|
{
|
||||||
|
private ?string $selectKey;
|
||||||
|
private array $selectKeyParams = [];
|
||||||
|
private ?string $jsonbMetadata;
|
||||||
|
private array $jsonbMetadataParams = [];
|
||||||
|
private ?string $pertinence;
|
||||||
|
private array $pertinenceParams = [];
|
||||||
|
private ?string $fromClause;
|
||||||
|
private array $fromClauseParams = [];
|
||||||
|
private ?string $whereClause;
|
||||||
|
private array $whereClauseParams = [];
|
||||||
|
|
||||||
|
public function setSelectKey(string $selectKey, array $params = []): self
|
||||||
|
{
|
||||||
|
$this->selectKey = $selectKey;
|
||||||
|
$this->selectKeyParams = $params;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSelectJsonbMetadata(string $jsonbMetadata, array $params = []): self
|
||||||
|
{
|
||||||
|
$this->jsonbMetadata = $jsonbMetadata;
|
||||||
|
$this->jsonbMetadataParams = $params;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSelectPertinence(string $pertinence, array $params = []): self
|
||||||
|
{
|
||||||
|
$this->pertinence = $pertinence;
|
||||||
|
$this->pertinenceParams = $params;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFromClause(string $fromClause, array $params = []): self
|
||||||
|
{
|
||||||
|
$this->fromClause = $fromClause;
|
||||||
|
$this->fromClauseParams = $params;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setWhereClause(string $whereClause, array $params = []): self
|
||||||
|
{
|
||||||
|
$this->whereClause = $whereClause;
|
||||||
|
$this->whereClauseParams = $params;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildQuery(): string
|
||||||
|
{
|
||||||
|
return \strtr("SELECT
|
||||||
|
'{key}' AS key,
|
||||||
|
{metadata} AS metadata,
|
||||||
|
{pertinence} AS pertinence
|
||||||
|
FROM {from}
|
||||||
|
WHERE {where}
|
||||||
|
", [
|
||||||
|
'{key}' => $this->selectKey,
|
||||||
|
'{metadata}' => $this->jsonbMetadata,
|
||||||
|
'{pertinence}' => $this->pertinence,
|
||||||
|
'{from}' => $this->fromClause,
|
||||||
|
'{where}' => $this->whereClause
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildParameters(): array
|
||||||
|
{
|
||||||
|
return \array_merge(
|
||||||
|
$this->selectKeyParams,
|
||||||
|
$this->jsonbMetadataParams,
|
||||||
|
$this->pertinenceParams,
|
||||||
|
$this->fromClauseParams,
|
||||||
|
$this->whereClauseParams
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
40
src/Bundle/ChillMainBundle/Search/SearchApiResult.php
Normal file
40
src/Bundle/ChillMainBundle/Search/SearchApiResult.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Search;
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\Annotation as Serializer;
|
||||||
|
|
||||||
|
class SearchApiResult
|
||||||
|
{
|
||||||
|
private float $pertinence;
|
||||||
|
|
||||||
|
private $result;
|
||||||
|
|
||||||
|
public function __construct(float $relevance)
|
||||||
|
{
|
||||||
|
$this->relevance = $relevance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setResult($result): self
|
||||||
|
{
|
||||||
|
$this->result = $result;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Serializer\Groups({"read"})
|
||||||
|
*/
|
||||||
|
public function getResult()
|
||||||
|
{
|
||||||
|
return $this->result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Serializer\Groups({"read"})
|
||||||
|
*/
|
||||||
|
public function getRelevance(): float
|
||||||
|
{
|
||||||
|
return $this->relevance;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bundle\ChillMainBundle\Tests\Controller;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
|
class SearchApiControllerTest extends WebTestCase
|
||||||
|
{
|
||||||
|
use PrepareClientTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider generateSearchData
|
||||||
|
*/
|
||||||
|
public function testSearch(string $pattern, array $types)
|
||||||
|
{
|
||||||
|
$client = $this->getClientAuthenticated();
|
||||||
|
|
||||||
|
$client->request(
|
||||||
|
Request::METHOD_GET,
|
||||||
|
'/api/1.0/search.json',
|
||||||
|
[ 'q' => $pattern, 'type' => $types ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertResponseIsSuccessful();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateSearchData()
|
||||||
|
{
|
||||||
|
yield ['per', ['person', 'thirdparty'] ];
|
||||||
|
yield ['per', ['thirdparty'] ];
|
||||||
|
yield ['per', ['person'] ];
|
||||||
|
yield ['fjklmeqjfkdqjklrmefdqjklm', ['person', 'thirdparty'] ];
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,5 @@ services:
|
|||||||
Chill\MainBundle\Search\SearchProvider: '@chill_main.search_provider'
|
Chill\MainBundle\Search\SearchProvider: '@chill_main.search_provider'
|
||||||
|
|
||||||
Chill\MainBundle\Search\SearchApi:
|
Chill\MainBundle\Search\SearchApi:
|
||||||
arguments:
|
autowire: true
|
||||||
$em: '@Doctrine\ORM\EntityManagerInterface'
|
autoconfigure: true
|
||||||
$search: '@Chill\MainBundle\Search\SearchProvider'
|
|
||||||
|
@ -37,6 +37,11 @@ final class PersonRepository
|
|||||||
return $this->repository->find($id, $lockMode, $lockVersion);
|
return $this->repository->find($id, $lockMode, $lockVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function findByIds($ids): array
|
||||||
|
{
|
||||||
|
return $this->repository->findBy(['id' => $ids]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $centers
|
* @param $centers
|
||||||
* @param $firstResult
|
* @param $firstResult
|
||||||
|
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Chill\PersonBundle\Search;
|
||||||
|
|
||||||
|
use Chill\PersonBundle\Repository\PersonRepository;
|
||||||
|
use Chill\MainBundle\Search\SearchApiQuery;
|
||||||
|
use Chill\MainBundle\Search\SearchApiInterface;
|
||||||
|
|
||||||
|
class SearchPersonApiProvider implements SearchApiInterface
|
||||||
|
{
|
||||||
|
private PersonRepository $personRepository;
|
||||||
|
|
||||||
|
public function __construct(PersonRepository $personRepository)
|
||||||
|
{
|
||||||
|
$this->personRepository = $personRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideQuery(string $pattern, array $parameters): SearchApiQuery
|
||||||
|
{
|
||||||
|
$query = new SearchApiQuery();
|
||||||
|
$query
|
||||||
|
->setSelectKey("person")
|
||||||
|
->setSelectJsonbMetadata("jsonb_build_object('id', person.id)")
|
||||||
|
->setSelectPertinence("SIMILARITY(LOWER(UNACCENT(?)), person.fullnamecanonical)", [ $pattern ])
|
||||||
|
->setFromClause("chill_person_person AS person")
|
||||||
|
->setWhereClause("SIMILARITY(LOWER(UNACCENT(?)), person.fullnamecanonical) > 0.20", [ $pattern ])
|
||||||
|
;
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsTypes(string $pattern, array $types, array $parameters): bool
|
||||||
|
{
|
||||||
|
return \in_array('person', $types);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function prepare(array $metadatas): void
|
||||||
|
{
|
||||||
|
$ids = \array_map(fn($m) => $m['id'], $metadatas);
|
||||||
|
|
||||||
|
$this->personRepository->findByIds($ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsResult(string $key, array $metadatas): bool
|
||||||
|
{
|
||||||
|
return $key === 'person';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getResult(string $key, array $metadata, float $pertinence)
|
||||||
|
{
|
||||||
|
return $this->personRepository->find($metadata['id']);
|
||||||
|
}
|
||||||
|
}
|
@ -28,3 +28,7 @@ services:
|
|||||||
$em: '@Doctrine\ORM\EntityManagerInterface'
|
$em: '@Doctrine\ORM\EntityManagerInterface'
|
||||||
$tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
|
$tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
|
||||||
$authorizationHelper: '@Chill\MainBundle\Security\Authorization\AuthorizationHelper'
|
$authorizationHelper: '@Chill\MainBundle\Security\Authorization\AuthorizationHelper'
|
||||||
|
|
||||||
|
Chill\PersonBundle\Search\SearchPersonApiProvider:
|
||||||
|
autowire: true
|
||||||
|
autoconfigure: true
|
||||||
|
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Chill\ThirdPartyBundle\Search;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Search\SearchApiInterface;
|
||||||
|
use Chill\MainBundle\Search\SearchApiQuery;
|
||||||
|
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
|
||||||
|
|
||||||
|
class ThirdPartyApiSearch implements SearchApiInterface
|
||||||
|
{
|
||||||
|
private ThirdPartyRepository $thirdPartyRepository;
|
||||||
|
|
||||||
|
public function __construct(ThirdPartyRepository $thirdPartyRepository)
|
||||||
|
{
|
||||||
|
$this->thirdPartyRepository = $thirdPartyRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideQuery(string $pattern, array $parameters): SearchApiQuery
|
||||||
|
{
|
||||||
|
return (new SearchApiQuery)
|
||||||
|
->setSelectKey('tparty')
|
||||||
|
->setSelectJsonbMetadata("jsonb_build_object('id', tparty.id)")
|
||||||
|
->setSelectPertinence("SIMILARITY(?, LOWER(UNACCENT(tparty.name)))", [ $pattern ])
|
||||||
|
->setFromClause('chill_3party.third_party AS tparty')
|
||||||
|
->setWhereClause('SIMILARITY(LOWER(UNACCENT(?)), LOWER(UNACCENT(tparty.name))) > 0.20', [ $pattern ])
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsTypes(string $pattern, array $types, array $parameters): bool
|
||||||
|
{
|
||||||
|
return \in_array('thirdparty', $types);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function prepare(array $metadatas): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsResult(string $key, array $metadatas): bool
|
||||||
|
{
|
||||||
|
return $key === 'tparty';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getResult(string $key, array $metadata, float $pertinence)
|
||||||
|
{
|
||||||
|
return $this->thirdPartyRepository->find($metadata['id']);
|
||||||
|
}
|
||||||
|
}
|
@ -7,3 +7,7 @@ services:
|
|||||||
$paginatorFactory: '@Chill\MainBundle\Pagination\PaginatorFactory'
|
$paginatorFactory: '@Chill\MainBundle\Pagination\PaginatorFactory'
|
||||||
tags:
|
tags:
|
||||||
- { name: 'chill.search', alias: '3party' }
|
- { name: 'chill.search', alias: '3party' }
|
||||||
|
|
||||||
|
Chill\ThirdPartyBundle\Search\ThirdPartyApiSearch:
|
||||||
|
autowire: true
|
||||||
|
autoconfigure: true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user