Skip to content

Commit

Permalink
WP-CLI: stats and status to output all indices info
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Nov 29, 2021
1 parent 5a9662f commit 2f2d3d1
Showing 1 changed file with 6 additions and 51 deletions.
57 changes: 6 additions & 51 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1228,23 +1228,7 @@ public function status() {

$request_args = [ 'headers' => Elasticsearch::factory()->format_request_headers() ];

$sites = ( is_multisite() ) ? Utils\get_sites() : array( 'blog_id' => get_current_blog_id() );

$term_indexable = Indexables::factory()->get( 'term' );

foreach ( $sites as $site ) {
$index_names[] = Indexables::factory()->get( 'post' )->get_index_name( $site['blog_id'] );

if ( ! empty( $term_indexable ) ) {
$index_names[] = $term_indexable->get_index_name( $site['blog_id'] );
}
}

$user_indexable = Indexables::factory()->get( 'user' );

if ( ! empty( $user_indexable ) ) {
$index_names[] = $user_indexable->get_index_name();
}
$registered_index_names = $this->get_index_names();

$response_cat_indices = Elasticsearch::factory()->remote_request( '_cat/indices?format=json' );

Expand All @@ -1257,7 +1241,7 @@ public function status() {
if ( is_array( $indexes_from_cat_indices_api ) ) {
$indexes_from_cat_indices_api = wp_list_pluck( $indexes_from_cat_indices_api, 'index' );

$index_names = array_intersect( $index_names, $indexes_from_cat_indices_api );
$index_names = array_intersect( $registered_index_names, $indexes_from_cat_indices_api );
} else {
WP_CLI::error( esc_html__( 'Failed to return status.', 'elasticpress' ) );
}
Expand Down Expand Up @@ -1289,24 +1273,7 @@ public function stats() {

$request_args = array( 'headers' => Elasticsearch::factory()->format_request_headers() );

$sites = ( is_multisite() ) ? Utils\get_sites() : array( 'blog_id' => get_current_blog_id() );

$post_indexable = Indexables::factory()->get( 'post' );
$term_indexable = Indexables::factory()->get( 'term' );

foreach ( $sites as $site ) {
$index_names[] = $post_indexable->get_index_name( $site['blog_id'] );

if ( ! empty( $term_indexable ) ) {
$index_names[] = $term_indexable->get_index_name( $site['blog_id'] );
}
}

$user_indexable = Indexables::factory()->get( 'user' );

if ( ! empty( $user_indexable ) ) {
$index_names[] = $user_indexable->get_index_name();
}
$registered_index_names = $this->get_index_names();

$response_cat_indices = Elasticsearch::factory()->remote_request( '_cat/indices?format=json' );

Expand All @@ -1319,7 +1286,7 @@ public function stats() {
if ( is_array( $indexes_from_cat_indices_api ) ) {
$indexes_from_cat_indices_api = wp_list_pluck( $indexes_from_cat_indices_api, 'index' );

$index_names = array_intersect( $index_names, $indexes_from_cat_indices_api );
$index_names = array_intersect( $registered_index_names, $indexes_from_cat_indices_api );
} else {
WP_CLI::error( esc_html__( 'Failed to return stats.', 'elasticpress' ) );
}
Expand All @@ -1333,20 +1300,8 @@ public function stats() {
}
$body = json_decode( wp_remote_retrieve_body( $request ), true );

foreach ( $sites as $site ) {
$current_index = $post_indexable->get_index_name( $site['blog_id'] );

$this->render_stats( $current_index, $body );

if ( $term_indexable ) {
$this->render_stats( $term_indexable->get_index_name( $site['blog_id'] ), $body );
}
}

if ( ! empty( $user_indexable ) ) {
$user_index = $user_indexable->get_index_name();

$this->render_stats( $user_index, $body );
foreach ( $registered_index_names as $index_name ) {
$this->render_stats( $index_name, $body );
}
}

Expand Down

0 comments on commit 2f2d3d1

Please sign in to comment.