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

Feature implementation for #1471 #1472

Merged
merged 18 commits into from
Aug 9, 2021
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
90 changes: 87 additions & 3 deletions includes/classes/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ElasticPress\Elasticsearch;
use ElasticPress\Screen;
use ElasticPress\Features;
use ElasticPress\Indexables;
use ElasticPress\Stats;

if ( ! defined( 'ABSPATH' ) ) {
Expand All @@ -40,6 +41,7 @@ class AdminNotices {
'upgrade_sync',
'auto_activate_sync',
'using_autosuggest_defaults',
'maybe_wrong_mapping',
'yellow_health',
];

Expand Down Expand Up @@ -592,6 +594,84 @@ protected function process_host_error_notice() {
];
}

/**
* Determine if the wrong mapping might be installed
*
* Type: error
* Dismiss: Always dismissable per es_version as custom mapping could exist
* Show: All screens
*
* @since 3.6.2
* @return array|bool
*/
protected function process_maybe_wrong_mapping_notice() {
$screen = Screen::factory()->get_current_screen();

if ( 'install' === $screen ) {
return false;
}

// we might have this dismissed
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$dismiss = get_site_option( 'ep_hide_maybe_wrong_mapping_notice', false );
oscarssanchez marked this conversation as resolved.
Show resolved Hide resolved
} else {
$dismiss = get_option( 'ep_hide_maybe_wrong_mapping_notice', false );
}

// we need a host
$host = Utils\get_host();
if ( empty( $host ) ) {
return false;
}

// we also need a version
$es_version = Elasticsearch::factory()->get_elasticsearch_version( false );

if ( false === $es_version || $dismiss === $es_version ) {
felipeelia marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

// we also likely need a sync to have a mapping
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$last_sync = get_site_option( 'ep_last_sync', false );
} else {
$last_sync = get_option( 'ep_last_sync', false );
}

if ( empty( $last_sync ) ) {
return false;
}

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

$mapping_file_wanted = $post_indexable->get_mapping_name();
$mapping_file_current = $post_indexable->determine_mapping_version();
if ( is_wp_error( $mapping_file_current ) ) {
return false;
}

if ( ! $mapping_file_current || $mapping_file_wanted !== $mapping_file_current ) {
$html = sprintf(
/* translators: 1. <em>; 2. </em> */
esc_html__( 'It seems the mapping data in your index does not match the Elasticsearch version used. We recommend to reindex your content using the sync button on the top of the screen or through wp-cli by adding the %1$s--setup%2$s flag', 'elasticpress' ),
'<em>',
'</em>'
);

if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$html .= '<span class="notice-error-es-response-code"> ' . sprintf( esc_html__( 'Current mapping: %1$s. Expected mapping: %2$s', 'elasticpress' ), esc_html( $mapping_file_current ), esc_html( $mapping_file_wanted ) ) . '</span>';
}

return [
'html' => $html,
'type' => 'error',
'dismiss' => true,
];

}

}

/**
* Single node notification. Shows when index health is yellow.
*
Expand Down Expand Up @@ -672,10 +752,15 @@ public function get_notices() {
* @since 3.0
*/
public function dismiss_notice( $notice ) {
$value = true;
// allow version dependent dismissal
if ( in_array( $notice, [ 'maybe_wrong_mapping' ], true ) ) {
$value = Elasticsearch::factory()->get_elasticsearch_version( false );
}
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
update_site_option( 'ep_hide_' . $notice . '_notice', true );
update_site_option( 'ep_hide_' . $notice . '_notice', $value );
} else {
update_option( 'ep_hide_' . $notice . '_notice', true );
update_option( 'ep_hide_' . $notice . '_notice', $value );
}
}

Expand All @@ -695,4 +780,3 @@ public static function factory() {
return $instance;
}
}

102 changes: 98 additions & 4 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ public function get_indexable_post_status() {
}

/**
* Send mapping to Elasticsearch
* Determine required mapping file
*
* @since 3.0
* @return array
* @since 3.6.2
* @return string
*/
public function put_mapping() {
public function get_mapping_name() {
$es_version = Elasticsearch::factory()->get_elasticsearch_version();

if ( empty( $es_version ) ) {
Expand All @@ -267,6 +267,100 @@ public function put_mapping() {
$mapping_file = '7-0.php';
}

return apply_filters( 'ep_post_mapping_version', $mapping_file );
}

/**
* Determine version of mapping currently on the post index.
*
* @since 3.6.2
* @return string|WP_Error|false $version
*/
public function determine_mapping_version() {
$index = $this->get_index_name();
$mapping = Elasticsearch::factory()->get_mapping( $index );

if ( empty( $mapping ) ) {
return new \WP_Error( 'ep_failed_mapping_version', esc_html__( 'Error while fetching the mapping version.', 'elasticpress' ) );
}

if ( ! isset( $mapping[ $index ] ) ) {
return false;
}

$version = 'unknown';

if ( isset( $mapping[ $index ]['mappings']['post']['_meta']['mapping_version'] ) ) {
$version = $mapping[ $index ]['mappings']['post']['_meta']['mapping_version'];
} elseif ( isset( $mapping[ $index ]['mappings']['_meta']['mapping_version'] ) ) {
$version = $mapping[ $index ]['mappings']['_meta']['mapping_version'];
}

// mapping does not have meta value set - use legacy detection
if ( 'unknown' === $version ) {

// check for pre-5-0 mapping
if ( isset( $mapping[ $index ]['mappings']['post']['properties']['post_name']['fields']['raw']['ignore_above'] ) ) {
$val = $mapping[ $index ]['mappings']['post']['properties']['post_name']['fields']['raw']['ignore_above'];
if ( ! $val || 10922 !== $val ) {
$version = 'pre-5-0.php';
} elseif ( $val && 10922 === $val ) {
$version = 'not-pre-5-0';
}
}

// check for 5-0 mapping
if ( 'not-pre-5-0' === $version ) {
if ( isset( $mapping[ $index ]['mappings']['post']['properties']['post_content_filtered']['fields'] ) ) {
$version = '5-0.php';
} else {
$version = 'not-5-0';
}
}

// check for 5-2 mapping
if ( 'not-5-0' === $version ) {
if ( isset( $mapping[ $index ]['mappings']['post']['_all'] ) ) {
$version = '5-2.php';
} else {
$version = 'not-5-2';
}
}

// check for 7-0 mapping
if ( 'not-5-2' === $version ) {
if ( isset( $mapping[ $index ]['settings']['index.max_shingle_diff'] ) ) {
$version = '7-0.php';
} else {
$version = 'not-7-0';
}
}

if ( preg_match( '/^not-.*/', $version ) ) {
$version = 'unknown';
}
}

/**
* Filter the mapping version for posts.
*
* @hook ep_post_mapping_version_determined
* @since 3.6.2
* @param {string} $version Determined version string
* @return {string} New version string
*/
return apply_filters( 'ep_post_mapping_version_determined', $version );
}

/**
* Send mapping to Elasticsearch
*
* @since 3.0
* @return array
*/
public function put_mapping() {
$mapping_file = $this->get_mapping_name();

/**
* Filter post indexable mapping file
*
Expand Down
3 changes: 3 additions & 0 deletions includes/mappings/post/5-0.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
),
'mappings' => array(
'post' => array(
'_meta' => array(
'mapping_version' => '5-0.php',
),
'date_detection' => false,
'dynamic_templates' => array(
array(
Expand Down
3 changes: 3 additions & 0 deletions includes/mappings/post/5-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
),
'mappings' => array(
'post' => array(
'_meta' => array(
'mapping_version' => '5-2.php',
),
'date_detection' => false,
'dynamic_templates' => array(
array(
Expand Down
3 changes: 3 additions & 0 deletions includes/mappings/post/7-0.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@
),
),
'mappings' => array(
'_meta' => array(
'mapping_version' => '7-0.php',
),
'date_detection' => false,
'dynamic_templates' => array(
array(
Expand Down
3 changes: 3 additions & 0 deletions includes/mappings/post/pre-5-0.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
),
'mappings' => array(
'post' => array(
'_meta' => array(
'mapping_version' => 'pre-5-0.php',
),
'date_detection' => false,
'dynamic_templates' => array(
array(
Expand Down
Loading