diff --git a/includes/indices/class-algolia-index.php b/includes/indices/class-algolia-index.php index 50f96107..ac801b2f 100644 --- a/includes/indices/class-algolia-index.php +++ b/includes/indices/class-algolia-index.php @@ -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 ); @@ -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(); diff --git a/includes/indices/class-algolia-posts-index.php b/includes/indices/class-algolia-posts-index.php index 9de8700f..fd1ce9b6 100644 --- a/includes/indices/class-algolia-posts-index.php +++ b/includes/indices/class-algolia-posts-index.php @@ -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 ); } @@ -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 ); } } diff --git a/includes/indices/class-algolia-searchable-posts-index.php b/includes/indices/class-algolia-searchable-posts-index.php index ba9191c8..96b1aad4 100644 --- a/includes/indices/class-algolia-searchable-posts-index.php +++ b/includes/indices/class-algolia-searchable-posts-index.php @@ -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 ); } @@ -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 ); + } }