Skip to content
This repository has been archived by the owner on Jan 25, 2021. It is now read-only.

Commit

Permalink
$algolia->index(): Upload objects in batches
Browse files Browse the repository at this point in the history
Fixes #5.
  • Loading branch information
lukasbestle committed Aug 25, 2017
1 parent 32bf9c5 commit ecbafca
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/algolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,19 @@ public function index() {
$tempIndex->clearIndex();

// Add all objects back in
$tempIndex->saveObjects($this->objects());
$queue = array();
foreach(site()->index()->filter(array($this, 'isIndexable')) as $p) {
$queue[] = $this->formatPage($p);

// Always upload objects in batches of 100 for performance reasons
if(count($queue) >= 100) {
$tempIndex->saveObjects($queue);
$queue = array();
}
}

// Upload the remaining objects
$tempIndex->saveObjects($queue);

// Move the temp index to the main index
$this->algolia->moveIndex($this->getIndexName(true), $this->getIndexName());
Expand Down Expand Up @@ -289,16 +301,6 @@ public function objectCount() {
return site()->index()->filter(array($this, 'isIndexable'))->count();
}

/**
* Returns an array of data to send to Algolia
* Includes all whitelisted pages and formats their data according to the configuration
*
* @return array
*/
protected function objects() {
return site()->index()->filter(array($this, 'isIndexable'))->toArray(array($this, 'formatPage'));
}

/**
* Returns the correct Algolia index (temporary or main)
*
Expand Down

0 comments on commit ecbafca

Please sign in to comment.