diff --git a/includes/classes/Feature/Search/Synonyms.php b/includes/classes/Feature/Search/Synonyms.php index 6f7745411..09fc5adf9 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( - [ $filter_name ], - $mapping['settings']['analysis']['analyzer']['default_search']['filter'] + $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( - [ $this->get_synonym_filter_name() ], - $filters + $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. + * + * @since 5.1.0 + * @param array $filters Array of filters. + * @return array + */ + protected function maybe_change_filter_position( array $filters ) : array { + $lowercase_filter = array_search( 'lowercase', $filters, true ); + + if ( false !== $lowercase_filter ) { + unset( $filters[ $lowercase_filter ] ); + array_unshift( $filters, 'lowercase' ); + } + + return $filters; + } + } diff --git a/tests/php/features/TestSynonyms.php b/tests/php/features/TestSynonyms.php index b8a705065..44729acae 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 test_synonyms_case_insensitive() { + $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'] ); + } }