From 6f9a1432f9d3f51010b40d35aac01863f21d782d Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Thu, 7 Mar 2024 19:55:44 +0500 Subject: [PATCH 1/6] Fix Synonyms case sensitive issue --- includes/classes/Feature/Search/Synonyms.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/classes/Feature/Search/Synonyms.php b/includes/classes/Feature/Search/Synonyms.php index de26fec2f..6149caec1 100644 --- a/includes/classes/Feature/Search/Synonyms.php +++ b/includes/classes/Feature/Search/Synonyms.php @@ -390,8 +390,8 @@ public function add_search_synonyms( $mapping, $index ) { // Tell the analyzer to use our newly created filter. $mapping['settings']['analysis']['analyzer']['default_search']['filter'] = array_values( array_merge( - [ $filter_name ], - $mapping['settings']['analysis']['analyzer']['default_search']['filter'] + $mapping['settings']['analysis']['analyzer']['default_search']['filter'], + [ $filter_name ] ) ); @@ -486,8 +486,8 @@ function( $success, $index ) { $setting['index']['analysis']['analyzer']['default_search']['filter'] = array_values( array_unique( array_merge( - [ $this->get_synonym_filter_name() ], - $filters + $filters, + [ $this->get_synonym_filter_name() ] ) ) ); From 4434fbec2b696fff7749dce8aee4a4db768cb28a Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 8 Mar 2024 11:50:40 +0500 Subject: [PATCH 2/6] Add test --- tests/php/features/TestSynonyms.php | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/php/features/TestSynonyms.php b/tests/php/features/TestSynonyms.php index b8a705065..6ae812e1c 100644 --- a/tests/php/features/TestSynonyms.php +++ b/tests/php/features/TestSynonyms.php @@ -160,4 +160,46 @@ public function test_synonyms_with_spaces() { $this->assertTrue( $query->elasticsearch_success ); $this->assertSame( $post_id, $query->posts['0'] ); } + + /** + * Tests synonyms are case insensitive + * + * @since 5.1.0 + * @group synonyms + */ + public function testSynonymsCaseInsensitive() { + $instance = $this->getFeature(); + + $this->ep_factory->post->create( + [ + 'ID' => $instance->get_synonym_post_id(), + 'post_content' => 'hoodie, sweatshirt', + 'post_type' => $instance::POST_TYPE_NAME, + ] + ); + + $instance->update_synonyms(); + + $post_id = $this->ep_factory->post->create( [ 'post_content' => 'sweatshirt' ] ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $query = new \WP_Query( + [ + 's' => 'HoOdiE', + 'fields' => 'ids', + ] + ); + $this->assertTrue( $query->elasticsearch_success ); + $this->assertSame( $post_id, $query->posts['0'] ); + + $query = new \WP_Query( + [ + 's' => 'hoodie', + 'fields' => 'ids', + ] + ); + $this->assertTrue( $query->elasticsearch_success ); + $this->assertSame( $post_id, $query->posts['0'] ); + } } From de336de17d616540e216503479a79506c2bf2456 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 8 Mar 2024 11:55:30 +0500 Subject: [PATCH 3/6] Update test --- tests/php/features/TestSynonyms.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/features/TestSynonyms.php b/tests/php/features/TestSynonyms.php index 6ae812e1c..82886cd95 100644 --- a/tests/php/features/TestSynonyms.php +++ b/tests/php/features/TestSynonyms.php @@ -195,7 +195,7 @@ public function testSynonymsCaseInsensitive() { $query = new \WP_Query( [ - 's' => 'hoodie', + 's' => 'HOODIE', 'fields' => 'ids', ] ); From 14401acb9fdad8f6f3cda2da81b215f4037e25cc Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 8 Mar 2024 15:19:21 +0500 Subject: [PATCH 4/6] Update test --- includes/classes/Feature/Search/Synonyms.php | 41 +++++++++++++++----- tests/php/features/TestSynonyms.php | 2 +- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/includes/classes/Feature/Search/Synonyms.php b/includes/classes/Feature/Search/Synonyms.php index 6149caec1..b7c069b2e 100644 --- a/includes/classes/Feature/Search/Synonyms.php +++ b/includes/classes/Feature/Search/Synonyms.php @@ -388,10 +388,12 @@ public function add_search_synonyms( $mapping, $index ) { $mapping['settings']['analysis']['filter'][ $filter_name ] = $this->get_synonym_filter(); // Tell the analyzer to use our newly created filter. - $mapping['settings']['analysis']['analyzer']['default_search']['filter'] = array_values( - array_merge( - $mapping['settings']['analysis']['analyzer']['default_search']['filter'], - [ $filter_name ] + $mapping['settings']['analysis']['analyzer']['default_search']['filter'] = $this->maybe_change_filter_position( + array_values( + array_merge( + [ $filter_name ], + $mapping['settings']['analysis']['analyzer']['default_search']['filter'], + ) ) ); @@ -483,11 +485,13 @@ function( $success, $index ) { $setting['index']['analysis']['filter']['ep_synonyms_filter'] = $filter; // Add the analyzer. - $setting['index']['analysis']['analyzer']['default_search']['filter'] = array_values( - array_unique( - array_merge( - $filters, - [ $this->get_synonym_filter_name() ] + $setting['index']['analysis']['analyzer']['default_search']['filter'] = $this->maybe_change_filter_position( + array_values( + array_unique( + array_merge( + [ $this->get_synonym_filter_name() ], + $filters + ) ) ) ); @@ -823,4 +827,23 @@ private function update_synonym_post( $content ) { true ); } + + /** + * Change the position of the lowercase filter to the beginning of the array. + * + * @param array $filters Array of filters. + * + * @return array + */ + private function maybe_change_filter_position( $filters ) { + $lowercase_index = array_search( 'lowercase', $filters, true ); + + if ( false !== $lowercase_index ) { + unset( $filters[ $lowercase_index ] ); + array_unshift( $filters, 'lowercase' ); + } + + return $filters; + } + } diff --git a/tests/php/features/TestSynonyms.php b/tests/php/features/TestSynonyms.php index 82886cd95..44729acae 100644 --- a/tests/php/features/TestSynonyms.php +++ b/tests/php/features/TestSynonyms.php @@ -167,7 +167,7 @@ public function test_synonyms_with_spaces() { * @since 5.1.0 * @group synonyms */ - public function testSynonymsCaseInsensitive() { + public function test_synonyms_case_insensitive() { $instance = $this->getFeature(); $this->ep_factory->post->create( From 9df26b71a2a8203590ee2aa9fb2cc22b1787a056 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Mon, 11 Mar 2024 15:19:38 +0500 Subject: [PATCH 5/6] Minor change --- includes/classes/Feature/Search/Synonyms.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/classes/Feature/Search/Synonyms.php b/includes/classes/Feature/Search/Synonyms.php index b7c069b2e..994932230 100644 --- a/includes/classes/Feature/Search/Synonyms.php +++ b/includes/classes/Feature/Search/Synonyms.php @@ -836,10 +836,10 @@ private function update_synonym_post( $content ) { * @return array */ private function maybe_change_filter_position( $filters ) { - $lowercase_index = array_search( 'lowercase', $filters, true ); + $lowercase_filter = array_search( 'lowercase', $filters, true ); - if ( false !== $lowercase_index ) { - unset( $filters[ $lowercase_index ] ); + if ( false !== $lowercase_filter ) { + unset( $filters[ $lowercase_filter ] ); array_unshift( $filters, 'lowercase' ); } From 025d6e908df6f76eb9e7f87cafc899ce8a7ececd Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 20 Mar 2024 09:13:14 -0300 Subject: [PATCH 6/6] Update includes/classes/Feature/Search/Synonyms.php --- includes/classes/Feature/Search/Synonyms.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/classes/Feature/Search/Synonyms.php b/includes/classes/Feature/Search/Synonyms.php index 994932230..1807f8c4d 100644 --- a/includes/classes/Feature/Search/Synonyms.php +++ b/includes/classes/Feature/Search/Synonyms.php @@ -831,11 +831,11 @@ private function update_synonym_post( $content ) { /** * Change the position of the lowercase filter to the beginning of the array. * + * @since 5.1.0 * @param array $filters Array of filters. - * * @return array */ - private function maybe_change_filter_position( $filters ) { + protected function maybe_change_filter_position( array $filters ) : array { $lowercase_filter = array_search( 'lowercase', $filters, true ); if ( false !== $lowercase_filter ) {