diff --git a/src/Loupe/Index.php b/src/Loupe/Index.php index d1bdd35..31d8602 100644 --- a/src/Loupe/Index.php +++ b/src/Loupe/Index.php @@ -6,6 +6,7 @@ use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Loupe\Loupe\BrowseParameters; use Loupe\Loupe\Config\TypoTolerance; use Loupe\Loupe\Configuration; use Loupe\Loupe\Loupe; @@ -231,16 +232,55 @@ public function insertDocuments(Documents $documents) public function update() { - $this->truncateIndex(); + $pruneCandidates = $this->indexedDocumentIds(); - $this->searchables()->lazy()->each(fn ($searchables) => $this->insertMultiple($searchables)); + $this->searchables()->lazy()->each(function ($searchables) use (&$pruneCandidates) { + $references = $searchables->map(function ($reference) use (&$pruneCandidates) { + unset($pruneCandidates[$reference]); + + return $reference; + }); + + $this->insertMultiple($references); + }); + + if ($pruneCandidates !== []) { + $this->client()->deleteDocuments(array_keys($pruneCandidates)); + } return $this; } + /** + * @return array + */ + protected function indexedDocumentIds(): array + { + $ids = []; + $page = 1; + + do { + $result = $this->client()->browse( + BrowseParameters::create() + ->withAttributesToRetrieve(['id']) + ->withHitsPerPage(BrowseParameters::MAX_LIMIT) + ->withPage($page) + ); + + foreach ($result->getHits() as $hit) { + $ids[$hit['id']] = true; + } + + $page++; + } while ($page <= $result->getTotalPages()); + + return $ids; + } + protected function deleteIndex() { - $this->filesystem->cleanDirectory($this->path()); + $this->client = null; + $this->filesystem->cleanDirectory($this->dir()); } protected function createIndex() diff --git a/tests/Feature/IndexTest.php b/tests/Feature/IndexTest.php index fb91f2d..b9aa3af 100644 --- a/tests/Feature/IndexTest.php +++ b/tests/Feature/IndexTest.php @@ -1,12 +1,18 @@ basePath = fixtures_path('indexes', random_int(11, 99999999)); @@ -73,6 +79,97 @@ $this->assertCount(2, $index->lookup('Entry')); }); +it('keeps existing documents searchable while an update is queued', function () { + $index = Search::index(); + + Collection::make()->handle('pages')->title('Pages')->save(); + + Entry::make() + ->id('test-1') + ->collection('pages') + ->data(['title' => 'Entry 1']) + ->save(); + + expect($index->client()->countDocuments())->toBe(1); + + Queue::fake(); + + $index->update(); + + Queue::assertPushed(InsertMultipleJob::class); + expect($index->client()->countDocuments())->toBe(1); +}); + +it('does not prune documents inserted while an update is running', function () { + $index = new class(app(Factory::class), app(Filesystem::class), 'concurrent', ['path' => $this->basePath, 'searchables' => [], 'fields' => ['title']]) extends Index + { + public LazyCollection $references; + + public function searchables() + { + return new class($this->references) + { + public function __construct(private LazyCollection $references) {} + + public function lazy(): LazyCollection + { + return collect([$this->references])->lazy(); + } + }; + } + }; + + $index->insertDocuments(new Documents([ + 'stale::1' => ['title' => 'Stale'], + ])); + + $index->references = LazyCollection::make(function () use ($index) { + $index->insertDocuments(new Documents([ + 'entry::concurrent' => ['title' => 'Concurrent'], + ])); + + yield 'entry::current'; + }); + + Queue::fake(); + + $index->update(); + + $ids = $index->client()->browse( + BrowseParameters::create()->withAttributesToRetrieve(['id']) + )->getHits(); + + expect(collect($ids)->pluck('id')->all())->toBe(['entry::concurrent']); +}); + +it('prunes stale documents across all browse result pages', function () { + $index = Search::index(); + $documents = collect(range(1, 1001))->mapWithKeys( + fn (int $id) => ["stale::$id" => ['title' => "Stale $id"]] + ); + + $index->insertDocuments(new Documents($documents)); + + expect($index->client()->countDocuments())->toBe(1001); + + Queue::fake(); + + $index->update(); + + expect($index->client()->countDocuments())->toBe(0); +}); + +it('deletes the index contents from its directory', function () { + $index = Search::index(); + $index->client(); + + expect($index->exists())->toBeTrue(); + + (fn () => $this->deleteIndex())->call($index); + + expect($index->exists())->toBeFalse(); +}); + it('updates documents in the index', function () { $index = Search::index();