Skip to content

Commit 700d5ba

Browse files
pschoffernickdaughertyrinatkhaziev
authored
Limit Max Window Size (#2051)
* limit_max_window_size * Update search/includes/classes/class-search.php Co-authored-by: Rinat K <[email protected]> * adds more comments * Add index.max_result_window to the monitored / healed index settings list * Whitespace Co-authored-by: Nick Daugherty <[email protected]> Co-authored-by: Rinat K <[email protected]>
1 parent 8d4a0b8 commit 700d5ba

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

search/includes/classes/class-health.php

+2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ class Health {
2626
'time',
2727
);
2828
const INDEX_SETTINGS_HEALTH_MONITORED_KEYS = array(
29+
'index.max_result_window',
2930
'index.number_of_replicas',
3031
'index.number_of_shards',
3132
'index.routing.allocation.include.dc',
3233
);
3334
const INDEX_SETTINGS_HEALTH_AUTO_HEAL_KEYS = array(
35+
'index.max_result_window',
3436
'index.number_of_replicas',
3537
'index.routing.allocation.include.dc',
3638
);

search/includes/classes/class-search.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Search {
1111
public const QUERY_INTEGRATION_FORCE_ENABLE_KEY = 'vip-search-enabled';
1212
public const SEARCH_ALERT_SLACK_CHAT = '#vip-go-es-alerts';
1313
public const SEARCH_ALERT_LEVEL = 2; // Level 2 = 'alert'
14+
public const MAX_RESULT_WINDOW = 9000;
1415
/**
1516
* Empty for now. Will flesh out once migration path discussions are underway and/or the same meta are added to the filter across many
1617
* sites.
@@ -484,6 +485,14 @@ protected function setup_hooks() {
484485

485486
add_filter( 'vip_search_post_meta_allow_list', array( $this, 'filter__vip_search_post_meta_allow_list_defaults' ) );
486487

488+
// Limit the max result window on index settings
489+
add_filter( 'ep_max_result_window', [ $this, 'limit_max_result_window' ], PHP_INT_MAX );
490+
add_filter( 'ep_term_max_result_window', [ $this, 'limit_max_result_window' ], PHP_INT_MAX );
491+
add_filter( 'ep_user_max_result_window', [ $this, 'limit_max_result_window' ], PHP_INT_MAX );
492+
493+
// Limit the max result window on query arguments
494+
add_filter( 'ep_max_results_window', [ $this, 'limit_max_result_window' ], PHP_INT_MAX );
495+
487496
add_action( 'after_setup_theme', array( $this, 'apply_settings' ), PHP_INT_MAX ); // Try to apply Search settings after other actions in this hook.
488497
}
489498

@@ -1637,7 +1646,7 @@ public function filter__ep_prepare_meta_allowed_protected_keys( $keys, $post ) {
16371646

16381647
/**
16391648
* Hooks into the ep_$indexable_mapping hooks to add things like allocation rules
1640-
*
1649+
*
16411650
* Note that this hook receives the mapping and settings together
16421651
*/
16431652
public function filter__ep_indexable_mapping( $mapping ) {
@@ -1802,4 +1811,8 @@ public function is_protected_content_enabled() {
18021811

18031812
return $protected_content_feature->is_active();
18041813
}
1814+
1815+
public function limit_max_result_window( $current_value ) {
1816+
return min( $current_value, self::MAX_RESULT_WINDOW );
1817+
}
18051818
}

tests/search/includes/classes/test-class-health.php

+17
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,13 @@ public function get_index_settings_diff_for_indexable_data() {
672672
array(
673673
'index.number_of_shards' => 1,
674674
'index.number_of_replicas' => 2,
675+
'index.max_result_window' => 9000,
675676
),
676677
// Desired index settings from ElasticPress
677678
array(
678679
'index.number_of_shards' => 1,
679680
'index.number_of_replicas' => 2,
681+
'index.max_result_window' => 9000,
680682
),
681683
// Options
682684
array(),
@@ -729,12 +731,14 @@ public function get_index_settings_diff_for_indexable_data() {
729731
'index.number_of_shards' => 1,
730732
'index.number_of_replicas' => 2,
731733
'foo' => 'bar',
734+
'index.max_result_window' => '1000000',
732735
),
733736
// Desired index settings from ElasticPress
734737
array(
735738
'index.number_of_shards' => 1,
736739
'index.number_of_replicas' => 1,
737740
'foo' => 'baz',
741+
'index.max_result_window' => 9000,
738742
),
739743
// Options
740744
array(),
@@ -744,6 +748,10 @@ public function get_index_settings_diff_for_indexable_data() {
744748
'expected' => 1,
745749
'actual' => 2,
746750
),
751+
'index.max_result_window' => array(
752+
'expected' => 9000,
753+
'actual' => 1000000,
754+
),
747755
),
748756
),
749757
// Diff expected, mismatched settings with specific index version
@@ -815,6 +823,7 @@ public function heal_index_settings_for_indexable_data() {
815823
array(
816824
'index.number_of_shards' => 1,
817825
'index.number_of_replicas' => 2,
826+
'index.max_result_window' => 9000,
818827
),
819828
// Options
820829
array(),
@@ -825,6 +834,7 @@ public function heal_index_settings_for_indexable_data() {
825834
array(
826835
'index.number_of_shards' => 1,
827836
'index.number_of_replicas' => 1,
837+
'index.max_result_window' => 9000,
828838
'foo' => 'baz',
829839
),
830840
// Options
@@ -836,6 +846,7 @@ public function heal_index_settings_for_indexable_data() {
836846
array(
837847
'index.number_of_shards' => 1,
838848
'index.number_of_replicas' => 1,
849+
'index.max_result_window' => 9000,
839850
'foo' => 'baz',
840851
),
841852
// Options
@@ -1003,12 +1014,14 @@ public function get_index_settings_diff_data() {
10031014
array(
10041015
'number_of_shards' => 1,
10051016
'number_of_replicas' => 2,
1017+
'max_result_window' => '1000000',
10061018
'foo' => 'bar',
10071019
),
10081020
// Desired index settings from ElasticPress
10091021
array(
10101022
'number_of_shards' => 1,
10111023
'number_of_replicas' => 1,
1024+
'max_result_window' => 9000,
10121025
'foo' => 'baz',
10131026
),
10141027
// Expected diff
@@ -1017,6 +1030,10 @@ public function get_index_settings_diff_data() {
10171030
'expected' => 1,
10181031
'actual' => 2,
10191032
),
1033+
'max_result_window' => array(
1034+
'expected' => 9000,
1035+
'actual' => '1000000',
1036+
),
10201037
'foo' => array(
10211038
'expected' => 'baz',
10221039
'actual' => 'bar',

tests/search/test-class-search.php

+23
Original file line numberDiff line numberDiff line change
@@ -2986,6 +2986,29 @@ public function test__maybe_enable_ep_query_logging_debug_bar_and_qm_enabled() {
29862986
$this->assertTrue( defined( 'WP_EP_DEBUG' ) );
29872987
$this->assertTrue( WP_EP_DEBUG );
29882988
}
2989+
public function limit_max_result_window_data() {
2990+
return [
2991+
[
2992+
'input' => 500,
2993+
'expected' => 500,
2994+
],
2995+
[
2996+
'input' => 10000,
2997+
'expected' => 9000,
2998+
],
2999+
];
3000+
}
3001+
3002+
/**
3003+
* @dataProvider limit_max_result_window_data
3004+
*/
3005+
public function test__limit_max_result_window( $input, $expected ) {
3006+
$es = new \Automattic\VIP\Search\Search();
3007+
3008+
$result = $es->limit_max_result_window( $input );
3009+
3010+
$this->assertEquals( $expected, $result );
3011+
}
29893012

29903013
/**
29913014
* Helper function for accessing protected methods.

0 commit comments

Comments
 (0)