Skip to content

Commit

Permalink
Use base language as well when looking up results from the database. …
Browse files Browse the repository at this point in the history
…Some cleanup.
  • Loading branch information
thisismeonmounteverest committed Oct 22, 2023
1 parent 0725c77 commit 0b1e2b1
Showing 1 changed file with 4 additions and 139 deletions.
143 changes: 4 additions & 139 deletions src/Model/SuggestLocationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class SuggestLocationModel
private const ADMIN_UNIT = 'search.admin.units';
private const COUNTRY = 'search.countries';

private const TYPE_PLACE = 'isplace';
private const TYPE_ADMIN_UNIT = 'isadmin';
private const TYPE_COUNTRY = 'iscountry';
private TranslatorInterface $translator;
private EntityManagerInterface $entityManager;

Expand Down Expand Up @@ -285,7 +282,7 @@ private function getCountryId(string $countryOrAdminUnit): ?string

public function getLocationDetails(array $results, string $typeTranslationId = null): array
{
$locale = $this->translator->getLocale();
$locale = $this->adaptLocale($this->translator->getLocale());
$type = '';
if (null !== $typeTranslationId) {
$type = $this->translator->trans($typeTranslationId);
Expand All @@ -295,7 +292,7 @@ public function getLocationDetails(array $results, string $typeTranslationId = n
foreach ($results as $location) {
$locationEntity = $this->getDetailsForId($location['geoname_id']);
if (null !== $locationEntity) {
$name = $locationEntity->getName() . " (" . $location['score'] . ")";
$name = $locationEntity->getName();
$admin1 = $locationEntity->getAdmin1();
if (null !== $admin1 && $locationEntity !== $admin1) {
$admin1->setTranslatableLocale($locale);
Expand Down Expand Up @@ -323,37 +320,9 @@ public function getLocationDetails(array $results, string $typeTranslationId = n
return $locations;
}

private function getPlaceInformation(string $term): array
{
$placeAdminOrCountry = explode(',', $term);
if (count($placeAdminOrCountry) > 3) {
return ['locations' => []];
}

$adminIds = [];
$countryId = null;
$place = trim($placeAdminOrCountry[0]);
if (2 === count($placeAdminOrCountry)) {
$country = trim($placeAdminOrCountry[1]);
$countryId = $this->findCountryId($country);
}

if (3 === count($placeAdminOrCountry)) {
$admin = trim($placeAdminOrCountry[1]);
$country = trim($placeAdminOrCountry[2]);
$countryId = $this->findCountryId($country);
$adminIds = $this->findAdminUnitIds($admin, $countryId);
}

return [
count($placeAdminOrCountry) > 1 ? $place : $term,
$adminIds,
$countryId,
];
}

private function getDetailsForId($id)
{
$locale = $this->adaptLocale($this->translator->getLocale());
$qb = $this->entityManager->createQueryBuilder();
$query = $qb
->select('l')
Expand All @@ -367,7 +336,7 @@ private function getDetailsForId($id)
);
$query->setHint(
TranslatableListener::HINT_TRANSLATABLE_LOCALE,
$this->translator->getLocale(),
$locale,
);
// fallback
$query->setHint(
Expand All @@ -394,57 +363,6 @@ public function removeDuplicates(string $key, ...$resultArrays): array
return $places;
}

private function findAdminUnits(string $term, int $count): array
{
$adminUnits = $this->queryManticore($term, self::TYPE_ADMIN_UNIT);
$result = $this->removeDuplicates('geoname_id', $adminUnits);

return array_slice($result, 0, $count);
}

private function findCountries(string $term, int $count): array
{
$countries = $this->getIdsForLocationType($term, self::TYPE_COUNTRY);
$result = $this->removeDuplicates('geoname_id', $countries);

return array_slice($result, 0, $count);
}

private function getIdsForLocationType(
array $locationParts,
?string $ranker = null
): array {
$resultsForLocale = $this->queryManticore(
$locationParts,
$ranker,
$this->translator->getLocale()
);

$results = $this->queryManticore(
$locationParts,
$ranker,
null
);

return $this->removeDuplicates('geoname_id', $resultsForLocale, $results);
}

private function getCountryIds(string $country): array
{
$query = $this->getQueryForGeonamesRt();
$query
->match("{$country}")
->filter('iscountry', 'equals', 1)
->option('ranker', 'expr(\'sum(exact_hit)\')');
;

$query
->orFilter('locale', 'equals', $this->translator->getLocale())
->orFilter('locale', 'equals', '_geo');

return $this->getManticoreResults($query);
}

private function searchAdminUnits(array $adminUnits, ?string $countryId): array
{
if (null !== $countryId) {
Expand Down Expand Up @@ -498,59 +416,6 @@ private function searchAdminUnits(array $adminUnits, ?string $countryId): array
return $results;
}

private function queryManticore(
array $locationParts,
string $type,
?string $ranker = null
): array {
if (empty($locationParts)) {
return [];
}

$query = $this->getQueryForGeonamesRt();
$query
->phrase("{$locationParts[0]}")
->filter($type, 'equals', 1)
;

if (null !== $ranker) {
$query->option('ranker', 'expr(\'' . $ranker . '\')');
}

$adminIds = array_chunk($locationParts, 1);
if (!empty($adminIds)) {
foreach ($adminIds as $adminId)
{
$query->orFilter('admin', 'equals', $adminId);
}
}

if (null !== $countryId) {
$query->filter('country', 'equals', $countryId);
}

if (null !== $locale) {
$query->filter('locale', 'equals', $locale);
} else {
$query->filter('locale', 'equals', '_geo');
}

$results = $query->get();
$results->rewind();

$manticoreResult = [];
while ($results->valid()) {
$hit = $results->current();
if ($hit->getScore() > 0) {
$data = $hit->getData();
$manticoreResult[] = $data;
}
$results->next();
}

return $manticoreResult;
}

private function getManticoreResults(Search $query, int $limit = 1000): array
{
$query->limit($limit);
Expand Down

0 comments on commit 0b1e2b1

Please sign in to comment.