Skip to content
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

Single sync option if index is deleted #3011

Merged
merged 22 commits into from
Nov 9, 2022
Merged
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
25 changes: 2 additions & 23 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,8 @@ public function get_mapping( $args, $assoc_args ) {
* @param array $assoc_args Associative CLI args.
*/
public function get_cluster_indices( $args, $assoc_args ) {
$path = '_cat/indices?format=json';

$response = Elasticsearch::factory()->remote_request( $path );
$response = Elasticsearch::factory()->get_cluster_indices();

$this->print_json_response( $response, ! empty( $assoc_args['pretty'] ) );
}
Expand Down Expand Up @@ -441,27 +440,7 @@ public function get_indices( $args, $assoc_args ) {
* @return array
*/
protected function get_index_names() {
$sites = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ? Utils\get_sites() : array( array( 'blog_id' => get_current_blog_id() ) );

$all_indexables = Indexables::factory()->get_all();

$global_indexes = [];
$non_global_indexes = [];
foreach ( $all_indexables as $indexable ) {
if ( $indexable->global ) {
$global_indexes[] = $indexable->get_index_name();
continue;
}

foreach ( $sites as $site ) {
if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) {
continue;
}
$non_global_indexes[] = $indexable->get_index_name( $site['blog_id'] );
}
}

return array_merge( $non_global_indexes, $global_indexes );
return Elasticsearch::factory()->get_index_names();
}

/**
Expand Down
46 changes: 46 additions & 0 deletions includes/classes/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace ElasticPress;

use ElasticPress\Utils as Utils;
use ElasticPress\Indexables;
use \WP_Error as WP_Error;

if ( ! defined( 'ABSPATH' ) ) {
Expand Down Expand Up @@ -1615,4 +1616,49 @@ protected function add_query_log( $query ) {
do_action( 'ep_add_query_log', $query );
}

/**
* Get all index names.
*
* @since 4.4.0
* @return array
*/
public function get_index_names() {
$sites = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ? Utils\get_sites() : array( array( 'blog_id' => get_current_blog_id() ) );

$all_indexables = Indexables::factory()->get_all();

$global_indexes = [];
$non_global_indexes = [];
foreach ( $all_indexables as $indexable ) {
if ( $indexable->global ) {
$global_indexes[] = $indexable->get_index_name();
continue;
}

foreach ( $sites as $site ) {
if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) {
continue;
}
$non_global_indexes[] = $indexable->get_index_name( $site['blog_id'] );
}
}

return array_merge( $non_global_indexes, $global_indexes );
}

/**
* Return all indices from the cluster.
*
* @since 4.4.0
* @return WP_Error|array WP_Error on failure or The response
*/
public function get_cluster_indices() {
$path = '_cat/indices?format=json';

$response = $this->remote_request( $path );

return $response;

}

}
18 changes: 17 additions & 1 deletion includes/classes/Screen/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use ElasticPress\IndexHelper;
use ElasticPress\Screen;
use ElasticPress\Utils;
use ElasticPress\Stats;
use ElasticPress\Elasticsearch;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
Expand Down Expand Up @@ -174,7 +176,21 @@ public function admin_enqueue_scripts() {

$ep_last_index = IndexHelper::factory()->get_last_index();

if ( ! empty( $ep_last_index ) ) {
$sync_required = false;
$all_index_names = Elasticsearch::factory()->get_index_names();
$cluster_indices = Elasticsearch::factory()->get_cluster_indices();
$cluster_indices = json_decode( wp_remote_retrieve_body( $cluster_indices ), true );

if ( is_array( $cluster_indices ) ) {
$cluster_index_names = wp_list_pluck( $cluster_indices, 'index' );
$synced_index_names = array_intersect( $all_index_names, $cluster_index_names );

if ( $synced_index_names !== $all_index_names ) {
$sync_required = true;
}
}

if ( ! empty( $ep_last_index ) && ! $sync_required ) {
$data['ep_last_sync_date'] = ! empty( $ep_last_index['end_date_time'] ) ? $ep_last_index['end_date_time'] : false;
$data['ep_last_sync_failed'] = ! empty( $ep_last_index['failed'] ) ? true : false;
}
Expand Down
34 changes: 31 additions & 3 deletions tests/cypress/integration/dashboard-sync.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('Dashboard Sync', () => {
);

cy.visitAdminPage('admin.php?page=elasticpress-sync');
cy.get('.ep-sync-button--delete').click();
cy.get('.ep-sync-button--sync').click();
cy.get('.ep-sync-progress strong', {
timeout: Cypress.config('elasticPressIndexTimeout'),
}).should('contain.text', 'Sync complete');
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Dashboard Sync', () => {
);

cy.visitAdminPage('network/admin.php?page=elasticpress-sync');
cy.get('.ep-sync-button--delete').click();
cy.get('.ep-sync-button--sync').click();
cy.get('.ep-sync-progress strong', {
timeout: Cypress.config('elasticPressIndexTimeout'),
}).should('contain.text', 'Sync complete');
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('Dashboard Sync', () => {

// Start sync via dashboard and pause it
cy.intercept('POST', '/wp-admin/admin-ajax.php*').as('ajaxRequest');
cy.get('.ep-sync-button--delete').click();
cy.get('.ep-sync-button--sync').click();
cy.wait('@ajaxRequest').its('response.statusCode').should('eq', 200);
cy.get('.ep-sync-button--pause').should('be.visible');

Expand All @@ -173,4 +173,32 @@ describe('Dashboard Sync', () => {

cy.setPerIndexCycle();
});

it('Should only display a single sync option if index is deleted', () => {
// Enable Terms
cy.wpCli('wp elasticpress activate-feature terms', true);

/**
* The sync page should only show a
* single sync panel.
*/
cy.visitAdminPage('admin.php?page=elasticpress-sync');
cy.get('.ep-sync-panel')
.should('have.length', 1)
.as('syncPanel')
.should('contain.text', 'Run a sync to index your existing content');

// Send mapping of the deleted index
cy.wpCli('wp elasticpress put-mapping --indexables=term');

/**
* After the mapping is sent there should be 2 sync panels
* and the second should contain the delete & sync option.
*/
cy.visitAdminPage('admin.php?page=elasticpress-sync');
cy.get('.ep-sync-panel')
.should('have.length', 2)
.last()
.should('contain.text', 'If you are still having issues with your search results');
});
});