Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Synonyms case sensitive issue #3857

Merged
merged 8 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions includes/classes/Feature/Search/Synonyms.php
Original file line number Diff line number Diff line change
@@ -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;
}

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