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

Cache the post mapping version #2445

Merged
merged 1 commit into from
Nov 11, 2021
Merged
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
38 changes: 29 additions & 9 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,36 @@ public function get_mapping_name() {
* @return string|WP_Error|false $version
*/
public function determine_mapping_version() {
$index = $this->get_index_name();
$mapping = Elasticsearch::factory()->get_mapping( $index );
$version = get_transient( 'ep_post_mapping_version' );

if ( empty( $mapping ) ) {
return new \WP_Error( 'ep_failed_mapping_version', esc_html__( 'Error while fetching the mapping version.', 'elasticpress' ) );
}
if ( empty( $version ) ) {
$index = $this->get_index_name();
$mapping = Elasticsearch::factory()->get_mapping( $index );

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

$version = $this->determine_mapping_version_based_on_existing( $mapping, $index );
if ( ! isset( $mapping[ $index ] ) ) {
return false;
}

$version = $this->determine_mapping_version_based_on_existing( $mapping, $index );

set_transient(
'ep_post_mapping_version',
$version,
/**
* Filter the post mapping version cache expiration.
*
* @hook ep_post_mapping_version_cache_expiration
* @since 4.0.0
* @param {int} $version Time in seconds for the transient expiration
* @return {int} New time
*/
apply_filters( 'ep_post_mapping_version_cache_expiration', DAY_IN_SECONDS )
);
}

/**
* Filter the mapping version for posts.
Expand Down Expand Up @@ -328,6 +346,8 @@ public function put_mapping() {
*/
$mapping = apply_filters( 'ep_post_mapping', $mapping );

delete_transient( 'ep_post_mapping_version' );

return Elasticsearch::factory()->put_mapping( $this->get_index_name(), $mapping );
}

Expand Down