Search update and optimization

This commit is contained in:
2023-11-23 23:35:19 -03:00
parent bf03e85975
commit e1ef31dccd
10 changed files with 232 additions and 36 deletions

View File

@ -22,7 +22,8 @@ class Search
} else {
$results = $this->find($query, $tipo);
}
return $this->sort($results);
return $results;
//return $this->sort($results);
}
protected function findCualquiera(string $query): array
@ -45,7 +46,7 @@ class Search
}
protected function find(string $query, string $tipo): array
{
preg_match_all('/["\']([\s\w]+)["\']|(\w+)/i', $query, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL);
preg_match_all('/["\']([\s\w]+)["\']|(\w+)/iu', $query, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL);
$queries = array_map(function($match) {
array_shift($match);
$valid = array_filter($match, function($line) {return $line !== null;});
@ -56,7 +57,7 @@ class Search
foreach ($queries as $q) {
$this->add($results, $this->findVentas($q, $tipo));
if (in_array($tipo, $tiposUnidades)) {
$this->add($results, $this->findUnidadesDisponibles($q, $tipo));
$this->add($results, $this->findUnidadesDisponibles($q, $tipo), false);
}
}
return $results;
@ -91,7 +92,7 @@ class Search
protected function findUnidadesDisponibles(string $query, string $tipo): array
{
try {
return $this->unidadRepository->fetchDisponiblesByDescripcionAndTipo($query, $tipo);
return $this->unidadRepository->fetchDisponiblesIdsByDescripcionAndTipo($query, $tipo);
} catch (EmptyResponse) {
return [];
}
@ -99,7 +100,7 @@ class Search
protected function findUnidad(string $query, string $tipo): array
{
try {
return $this->ventaService->getByUnidad($query, $tipo);
return $this->ventaRepository->fetchIdsByUnidad($query, $tipo);
} catch (EmptyResult) {
return [];
}
@ -107,7 +108,7 @@ class Search
protected function findPropietario(string $query): array
{
try {
return $this->ventaService->getByPropietario($query);
return $this->ventaRepository->fetchIdsByPropietario($query);
} catch (EmptyResult) {
return [];
}
@ -116,7 +117,7 @@ class Search
{
try {
$precio = str_replace(['$', '.', ','], ['', '', '.'], $query);
return $this->ventaService->getByPrecio($precio);
return $this->ventaRepository->fetchIdsByPrecio($precio);
} catch (EmptyResult) {
return [];
}
@ -125,7 +126,7 @@ class Search
{
try {
$proyecto = $this->proyectoService->getByName($query);
return $this->ventaService->getByProyecto($proyecto->id);
return $this->ventaRepository->fetchIdsByProyecto($proyecto->id);
} catch (EmptyResult) {
return [];
}
@ -145,22 +146,21 @@ class Search
}
return $this->tipos;
}
protected function add(array &$results, array $found): void
protected function add(array &$results, array $found, bool $is_venta = true): void
{
foreach ($found as $item) {
if (!$this->inResults($item, $results)) {
$item['tipo'] = 'venta';
if (!$is_venta) {
$item['tipo'] = 'unidad';
}
$results []= $item;
}
}
}
protected function inResults($item, array $results): bool
{
foreach ($results as $result) {
if (get_class($item) === get_class($result) and $item->id === $result->id) {
return true;
}
}
return false;
return in_array($item, $results, true);
}
protected function sort(&$results): array
{