Skip to content

Commit

Permalink
Merge pull request #3857 from 10up/fix/3855
Browse files Browse the repository at this point in the history
Fix Synonyms case sensitive issue
  • Loading branch information
felipeelia committed Mar 20, 2024
2 parents b7d2afa + 9c247a7 commit 494aeb8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
41 changes: 32 additions & 9 deletions includes/classes/Feature/Search/Synonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
)
)
);

Expand Down Expand Up @@ -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
)
)
)
);
Expand Down Expand Up @@ -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;
}

}
42 changes: 42 additions & 0 deletions tests/php/features/TestSynonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
}
}

0 comments on commit 494aeb8

Please sign in to comment.