Skip to content

Commit

Permalink
Merge pull request #653 from greg0ire/upgrade-algolia
Browse files Browse the repository at this point in the history
Upgrade the Algolia SDK to v4
  • Loading branch information
greg0ire authored Feb 6, 2025
2 parents a83aded + 5e98897 commit 2bab51b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 71 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": "8.3.*",
"algolia/algoliasearch-client-php": "^3.3",
"algolia/algoliasearch-client-php": "^4",
"doctrine/collections": "^2",
"doctrine/dbal": "^4.0",
"doctrine/inflector": "^2.0",
Expand Down
49 changes: 21 additions & 28 deletions composer.lock

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

2 changes: 1 addition & 1 deletion config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<argument>%doctrine.website.send_grid.api_key%</argument>
</service>

<service id="Algolia\AlgoliaSearch\SearchClient" autowire="false">
<service id="Algolia\AlgoliaSearch\Api\SearchClient" autowire="false">
<factory method="create"/>
<argument>%doctrine.website.algolia.app_id%</argument>
<argument>%doctrine.website.algolia.admin_api_key%</argument>
Expand Down
16 changes: 4 additions & 12 deletions lib/Docs/SearchIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Doctrine\Website\Docs;

use Algolia\AlgoliaSearch\SearchClient;
use Algolia\AlgoliaSearch\SearchIndex;
use Algolia\AlgoliaSearch\Api\SearchClient;
use Doctrine\Website\Model\Project;
use Doctrine\Website\Model\ProjectVersion;
use Generator;
Expand Down Expand Up @@ -50,9 +49,7 @@ public function __construct(

public function initSearchIndex(): void
{
$index = $this->getSearchIndex();

$index->setSettings([
$this->client->setSettings(self::INDEX_NAME, [
'attributesToIndex' => ['projectName', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'content'],
'customRanking' => ['asc(rank)'],
'ranking' => ['words', 'typo', 'attribute', 'proximity', 'custom'],
Expand All @@ -65,7 +62,7 @@ public function initSearchIndex(): void
'removeWordsIfNoResults' => 'allOptional',
]);

$index->clearObjects();
$this->client->clearObjects(self::INDEX_NAME);
}

/** @param DocumentNode[] $documents */
Expand All @@ -82,7 +79,7 @@ public function buildSearchIndexes(
}
}

$this->getSearchIndex()->saveObjects($records, ['autoGenerateObjectIDIfNotExist' => true]);
$this->client->saveObjects(self::INDEX_NAME, $records, ['autoGenerateObjectIDIfNotExist' => true]);
}

/** @return Generator<searchRecord> */
Expand Down Expand Up @@ -225,9 +222,4 @@ private function stripContent(string $content): string
{
return str_replace(['"', '\''], '', $content);
}

private function getSearchIndex(): SearchIndex
{
return $this->client->initIndex(self::INDEX_NAME);
}
}
36 changes: 7 additions & 29 deletions tests/Docs/SearchIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Doctrine\Website\Tests\Docs;

use Algolia\AlgoliaSearch\SearchClient;
use Algolia\AlgoliaSearch\SearchIndex;
use Algolia\AlgoliaSearch\Api\SearchClient;
use Doctrine\Website\Application;
use Doctrine\Website\Docs\RST\GuidesParser;
use Doctrine\Website\Docs\SearchIndexer;
Expand Down Expand Up @@ -33,16 +32,9 @@ protected function setUp(): void

public function testInitSearchIndex(): void
{
$index = $this->createMock(SearchIndex::class);

$this->client->expects(self::once())
->method('initIndex')
->with(SearchIndexer::INDEX_NAME)
->willReturn($index);

$index->expects(self::once())
->method('setSettings')
->with([
->with(SearchIndexer::INDEX_NAME, [
'attributesToIndex' => ['projectName', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'content'],
'customRanking' => ['asc(rank)'],
'ranking' => ['words', 'typo', 'attribute', 'proximity', 'custom'],
Expand All @@ -55,7 +47,7 @@ public function testInitSearchIndex(): void
'removeWordsIfNoResults' => 'allOptional',
]);

$index->expects(self::once())
$this->client->expects(self::once())
->method('clearObjects');

$this->searchIndexer->initSearchIndex();
Expand All @@ -70,13 +62,6 @@ public function testBuildSearchIndexes(): void
]);
$version = new ProjectVersion(['slug' => '1.0']);

$index = $this->createMock(SearchIndex::class);

$this->client->expects(self::once())
->method('initIndex')
->with(SearchIndexer::INDEX_NAME)
->willReturn($index);

$documents = $this->buildDocuments(__DIR__ . '/resources/search-indexer');

$expectedRecords = [
Expand Down Expand Up @@ -199,9 +184,9 @@ public function testBuildSearchIndexes(): void
],
];

$index->expects(self::once())
$this->client->expects(self::once())
->method('saveObjects')
->with($expectedRecords, ['autoGenerateObjectIDIfNotExist' => true]);
->with(SearchIndexer::INDEX_NAME, $expectedRecords, ['autoGenerateObjectIDIfNotExist' => true]);

$this->searchIndexer->buildSearchIndexes($project, $version, $documents);
}
Expand All @@ -215,13 +200,6 @@ public function testBuildSearchIndexesContainingQuotes(): void
]);
$version = new ProjectVersion(['slug' => '1.0']);

$index = $this->createMock(SearchIndex::class);

$this->client->expects(self::once())
->method('initIndex')
->with(SearchIndexer::INDEX_NAME)
->willReturn($index);

$documents = $this->buildDocuments(__DIR__ . '/resources/search-indexer-with-quotes');

$expectedRecords = [
Expand Down Expand Up @@ -266,9 +244,9 @@ public function testBuildSearchIndexesContainingQuotes(): void
],
];

$index->expects(self::once())
$this->client->expects(self::once())
->method('saveObjects')
->with($expectedRecords, ['autoGenerateObjectIDIfNotExist' => true]);
->with(SearchIndexer::INDEX_NAME, $expectedRecords, ['autoGenerateObjectIDIfNotExist' => true]);

$this->searchIndexer->buildSearchIndexes($project, $version, $documents);
}
Expand Down

0 comments on commit 2bab51b

Please sign in to comment.