Skip to content
This repository has been archived by the owner on May 9, 2019. It is now read-only.

Commit

Permalink
fix: remove usage of deleteby to avoid delaying indexing
Browse files Browse the repository at this point in the history
Closes: #708
  • Loading branch information
rayrutjes committed Jan 15, 2018
1 parent 4f3f419 commit 6296fab
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 74 deletions.
14 changes: 1 addition & 13 deletions includes/indices/class-algolia-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,10 @@ public function re_index( $page ) {

$items = $this->get_items( $page, $batch_size );

// Give an opportunity to do some work before actual re-indexing takes place.
$this->before_re_index_items( $items );

$records = array();
foreach ( $items as $item ) {
if ( ! $this->should_index( $item ) ) {
$this->delete_item( $item );
continue;
}
do_action( 'algolia_before_get_records', $item );
Expand All @@ -267,16 +265,6 @@ public function re_index( $page ) {
}
}

protected function before_re_index_items( array $items ) {
foreach ( $items as $item ) {
if ( ! $this->should_index( $item ) ) {
// Always try to delete items that should not be indexed.
$this->delete_item( $item );
continue;
}
}
}

public function create_index_if_not_existing( $clear_if_existing = true ) {
$index = $this->get_index();

Expand Down
46 changes: 22 additions & 24 deletions includes/indices/class-algolia-posts-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,17 @@ protected function update_records( $item, array $records ) {
*/
private function update_post_records( WP_Post $post, array $records ) {
// If there are no records, parent `update_records` will take care of the deletion.
// I case of posts, we ALWAYS need to delete existing records.
// In case of posts, we ALWAYS need to delete existing records.
if ( ! empty( $records ) ) {
$this->delete_item( $post );
}

parent::update_records( $post, $records );

// Keep track of the new record count for future updates relying on the objectID's naming convention .
$new_records_count = count( $records );
$this->set_post_records_count( $post, $new_records_count );

do_action( 'algolia_posts_index_post_updated', $post, $records );
do_action( 'algolia_posts_index_post_' . $post->post_type . '_updated', $post, $records );
}
Expand Down Expand Up @@ -320,37 +324,31 @@ protected function get_items( $page, $batch_size ) {
*/
public function delete_item( $item ) {
$this->assert_is_supported( $item );
$this->get_index()->deleteBy(
array(
'filters' => 'post_id=' . $item->ID,
)
);
}

private function delete_posts( array $post_ids ) {
if ( empty( $post_ids ) ) {
return;
$records_count = $this->get_post_records_count( $item->ID );
$object_ids = array();
for ( $i = 0; $i < $records_count; $i++ ) {
$object_ids[] = $this->get_post_object_id( $item->ID, $i );
}

$filters = array();
foreach ( $post_ids as $post_id ) {
$filters[] = 'post_id=' . $post_id;
if ( ! empty( $object_ids ) ) {
$this->get_index()->deleteObjects( $object_ids );
}

$this->get_index()->deleteBy(
array(
'filters' => implode( ' OR ', $filters ),
)
);
}

/**
* Delete all post records for the current batch being re-indexed.
* @param int $post_id
*
* @param array $items
* @return int
*/
private function get_post_records_count( $post_id ) {
return (int) get_post_meta( (int) $post_id, 'algolia_' . $this->get_id() . '_records_count', true );
}
/**
* @param WP_Post $post
* @param int $count
*/
protected function before_re_index_items( array $items ) {
$post_ids = wp_list_pluck( $items, 'ID' );
$this->delete_posts( $post_ids );
private function set_post_records_count( WP_Post $post, $count ) {
update_post_meta( (int) $post->ID, 'algolia_' . $this->get_id() . '_records_count', (int) $count );
}
}
71 changes: 34 additions & 37 deletions includes/indices/class-algolia-searchable-posts-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,18 @@ protected function update_records( $item, array $records ) {
* @param array $records
*/
private function update_post_records( WP_Post $post, array $records ) {
// If there are no records, parent `update_records` will take care of the deletion.
// I case of posts, we ALWAYS need to delete existing records.
if ( ! empty( $records ) ) {
$this->delete_item( $post );
}
// If there are no records, parent `update_records` will take care of the deletion.
// In case of posts, we ALWAYS need to delete existing records.
if ( ! empty( $records ) ) {
$this->delete_item( $post );
}

// Update the other records.
parent::update_records( $post, $records );

// Keep track of the new record count for future updates relying on the objectID's naming convention .
$new_records_count = count( $records );
$this->set_post_records_count( $post, $new_records_count );

do_action( 'algolia_searchable_posts_index_post_updated', $post, $records );
do_action( 'algolia_searchable_posts_index_post_' . $post->post_type . '_updated', $post, $records );
}
Expand Down Expand Up @@ -311,37 +314,31 @@ protected function get_items( $page, $batch_size ) {
*/
public function delete_item( $item ) {
$this->assert_is_supported( $item );
$this->get_index()->deleteBy(
array(
'filters' => 'post_id=' . $item->ID,
)
);

$records_count = $this->get_post_records_count( $item->ID );
$object_ids = array();
for ( $i = 0; $i < $records_count; $i++ ) {
$object_ids[] = $this->get_post_object_id( $item->ID, $i );
}

if ( ! empty( $object_ids ) ) {
$this->get_index()->deleteObjects( $object_ids );
}
}

private function delete_posts( array $post_ids ) {
if ( empty( $post_ids ) ) {
return;
}

$filters = array();
foreach ( $post_ids as $post_id ) {
$filters[] = 'post_id=' . $post_id;
}

$this->get_index()->deleteBy(
array(
'filters' => implode( ' OR ', $filters ),
)
);
}

/**
* Delete all post records for the current batch being re-indexed.
*
* @param array $items
*/
protected function before_re_index_items( array $items ) {
$post_ids = wp_list_pluck( $items, 'ID' );
$this->delete_posts( $post_ids );
}
/**
* @param int $post_id
*
* @return int
*/
private function get_post_records_count( $post_id ) {
return (int) get_post_meta( (int) $post_id, 'algolia_' . $this->get_id() . '_records_count', true );
}
/**
* @param WP_Post $post
* @param int $count
*/
private function set_post_records_count( WP_Post $post, $count ) {
update_post_meta( (int) $post->ID, 'algolia_' . $this->get_id() . '_records_count', (int) $count );
}
}

0 comments on commit 6296fab

Please sign in to comment.