Skip to content

Commit

Permalink
Upgrade AlgoliaSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
stakovicz committed Feb 13, 2025
1 parent d4a7eab commit 4916fd7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 81 deletions.
3 changes: 2 additions & 1 deletion app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ services:
AppBundle\Routing\LegacyRouter:
autowire: true

AlgoliaSearch\Client:
Algolia\AlgoliaSearch\SearchClient:
factory: [ Algolia\AlgoliaSearch\SearchClient, create ]
arguments: ["%algolia_app_id%", "%algolia_backend_api_key%"]

AppBundle\Joindin\JoindinComments:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ext-json": "*",
"ext-libxml": "*",
"ext-openssl": "*",
"algolia/algoliasearch-client-php": "^1.12",
"algolia/algoliasearch-client-php": "^3.4",
"beberlei/assert": "^2.9",
"captioning/captioning": "^2.6",
"ccmbenchmark/ting_bundle": "3.4.1",
Expand Down
58 changes: 36 additions & 22 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions sources/AppBundle/Command/IndexMeetupsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace AppBundle\Command;

use AlgoliaSearch\AlgoliaException;
use AlgoliaSearch\Client;
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
use Algolia\AlgoliaSearch\SearchClient;
use AppBundle\Event\Model\Repository\MeetupRepository;
use AppBundle\Indexation\Meetups\Runner;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
Expand Down Expand Up @@ -35,8 +35,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$container = $this->getContainer();
$ting = $this->getContainer()->get('ting');

/** @var Client $algoliaClient */
$algoliaClient = $container->get(Client::class);
/** @var SearchClient $algoliaClient */
$algoliaClient = $container->get(SearchClient::class);
$meetupRepository = $ting->get(MeetupRepository::class);

if ($input->getOption('run-scraping')) {
Expand Down
36 changes: 14 additions & 22 deletions sources/AppBundle/Controller/Website/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Afup\Site\Corporate\Articles;
use Afup\Site\Corporate\Branche;
use Afup\Site\Corporate\Feuille;
use AlgoliaSearch\AlgoliaException;
use AlgoliaSearch\Client;
use Algolia\AlgoliaSearch\SearchClient;
use AppBundle\Event\Model\Meetup;
use AppBundle\Event\Model\Repository\TalkRepository;
use AppBundle\Event\Model\Talk;
use AppBundle\Twig\ViewRenderer;
Expand All @@ -26,14 +26,14 @@ class HomeController extends AbstractController
private LoggerInterface $logger;
private RepositoryFactory $repositoryFactory;
private AdapterInterface $traceableAdapter;
private Client $client;
private SearchClient $client;
private bool $homeAlgoliaEnabled;

public function __construct(ViewRenderer $view,
LoggerInterface $logger,
RepositoryFactory $repositoryFactory,
AdapterInterface $traceableAdapter,
Client $client,
SearchClient $client,
bool $homeAlgoliaEnabled)
{
$this->view = $view;
Expand Down Expand Up @@ -65,18 +65,15 @@ public function display(): Response
]);
}

/**
* @return Talk
*/
protected function getTalkOfTheDay()
protected function getTalkOfTheDay(): Talk
{
return $this->repositoryFactory->get(TalkRepository::class)->getTalkOfTheDay(new \DateTime());
}

/**
* @return array
* @return array<Meetup>
*/
protected function getLatestMeetups()
protected function getLatestMeetups(): array
{
if (!$this->homeAlgoliaEnabled) {
return [];
Expand All @@ -85,19 +82,14 @@ protected function getLatestMeetups()
$cache = $this->traceableAdapter;
$cacheKey = 'home_algolia_meetups';

try {
$cacheItem = $cache->getItem($cacheKey);
if (!$cacheItem->isHit()) {
$cacheItem->expiresAfter(new \DateInterval('P1D'));
$cacheItem->set($this->doGetLatestMeetups());
$cache->save($cacheItem);
}

return $cacheItem->get();
} catch (AlgoliaException $e) {
$this->logger->error($e->getMessage());
return [];
$cacheItem = $cache->getItem($cacheKey);
if (!$cacheItem->isHit()) {
$cacheItem->expiresAfter(new \DateInterval('P1D'));
$cacheItem->set($this->doGetLatestMeetups());
$cache->save($cacheItem);
}

return $cacheItem->get();
}

private function doGetLatestMeetups()
Expand Down
28 changes: 10 additions & 18 deletions sources/AppBundle/Indexation/Meetups/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,28 @@

namespace AppBundle\Indexation\Meetups;

use AlgoliaSearch\AlgoliaException;
use AlgoliaSearch\Client;
use AlgoliaSearch\Index;
use Algolia\AlgoliaSearch\SearchClient;
use Algolia\AlgoliaSearch\SearchIndex;
use AppBundle\Antennes\AntennesCollection;
use AppBundle\Event\Model\Meetup;
use AppBundle\Event\Model\Repository\MeetupRepository;
use CCMBenchmark\Ting\Repository\CollectionInterface;

class Runner
{
protected Client $algoliaClient;
protected SearchClient $algoliaClient;

protected MeetupRepository $meetupRepository;

protected Transformer $transformer;

public function __construct(Client $algoliaClient, MeetupRepository $meetupRepository)
public function __construct(SearchClient $algoliaClient, MeetupRepository $meetupRepository)
{
$this->algoliaClient = $algoliaClient;
$this->meetupRepository = $meetupRepository;
$this->transformer = new Transformer(new AntennesCollection());
}

/**
*
* @throws AlgoliaException
*/
public function run(): void
{
$index = $this->initIndex();
Expand All @@ -39,17 +34,15 @@ public function run(): void

$meetups = $this->getTransformedMeetupsFromDatabase();

$index->clearIndex();
$index->addObjects($meetups, 'meetup_id');
$index->clearObjects();
$index->saveObjects($meetups, [
'objectIDKey' => 'meetup_id'
]);

echo "Indexation des meetups terminée avec succès !\n";
}

/**
* @return Index
* @throws AlgoliaException
*/
protected function initIndex()
protected function initIndex(): SearchIndex
{
$index = $this->algoliaClient->initIndex('afup_meetups');

Expand Down Expand Up @@ -80,10 +73,9 @@ private function getTransformedMeetupsFromDatabase(): array
}

/**
* @param CollectionInterface $meetupsCollection
* @return array<Meetup>
*/
public function transformMeetupsForIndexation($meetupsCollection): array
public function transformMeetupsForIndexation(CollectionInterface $meetupsCollection): array
{
$meetupsArray = [];
/** @var Meetup $meetup */
Expand Down
22 changes: 9 additions & 13 deletions sources/AppBundle/Indexation/Talks/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace AppBundle\Indexation\Talks;

use AlgoliaSearch\Client;
use AlgoliaSearch\Index;
use Algolia\AlgoliaSearch\SearchClient;
use Algolia\AlgoliaSearch\SearchIndex;
use AppBundle\Event\Model\Planning;
use AppBundle\Event\Model\Repository\EventRepository;
use AppBundle\Event\Model\Repository\PlanningRepository;
Expand All @@ -15,22 +15,19 @@

class Runner
{
protected Client $algoliaClient;
protected SearchClient $algoliaClient;

protected RepositoryFactory $ting;

protected Transformer $transformer;

public function __construct(Client $algoliaClient, RepositoryFactory $ting)
public function __construct(SearchClient $algoliaClient, RepositoryFactory $ting)
{
$this->algoliaClient = $algoliaClient;
$this->ting = $ting;
$this->transformer = new Transformer();
}

/**
*
*/
public function run(): void
{
$index = $this->initIndex();
Expand All @@ -45,14 +42,13 @@ public function run(): void
$objects[] = $object;
}

$index->clearIndex();
$index->addObjects($objects, 'planning_id');
$index->clearObjects();
$index->saveObjects($objects, [
'objectIDKey' => 'planning_id'
]);
}

/**
* @return Index
*/
protected function initIndex()
protected function initIndex(): SearchIndex
{
$index = $this->algoliaClient->initIndex('afup_talks');

Expand Down

0 comments on commit 4916fd7

Please sign in to comment.