From c549863701eab77500bc8fd70d07d605cb94390b Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 10 Dec 2024 14:59:22 -0300 Subject: [PATCH 1/3] If using the new way to index meta, avoid querying distinct meta fields in the sync page --- includes/classes/AdminNotices.php | 10 ++++++++-- tests/php/indexables/TestPost.php | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/includes/classes/AdminNotices.php b/includes/classes/AdminNotices.php index da03b779e..175d37d30 100644 --- a/includes/classes/AdminNotices.php +++ b/includes/classes/AdminNotices.php @@ -876,8 +876,14 @@ public static function factory() { protected function check_field_count() { $post_indexable = Indexables::factory()->get( 'post' ); - $indexable_fields = $post_indexable->get_predicted_indexable_meta_keys(); - $count_fields_db = count( $indexable_fields ); + $search = Features::factory()->get_registered_feature( 'search' ); + if ( $search && ! empty( $search->weighting ) && 'manual' === $search->weighting->get_meta_mode() ) { + $indexable_fields = $post_indexable->get_all_allowed_metas_manual(); + $count_fields_db = count( $indexable_fields ); + } else { + $indexable_fields = $post_indexable->get_predicted_indexable_meta_keys(); + $count_fields_db = count( $indexable_fields ); + } $index_name = $post_indexable->get_index_name(); $es_field_limit = Elasticsearch::factory()->get_index_total_fields_limit( $index_name ); diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index e351b31b7..e851d8494 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -9215,4 +9215,31 @@ public function test_aggregations_return() { $this->assertArrayHasKey( 'ep_aggregations', $query->query_vars ); $this->assertArrayHasKey( 'my_aggs', $query->query_vars['ep_aggregations'] ); } + + /** + * Test the get_all_allowed_metas_manual method + * + * @since 5.1.4 + * @group post + */ + public function test_get_all_allowed_metas_manual() { + // Remove product meta data to avoid some noise. + ElasticPress\Features::factory()->get_registered_feature( 'woocommerce' )->tear_down(); + + // Add some meta data using the Weighting Dashboard + $set_changed_weighting = function( $weighting_default ) { + $weighting_default['post']['meta.allowed_weighting_dashboard.value'] = [ + 'enabled' => true, + 'weight' => 1, + ]; + return $weighting_default; + }; + add_filter( 'ep_weighting_configuration', $set_changed_weighting ); + + $allowed_metas_manual = ElasticPress\Indexables::factory()->get( 'post' )->get_all_allowed_metas_manual(); + + $this->assertContains( 'allowed_weighting_dashboard', $allowed_metas_manual ); + // Added using the `ep_prepare_meta_allowed_keys` in the test set_up method. + $this->assertContains( 'test_key6', $allowed_metas_manual ); + } } From 82fa2617df92f88d0cf7096bb403732e044c3ad7 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 10 Dec 2024 15:28:37 -0300 Subject: [PATCH 2/3] get_all_allowed_metas_manual method --- includes/classes/Indexable/Post/Post.php | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 7a4ec1d7c..a04280d84 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -2976,4 +2976,44 @@ public function add_term_suggest_field( array $mapping ) : array { return $mapping; } + + /** + * Return all meta data added to the Weighting Dashboard plus all allowed keys via code. + * + * @since 5.1.4 + * @return array + */ + public function get_all_allowed_metas_manual() : array { + $post_types = \ElasticPress\Indexables::factory()->get( 'post' )->get_indexable_post_types(); + $search_feature = \ElasticPress\Features::factory()->get_registered_feature( 'search' ); + $weighting = $search_feature->weighting->get_weighting_configuration_with_defaults(); + $fake_post = new \WP_Post( new \stdClass() ); + + $all_allowed_metas = []; + foreach ( $post_types as $post_type ) { + $fake_post->post_type = $post_type; + $allowed_protected_keys = apply_filters( 'ep_prepare_meta_allowed_protected_keys', [], $fake_post ); + + $selected_keys = []; + if ( ! empty( $weighting[ $post_type ] ) ) { + $selected_keys = array_map( + function ( $field ) { + if ( false === strpos( $field, 'meta.' ) ) { + return null; + } + $field_name_parts = explode( '.', $field ); + return $field_name_parts[1]; + }, + array_keys( $weighting[ $post_type ] ) + ); + $selected_keys = array_filter( $selected_keys ); + } + + $allowed_keys = apply_filters( 'ep_prepare_meta_allowed_keys', array_merge( $allowed_protected_keys, $selected_keys ), $fake_post ); + + $all_allowed_metas = array_merge( $all_allowed_metas, $allowed_keys ); + } + + return array_unique( $all_allowed_metas ); + } } From aa2c603588faeca3b448ab295938c16b8a125f92 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 10 Dec 2024 17:01:45 -0300 Subject: [PATCH 3/3] Adjust test --- tests/php/TestAdminNotices.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/php/TestAdminNotices.php b/tests/php/TestAdminNotices.php index e9125ab39..70ca80e9b 100644 --- a/tests/php/TestAdminNotices.php +++ b/tests/php/TestAdminNotices.php @@ -561,6 +561,13 @@ function() use ( $es_version ) { * @since 4.4.0 */ public function testTooManyFieldsNoticeInAdmin() { + add_filter( + 'ep_meta_mode', + function() { + return 'auto'; + } + ); + add_filter( 'ep_prepare_meta_allowed_keys', function( $allowed_metakeys ) {