Skip to content

Commit

Permalink
Merge pull request #3037 from 10up/fix/issue-3033
Browse files Browse the repository at this point in the history
Apply deprecated filters without overwritting values
  • Loading branch information
felipeelia authored Oct 11, 2022
2 parents 2b7e1a4 + d9d6c48 commit 0daae8e
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 8 deletions.
6 changes: 3 additions & 3 deletions includes/classes/SearchAlgorithm/DefaultAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a
*/
$query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated(
'ep_match_phrase_boost',
[ 4, $search_fields, $query_vars ],
[ $query['bool']['should'][0]['multi_match']['boost'], $search_fields, $query_vars ],
'4.3.0',
'ep_post_match_phrase_boost'
);
Expand All @@ -166,7 +166,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a
*/
$query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated(
'ep_match_boost',
[ 2, $search_fields, $query_vars ],
[ $query['bool']['should'][1]['multi_match']['boost'], $search_fields, $query_vars ],
'4.3.0',
'ep_post_match_boost'
);
Expand All @@ -184,7 +184,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a
*/
$query['bool']['should'][2]['multi_match']['fuzziness'] = apply_filters_deprecated(
'ep_fuzziness_arg',
[ 1, $search_fields, $query_vars ],
[ $query['bool']['should'][2]['multi_match']['fuzziness'], $search_fields, $query_vars ],
'4.3.0',
'ep_post_fuzziness_arg'
);
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/SearchAlgorithm/Version_350.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a
/** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */
$query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated(
'ep_match_phrase_boost',
[ 3, $search_fields, $query_vars ],
[ $query['bool']['should'][0]['multi_match']['boost'], $search_fields, $query_vars ],
'4.3.0',
'ep_post_match_phrase_boost'
);
Expand Down
8 changes: 4 additions & 4 deletions includes/classes/SearchAlgorithm/Version_400.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a
/** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */
$query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated(
'ep_match_phrase_boost',
[ 3, $search_fields, $query_vars ],
[ $query['bool']['should'][0]['multi_match']['boost'], $search_fields, $query_vars ],
'4.3.0',
'ep_post_match_phrase_boost'
);

/** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */
$query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated(
'ep_match_boost',
[ 1, $search_fields, $query_vars ],
[ $query['bool']['should'][1]['multi_match']['boost'], $search_fields, $query_vars ],
'4.3.0',
'ep_post_match_boost'
);
Expand All @@ -161,7 +161,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a
*/
$query['bool']['should'][1]['multi_match']['fuzziness'] = apply_filters_deprecated(
'ep_match_fuzziness',
[ 'auto', $search_fields, $query_vars ],
[ $query['bool']['should'][1]['multi_match']['fuzziness'], $search_fields, $query_vars ],
'4.3.0',
'ep_post_match_fuzziness'
);
Expand All @@ -180,7 +180,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a
*/
$query['bool']['should'][2]['multi_match']['boost'] = apply_filters_deprecated(
'ep_match_cross_fields_boost',
[ 1, $search_fields, $query_vars ],
[ $query['bool']['should'][2]['multi_match']['boost'], $search_fields, $query_vars ],
'4.3.0',
'ep_post_match_cross_fields_boost'
);
Expand Down
49 changes: 49 additions & 0 deletions tests/php/searchAlgorithms/TestDefaultSearchAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,55 @@ public function testFilters() {
remove_filter( 'ep_indexable_fuzziness_arg', $test_filter );
}

/**
* Test filters with posts
*
* As posts also apply the legacy filters, these tests assure code honors the value of the newer filters
*
* @see https://github.com/10up/ElasticPress/issues/3033
* @group searchAlgorithms
*/
public function testPostFilters() {
$default = new DefaultAlgorithm();

$search_term = 'search_term';
$search_fields = [ 'post_title', 'post_content' ];

$test_filter = function() {
return 1234;
};

/**
* Test the `ep_post_match_phrase_boost` filter.
*/
add_filter( 'ep_post_match_phrase_boost', $test_filter );

$query = $default->get_query( 'post', $search_term, $search_fields, [] );
$this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] );

remove_filter( 'ep_post_match_phrase_boost', $test_filter );

/**
* Test the `ep_post_match_boost` filter.
*/
add_filter( 'ep_post_match_boost', $test_filter );

$query = $default->get_query( 'post', $search_term, $search_fields, [] );
$this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['boost'] );

remove_filter( 'ep_post_match_boost', $test_filter );

/**
* Test the `ep_post_fuzziness_arg` filter.
*/
add_filter( 'ep_post_fuzziness_arg', $test_filter );

$query = $default->get_query( 'post', $search_term, $search_fields, [] );
$this->assertEquals( 1234, $query['bool']['should'][2]['multi_match']['fuzziness'] );

remove_filter( 'ep_post_fuzziness_arg', $test_filter );
}

/**
* Test deprecated/legacy filters
*
Expand Down
29 changes: 29 additions & 0 deletions tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ public function testFilters() {
remove_filter( 'ep_indexable_match_phrase_boost', $test_filter );
}

/**
* Test filters with posts
*
* As posts also apply the legacy filters, these tests assure code honors the value of the newer filters
*
* @see https://github.com/10up/ElasticPress/issues/3033
* @group searchAlgorithms
*/
public function testPostFilters() {
$basic = new Version_350();

$search_term = 'search_term';
$search_fields = [ 'post_title', 'post_content' ];

$test_filter = function() {
return 1234;
};

/**
* Test the `ep_post_match_phrase_boost` filter.
*/
add_filter( 'ep_post_match_phrase_boost', $test_filter );

$query = $basic->get_query( 'post', $search_term, $search_fields, [] );
$this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] );

remove_filter( 'ep_post_match_phrase_boost', $test_filter );
}

/**
* Test deprecated/legacy filters
*
Expand Down
59 changes: 59 additions & 0 deletions tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,65 @@ public function testFilters() {
remove_filter( 'ep_indexable_match_cross_fields_boost', $test_filter );
}

/**
* Test filters with posts
*
* As posts also apply the legacy filters, these tests assure code honors the value of the newer filters
*
* @see https://github.com/10up/ElasticPress/issues/3033
* @group searchAlgorithms
*/
public function testPostFilters() {
$basic = new Version_400();

$search_term = 'search_term';
$search_fields = [ 'post_title', 'post_content' ];

$test_filter = function() {
return 1234;
};

/**
* Test the `ep_post_match_phrase_boost` filter.
*/
add_filter( 'ep_post_match_phrase_boost', $test_filter );

$query = $basic->get_query( 'post', $search_term, $search_fields, [] );
$this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] );

remove_filter( 'ep_post_match_phrase_boost', $test_filter );

/**
* Test the `ep_post_match_boost` filter.
*/
add_filter( 'ep_post_match_boost', $test_filter );

$query = $basic->get_query( 'post', $search_term, $search_fields, [] );
$this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['boost'] );

remove_filter( 'ep_post_match_boost', $test_filter );

/**
* Test the `ep_post_match_fuzziness` filter.
*/
add_filter( 'ep_post_match_fuzziness', $test_filter );

$query = $basic->get_query( 'post', $search_term, $search_fields, [] );
$this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['fuzziness'] );

remove_filter( 'ep_post_match_fuzziness', $test_filter );

/**
* Test the `ep_post_match_cross_fields_boost` filter.
*/
add_filter( 'ep_post_match_cross_fields_boost', $test_filter );

$query = $basic->get_query( 'post', $search_term, $search_fields, [] );
$this->assertEquals( 1234, $query['bool']['should'][2]['multi_match']['boost'] );

remove_filter( 'ep_post_match_cross_fields_boost', $test_filter );
}

/**
* Test deprecated/legacy filters
*
Expand Down

0 comments on commit 0daae8e

Please sign in to comment.