Skip to content

Fixes #442: Running WP_CLI command wp algolia reindex --all would clear index even if --clear flag is not specified #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions includes/class-algolia-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function do_reindex( Algolia_Index $index, $clear, $from_batch ) {
}

if ( 0 === $total_pages ) {
$index->re_index( 1 );
$index->re_index( 1, array(), false ); // Reindex but don't clear the index, we've already done that.
WP_CLI::success( sprintf( 'Index %s was created but no entries were sent.', $index->get_name() ) );

return;
Expand All @@ -144,7 +144,7 @@ private function do_reindex( Algolia_Index $index, $clear, $from_batch ) {
$page = $from_batch;
do {
WP_CLI::log( sprintf( 'Indexing batch %s.', $page ) );
$index->re_index( $page++ );
$index->re_index( $page++, array(), false ); // Reindex but don't clear the index, we've already done that.
WP_CLI::log( sprintf( 'Indexed batch %s.', ( $page - 1 ) ) );
$progress->tick();
} while ( $page <= ( $total_pages + $from_batch - 1 ) );
Expand Down
9 changes: 5 additions & 4 deletions includes/indices/class-algolia-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,20 +420,21 @@ public function get_name( $prefix = null ) {
* @since 1.0.0
* @since 2.6.2 Added $specific_ids parameter
*
* @param int $page Page of the index.
* @param array $specific_ids Array of IDs to specifically fetch and index.
* @param int $page Page of the index.
* @param array $specific_ids Array of IDs to specifically fetch and index.
* @param bool $clear_index_on_page_one Whether to clear the index or not if $page is set to 1.
*
* @throws InvalidArgumentException If the page is less than 1.
*/
public function re_index( $page, $specific_ids = [] ) {
public function re_index( $page, $specific_ids = [], $clear_index_on_page_one = true ) {
$page = (int) $page;

if ( $page < 1 ) {
throw new InvalidArgumentException( 'Page should be superior to 0.' );
}

if ( 1 === $page ) {
$this->create_index_if_not_existing();
$this->create_index_if_not_existing( $clear_index_on_page_one );
}

$batch_size = (int) $this->get_re_index_batch_size();
Expand Down