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

Failed Queries: Different message if no index is found #3436

Merged
merged 9 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
New get_indices_comparison() method
  • Loading branch information
felipeelia committed Apr 6, 2023
commit 58b0bddc45a3f35029414b0b3ea875537aabc56b
18 changes: 18 additions & 0 deletions includes/classes/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,24 @@ public function get_cluster_indices() : array {
return (array) json_decode( wp_remote_retrieve_body( $response ), true );
}

/**
* Return a comparison between which indices should be and are present in the ES server.
*
* @since 5.0.0
* @return array Array with `missing_indices` and `present_indices` keys.
*/
public function get_indices_comparison() {
$all_index_names = $this->get_index_names();
$cluster_indices = $this->get_cluster_indices();

$cluster_index_names = wp_list_pluck( $cluster_indices, 'index' );

return [
'missing_indices' => array_diff( $all_index_names, $cluster_index_names ),
'present_indices' => array_intersect( $all_index_names, $cluster_index_names ),
];
}

/**
* Given an index return its total fields limit
*
Expand Down
57 changes: 57 additions & 0 deletions tests/php/TestElasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,61 @@ public function testFormatRequestHeaders() {
$this->assertCount( 4, $new_headers ); // 3 old + 1 new
$this->assertSame( 'totally custom', $new_headers['X-Custom'] );
}

/**
* Test the get_indices_comparison method
*
* @since 5.0.0
*/
public function testGetIndicesComparison() {
ElasticPress\Features::factory()->activate_feature( 'terms' );
ElasticPress\Features::factory()->setup_features();

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

$post_indexable->put_mapping();
$term_indexable->put_mapping();

/**
* All indices are present
*/
$expected = [
'missing_indices' => [],
'present_indices' => [
$post_indexable->get_index_name(),
$term_indexable->get_index_name(),
],
];
$this->assertEqualsCanonicalizing( $expected, \ElasticPress\Elasticsearch::factory()->get_indices_comparison() );

/**
* One missing index
*/
$term_indexable->delete_index();

$expected = [
'missing_indices' => [
$term_indexable->get_index_name(),
],
'present_indices' => [
$post_indexable->get_index_name(),
],
];
$this->assertEqualsCanonicalizing( $expected, \ElasticPress\Elasticsearch::factory()->get_indices_comparison() );

/**
* All indices are missing
*/
ElasticPress\Elasticsearch::factory()->delete_all_indices();

$expected = [
'missing_indices' => [
$post_indexable->get_index_name(),
$term_indexable->get_index_name(),
],
'present_indices' => [],
];
$this->assertEqualsCanonicalizing( $expected, \ElasticPress\Elasticsearch::factory()->get_indices_comparison() );
}
}