Skip to content

Commit

Permalink
Change the stop filter depending on the language
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Jul 7, 2023
1 parent 8ed79a2 commit 239b4ea
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
31 changes: 23 additions & 8 deletions includes/classes/Indexable/Post/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public function setup() {
add_action( 'deleted_term_relationships', array( $this, 'action_deleted_term_relationships' ), 10, 3 );

// Clear field limit cache
add_action( 'ep_update_index_settings', [ $this, 'clear_total_fields_limit_cache' ] );
add_action( 'ep_sync_put_mapping', [ $this, 'clear_total_fields_limit_cache' ] );
add_action( 'ep_saved_weighting_configuration', [ $this, 'clear_total_fields_limit_cache' ] );
add_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] );
add_action( 'ep_sync_put_mapping', [ $this, 'clear_index_settings_cache' ] );
add_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] );

// Clear distinct meta field per post type cache
add_action( 'wp_insert_post', [ $this, 'clear_meta_keys_db_per_post_type_cache_by_post_id' ] );
Expand Down Expand Up @@ -743,13 +743,28 @@ public function action_create_blog_index( $blog ) {
* @since 4.4.0
*/
public function clear_total_fields_limit_cache() {
_deprecated_function( __METHOD__, '4.5.0', '\ElasticPress\Indexable\Post\SyncManager::clear_index_settings_cache()' );

$this->clear_index_settings_cache();
}

/**
* Clear the cache of specific index settings
*
* @since 4.7.0
*/
public function clear_index_settings_cache() {
$indexable = Indexables::factory()->get( $this->indexable_slug );
$cache_key = 'ep_total_fields_limit_' . $indexable->get_index_name();

if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
delete_site_transient( $cache_key );
} else {
delete_transient( $cache_key );
$settings = [ 'total_fields_limit', 'default_analyzer_language', 'snowball_filter_language' ];
foreach ( $settings as $setting ) {
$cache_key = 'ep_' . $setting . '_' . $indexable->get_index_name();

if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
delete_site_transient( $cache_key );
} else {
delete_transient( $cache_key );
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,13 +746,13 @@ function use_language_in_setting( $language = 'english', $context = '' ) {
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
$translations = wp_get_available_translations();

// Bail early if not in the array of available translations.
if ( empty( $translations[ $ep_language ]['english_name'] ) ) {
return $language;
// Default to en_US if not in the array of available translations.
if ( ! empty( $translations[ $ep_language ]['english_name'] ) ) {
$wp_language = $translations[ $ep_language ]['language'];
} else {
$wp_language = 'en_US';
}

$wp_language = $translations[ $ep_language ]['language'];

/**
* Languages supported in Elasticsearch mappings.
* Array format: Elasticsearch analyzer name => WordPress language package name
Expand Down Expand Up @@ -848,6 +848,10 @@ function use_language_in_setting( $language = 'english', $context = '' ) {
return $es_snowball_similar[ $uc_first_language ] ?? 'English';
}

if ( 'filter_ep_stop' === $context ) {
return "_{$language}_";
}

return $language;
}

Expand Down
17 changes: 8 additions & 9 deletions includes/mappings/post/7-0.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
* @param {array<string>} $filters Default filters
* @return {array<string>} New filters
*/
'filter' => apply_filters( 'ep_default_analyzer_filters', array( 'ewp_word_delimiter', 'lowercase', 'stop', 'ewp_snowball' ) ),
'filter' => apply_filters( 'ep_default_analyzer_filters', array( 'ewp_word_delimiter', 'lowercase', 'ep_stop', 'ewp_snowball' ) ),
/**
* Filter Elasticsearch default analyzer's char_filter
*
Expand Down Expand Up @@ -115,14 +115,7 @@
),
'ewp_snowball' => array(
'type' => 'snowball',
/**
* Filter Elasticsearch default language in mapping
*
* @hook ep_analyzer_language
* @param {string} $lang Default language
* @param {string} $lang_context Language context
* @return {string} New language
*/
/* This filter is documented above */
'language' => apply_filters( 'ep_analyzer_language', 'english', 'filter_ewp_snowball' ),
),
'edge_ngram' => array(
Expand All @@ -131,6 +124,12 @@
'min_gram' => 3,
'type' => 'edge_ngram',
),
'ep_stop' => array(
'type' => 'stop',
'ignore_case' => true,
/* This filter is documented above */
'stopwords' => apply_filters( 'ep_analyzer_language', 'english', 'filter_ep_stop' ),
),
),
'normalizer' => array(
'lowerasciinormalizer' => array(
Expand Down

0 comments on commit 239b4ea

Please sign in to comment.