From f897b5429e5369a744c1dfc9249525cef6c73d10 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 22 Mar 2024 11:07:02 +0500 Subject: [PATCH 1/6] Apply ngram_fields on search query when ajax and weighting enable --- .../Feature/Autosuggest/Autosuggest.php | 2 +- tests/php/features/TestAutosuggest.php | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/includes/classes/Feature/Autosuggest/Autosuggest.php b/includes/classes/Feature/Autosuggest/Autosuggest.php index 1b9eea9b96..d4aa158704 100644 --- a/includes/classes/Feature/Autosuggest/Autosuggest.php +++ b/includes/classes/Feature/Autosuggest/Autosuggest.php @@ -195,7 +195,7 @@ public function set_fuzziness( $fuzziness, $search_fields, $args ) { * @return array $query adjusted ES Query arguments */ public function adjust_fuzzy_fields( $query, $post_type, $args ) { - if ( ! Utils\is_integrated_request( $this->slug, [ 'public' ] ) || empty( $args['s'] ) ) { + if ( ! Utils\is_integrated_request( $this->slug, [ 'public', 'ajax' ] ) || empty( $args['s'] ) ) { return $query; } diff --git a/tests/php/features/TestAutosuggest.php b/tests/php/features/TestAutosuggest.php index 796c821a22..20940f95b1 100644 --- a/tests/php/features/TestAutosuggest.php +++ b/tests/php/features/TestAutosuggest.php @@ -321,4 +321,50 @@ public function test_get_settings_schema() { $settings_keys ); } + + /** + * Test whether autosuggest ngram fields apply to the search query when AJAX integration and weighting is enabled. + * + * @since 5.1.o + * @group autosuggest + */ + public function test_autosuggest_ngram_fields_for_ajax_call() { + define( 'DOING_AJAX', true ); + + $this->get_feature()->setup(); + + add_filter( 'ep_ajax_wp_query_integration', '__return_true' ); + add_filter( 'ep_enable_do_weighting', '__return_true' ); + + $this->ep_factory->post->create( + array( + 'post_title' => 'search me', + 'post_type' => 'post', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + add_filter( + 'ep_query_request_path', + function( $path, $index, $type, $query, $query_args, $query_object ) { + $fields = $query['query']['function_score']['query']['bool']['should'][0]['bool']['must'][0]['bool']['should'][1]['multi_match']['fields']; + + $this->assertContains( 'term_suggest^1', $fields ); + $this->assertContains( 'post_title.suggest^1', $fields ); + return $path; + }, + 10, + 6 + ); + + $query = new \WP_Query( + array( + 's' => 'search me', + 'ep_integrate' => true, + ) + ); + + $this->assertTrue( $query->elasticsearch_success ); + } } From 739d73aaff4f6eaf94e03d57abad402bc7e0da1a Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 22 Mar 2024 11:25:36 +0500 Subject: [PATCH 2/6] Fix tests --- includes/utils.php | 2 +- tests/php/features/TestAutosuggest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/utils.php b/includes/utils.php index 1d6d5aa535..be0f5bc7d7 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -675,7 +675,7 @@ function is_integrated_request( $context, $types = [] ) { } $is_admin_request = is_admin(); - $is_ajax_request = defined( 'DOING_AJAX' ) && DOING_AJAX; + $is_ajax_request = wp_doing_ajax(); $is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST; $is_integrated_admin_request = false; $is_integrated_ajax_request = false; diff --git a/tests/php/features/TestAutosuggest.php b/tests/php/features/TestAutosuggest.php index 20940f95b1..1a862ae415 100644 --- a/tests/php/features/TestAutosuggest.php +++ b/tests/php/features/TestAutosuggest.php @@ -325,11 +325,11 @@ public function test_get_settings_schema() { /** * Test whether autosuggest ngram fields apply to the search query when AJAX integration and weighting is enabled. * - * @since 5.1.o + * @since 5.1.0 * @group autosuggest */ public function test_autosuggest_ngram_fields_for_ajax_call() { - define( 'DOING_AJAX', true ); + add_filter( 'wp_doing_ajax', '__return_true' ); $this->get_feature()->setup(); From 4a432f5166bfdebc41aed453d873bc0b4eee5ef6 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 22 Mar 2024 11:34:56 +0500 Subject: [PATCH 3/6] Minor change --- tests/php/features/TestAutosuggest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/php/features/TestAutosuggest.php b/tests/php/features/TestAutosuggest.php index 1a862ae415..5b980f934d 100644 --- a/tests/php/features/TestAutosuggest.php +++ b/tests/php/features/TestAutosuggest.php @@ -366,5 +366,6 @@ function( $path, $index, $type, $query, $query_args, $query_object ) { ); $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->found_posts ); } } From cb384dd86d24160c60deee78f9ef68982ccdab66 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Tue, 26 Mar 2024 13:56:04 +0500 Subject: [PATCH 4/6] Add get_contexts_method --- .../Feature/Autosuggest/Autosuggest.php | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/includes/classes/Feature/Autosuggest/Autosuggest.php b/includes/classes/Feature/Autosuggest/Autosuggest.php index d4aa158704..cc5cb222fb 100644 --- a/includes/classes/Feature/Autosuggest/Autosuggest.php +++ b/includes/classes/Feature/Autosuggest/Autosuggest.php @@ -180,7 +180,7 @@ public function mapping( $mapping ) { * @return array */ public function set_fuzziness( $fuzziness, $search_fields, $args ) { - if ( Utils\is_integrated_request( $this->slug, [ 'public' ] ) && ! empty( $args['s'] ) ) { + if ( Utils\is_integrated_request( $this->slug, $this->get_contexts() ) && ! empty( $args['s'] ) ) { return 'auto'; } return $fuzziness; @@ -195,7 +195,7 @@ public function set_fuzziness( $fuzziness, $search_fields, $args ) { * @return array $query adjusted ES Query arguments */ public function adjust_fuzzy_fields( $query, $post_type, $args ) { - if ( ! Utils\is_integrated_request( $this->slug, [ 'public', 'ajax' ] ) || empty( $args['s'] ) ) { + if ( ! Utils\is_integrated_request( $this->slug, $this->get_contexts() ) || empty( $args['s'] ) ) { return $query; } @@ -926,4 +926,22 @@ public function delete_cached_query() { 'ElasticPress 4.7.0' ); } + + /** + * Get the contexts where autosuggest is used. + * + * @since 5.1.0 + * @return array + */ + protected function get_contexts() : array { + /** + * Filter contexts where autosuggest is used. + * + * @hook ep_autosuggest_contexts + * @since 5.1.0 + * @param {array} $contexts Contexts where autosuggest is used + * @return {array} New contexts + */ + return apply_filters( 'ep_autosuggest_contexts', [ 'public', 'ajax' ] ); + } } From 8686e8692664e0273f1c8b694477fb034aedbd88 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Tue, 26 Mar 2024 14:54:35 +0500 Subject: [PATCH 5/6] Add tests --- tests/php/features/TestAutosuggest.php | 155 ++++++++++++++++++++++++- 1 file changed, 152 insertions(+), 3 deletions(-) diff --git a/tests/php/features/TestAutosuggest.php b/tests/php/features/TestAutosuggest.php index 5b980f934d..d4bfa8b563 100644 --- a/tests/php/features/TestAutosuggest.php +++ b/tests/php/features/TestAutosuggest.php @@ -328,14 +328,65 @@ public function test_get_settings_schema() { * @since 5.1.0 * @group autosuggest */ - public function test_autosuggest_ngram_fields_for_ajax_call() { + public function test_autosuggest_ngram_fields_for_ajax_request() { add_filter( 'wp_doing_ajax', '__return_true' ); + add_filter( 'ep_ajax_wp_query_integration', '__return_true' ); + add_filter( 'ep_enable_do_weighting', '__return_true' ); $this->get_feature()->setup(); + $this->ep_factory->post->create( + array( + 'post_title' => 'search me', + 'post_type' => 'post', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + add_filter( + 'ep_query_request_path', + function( $path, $index, $type, $query, $query_args, $query_object ) { + $fields = $query['query']['function_score']['query']['bool']['should'][0]['bool']['must'][0]['bool']['should'][1]['multi_match']['fields']; + + $this->assertContains( 'term_suggest^1', $fields ); + $this->assertContains( 'post_title.suggest^1', $fields ); + return $path; + }, + 10, + 6 + ); + + $query = new \WP_Query( + array( + 's' => 'search me', + 'ep_integrate' => true, + ) + ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->found_posts ); + } + + /** + * Test whether autosuggest ngram fields do not apply to the search query when `ep_autosuggest_contexts` is only set to public. + * + * @since 5.1.0 + * @group autosuggest + */ + public function test_autosuggest_ngram_fields_for_ajax_request_negative() { + add_filter( 'wp_doing_ajax', '__return_true' ); add_filter( 'ep_ajax_wp_query_integration', '__return_true' ); add_filter( 'ep_enable_do_weighting', '__return_true' ); + $autosuggest_context = function() { + return [ 'public' ]; + }; + + add_filter( 'ep_autosuggest_contexts', $autosuggest_context ); + + $this->get_feature()->setup(); + $this->ep_factory->post->create( array( 'post_title' => 'search me', @@ -350,8 +401,8 @@ public function test_autosuggest_ngram_fields_for_ajax_call() { function( $path, $index, $type, $query, $query_args, $query_object ) { $fields = $query['query']['function_score']['query']['bool']['should'][0]['bool']['must'][0]['bool']['should'][1]['multi_match']['fields']; - $this->assertContains( 'term_suggest^1', $fields ); - $this->assertContains( 'post_title.suggest^1', $fields ); + $this->assertNotContains( 'term_suggest^1', $fields ); + $this->assertNotContains( 'post_title.suggest^1', $fields ); return $path; }, 10, @@ -368,4 +419,102 @@ function( $path, $index, $type, $query, $query_args, $query_object ) { $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 1, $query->found_posts ); } + + /** + * Test whether fuzziness is set to `auto` for AJAX calls. + * + * @since 5.1.0 + * @group autosuggest + */ + public function test_fuziness_with_type_auto_set_for_ajax_call() { + add_filter( 'wp_doing_ajax', '__return_true' ); + add_filter( 'ep_ajax_wp_query_integration', '__return_true' ); + + $algorithm = function() { + return 'default'; + }; + add_filter( 'ep_search_algorithm_version', $algorithm ); + + $this->get_feature()->setup(); + + $this->ep_factory->post->create( + array( + 'post_title' => 'search me', + 'post_type' => 'post', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + add_filter( + 'ep_query_request_path', + function( $path, $index, $type, $query, $query_args, $query_object ) { + $this->assertEquals( 'auto', $query['query']['function_score']['query']['bool']['should'][2]['multi_match']['fuzziness'] ); + return $path; + }, + 10, + 6 + ); + + $query = new \WP_Query( + array( + 's' => 'search me', + 'ep_integrate' => true, + ) + ); + + $this->assertTrue( $query->elasticsearch_success ); + } + + /** + * Test whether fuzziness is not set to `auto` for AJAX calls when `ep_autosuggest_contexts` is only set to public. + * + * @since 5.1.0 + * @group autosuggest + */ + public function test_fuziness_with_type_auto_set_for_ajax_call_negative() { + add_filter( 'wp_doing_ajax', '__return_true' ); + add_filter( 'ep_ajax_wp_query_integration', '__return_true' ); + + $algorithm = function() { + return 'default'; + }; + add_filter( 'ep_search_algorithm_version', $algorithm ); + + $autosuggest_context = function() { + return [ 'public' ]; + }; + add_filter( 'ep_autosuggest_contexts', $autosuggest_context ); + + $this->get_feature()->setup(); + + $this->ep_factory->post->create( + array( + 'post_title' => 'search me', + 'post_type' => 'post', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + add_filter( + 'ep_query_request_path', + function( $path, $index, $type, $query, $query_args, $query_object ) { + $this->assertNotEquals( 'auto', $query['query']['function_score']['query']['bool']['should'][2]['multi_match']['fuzziness'] ); + return $path; + }, + 10, + 6 + ); + + $query = new \WP_Query( + array( + 's' => 'search me', + 'ep_integrate' => true, + ) + ); + + $this->assertTrue( $query->elasticsearch_success ); + + } } From cb25a9d60ed8bea4acc4f9b722b7632c082bdf89 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Tue, 26 Mar 2024 15:02:57 +0500 Subject: [PATCH 6/6] minor chagne --- includes/classes/Feature/Autosuggest/Autosuggest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/classes/Feature/Autosuggest/Autosuggest.php b/includes/classes/Feature/Autosuggest/Autosuggest.php index cc5cb222fb..d77f4cdbc1 100644 --- a/includes/classes/Feature/Autosuggest/Autosuggest.php +++ b/includes/classes/Feature/Autosuggest/Autosuggest.php @@ -928,18 +928,18 @@ public function delete_cached_query() { } /** - * Get the contexts where autosuggest is used. + * Get the contexts for autosuggest. * * @since 5.1.0 * @return array */ protected function get_contexts() : array { /** - * Filter contexts where autosuggest is used. + * Filter contexts for autosuggest. * * @hook ep_autosuggest_contexts * @since 5.1.0 - * @param {array} $contexts Contexts where autosuggest is used + * @param {array} $contexts Contexts for autosuggest * @return {array} New contexts */ return apply_filters( 'ep_autosuggest_contexts', [ 'public', 'ajax' ] );