Skip to content

Commit

Permalink
Merge pull request #4033 from 10up/feature/issue-3965
Browse files Browse the repository at this point in the history
If using the new way to index meta, avoid querying distinct meta fields in the sync page
  • Loading branch information
felipeelia authored Dec 10, 2024
2 parents 9b9ad02 + aa2c603 commit ee4617e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
10 changes: 8 additions & 2 deletions includes/classes/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
40 changes: 40 additions & 0 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
7 changes: 7 additions & 0 deletions tests/php/TestAdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
27 changes: 27 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

0 comments on commit ee4617e

Please sign in to comment.