Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Search/Algolia/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ public function __construct(SearchClient $client, $name, $config, $locale)

protected function client(): SearchClient
{
if (! $this->settingsInitialized && isset($this->config['settings']) && ! $this->exists()) {
$this->client->setSettings($this->name, $this->config['settings']);
if (! $this->settingsInitialized && isset($this->config['settings'])) {
if (! $this->exists()) {
$this->client->setSettings($this->name, $this->config['settings']);
}

// Mark as initialized either way. Otherwise, when the index already
// exists, we'd never stop checking and every write would spend an
// extra API call on listIndices().
$this->settingsInitialized = true;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Search/AlgoliaIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Tests\Search;

use Algolia\AlgoliaSearch\Api\SearchClient;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Search\Algolia\Index as AlgoliaIndex;
use Statamic\Search\Documents;
use Tests\TestCase;

class AlgoliaIndexTest extends TestCase
Expand All @@ -21,4 +24,33 @@ public function getIndex($name, $config, $locale)

return new AlgoliaIndex($client, $name, $config, $locale);
}

#[Test]
public function it_only_checks_whether_an_existing_index_exists_once()
{
$client = Mockery::mock(SearchClient::class);
$client->shouldReceive('listIndices')->once()->andReturn(['items' => [['name' => 'test']]]);
$client->shouldNotReceive('setSettings');
$client->shouldReceive('saveObjects')->times(3);

$index = new AlgoliaIndex($client, 'test', ['settings' => ['hitsPerPage' => 20]], null);

$index->insertDocuments(new Documents(collect(['one' => ['title' => 'One']])));
$index->insertDocuments(new Documents(collect(['two' => ['title' => 'Two']])));
$index->insertDocuments(new Documents(collect(['three' => ['title' => 'Three']])));
}

#[Test]
public function it_applies_settings_once_when_the_index_does_not_exist()
{
$client = Mockery::mock(SearchClient::class);
$client->shouldReceive('listIndices')->once()->andReturn(['items' => []]);
$client->shouldReceive('setSettings')->once()->with('test', ['hitsPerPage' => 20]);
$client->shouldReceive('saveObjects')->twice();

$index = new AlgoliaIndex($client, 'test', ['settings' => ['hitsPerPage' => 20]], null);

$index->insertDocuments(new Documents(collect(['one' => ['title' => 'One']])));
$index->insertDocuments(new Documents(collect(['two' => ['title' => 'Two']])));
}
}
Loading