Skip to content

Commit a833c88

Browse files
authored
fix: compatibility with stopwords on ElasticPress 4.7.0 (#33)
Fixes #32 - ElasticPress 4.7.0 changed the stopword filter slug. - From stop to ep_stop. - See: 10up/ElasticPress#3549 Co-authored-by: Juan de Paco <juan.d@onthegosystems.com>
1 parent 27846b1 commit a833c88

1 file changed

Lines changed: 37 additions & 18 deletions

File tree

src/Manager/Indices.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class Indices {
1212

1313
use TranslateLanguages;
1414

15+
const STOPWORD_FILTER_SLUG = 'ep_stop';
16+
// Before ElasticPress 4.7.0
17+
const LEGACY_STOPWORD_FILTER_SLUG = 'stop';
18+
1519
/** @var Elasticsearch */
1620
private $elasticsearch;
1721

@@ -27,6 +31,9 @@ class Indices {
2731
/** @var string */
2832
private $currentIndexLanguage = '';
2933

34+
/** @var string|null */
35+
private $stopwordFilterSlug = null;
36+
3037
/**
3138
* @param Elasticsearch $elasticsearch
3239
* @param Indexables $indexables
@@ -92,6 +99,24 @@ public function indexExists( $indexName ) {
9299
return $this->elasticsearch->index_exists( $indexName );
93100
}
94101

102+
/**
103+
* @param array $filtersList
104+
*
105+
* @return string|null
106+
*/
107+
private function getStopwordFilterKey( $filtersList ) {
108+
if ( null !== $this->stopwordFilterSlug ) {
109+
return $this->stopwordFilterSlug;
110+
}
111+
if ( in_array( self::LEGACY_STOPWORD_FILTER_SLUG, $filtersList, true ) ) {
112+
$this->stopwordFilterSlug = self::LEGACY_STOPWORD_FILTER_SLUG;
113+
}
114+
if ( in_array( self::STOPWORD_FILTER_SLUG, $filtersList, true ) ) {
115+
$this->stopwordFilterSlug = self::STOPWORD_FILTER_SLUG;
116+
}
117+
return $this->stopwordFilterSlug;
118+
}
119+
95120
/**
96121
* @param Indexable $indexable
97122
*/
@@ -101,7 +126,7 @@ public function generateIndexByIndexable( $indexable ) {
101126
return;
102127
}
103128
$mapping = $indexable->generate_mapping();
104-
if ( 'en' === $this->currentIndexLanguage ) {
129+
if ( $this->defaultLanguage === $this->currentIndexLanguage ) {
105130
$this->elasticsearch->put_mapping( $indexName, $mapping );
106131
return;
107132
}
@@ -112,24 +137,18 @@ public function generateIndexByIndexable( $indexable ) {
112137
$mapping['settings']['analysis']['analyzer']['default']['language'] = $languages['analyzer'];
113138
$mapping['settings']['analysis']['filter']['ewp_snowball']['language'] = $languages['snowball'];
114139

115-
// Set language stopwords and stemmer filters
116-
$mapping['settings']['analysis']['analyzer']['default']['filter'] = array_map(
117-
function( $filter ) use ( $currentIndexLanguage ) {
118-
if ( 'stop' === $filter ) {
119-
return 'stop_' . $currentIndexLanguage;
120-
}
121-
return $filter;
122-
},
123-
$mapping['settings']['analysis']['analyzer']['default']['filter']
124-
);
125-
$mapping['settings']['analysis']['analyzer']['default']['filter'][] = 'stemmer_' . $this->currentIndexLanguage;
140+
// Define language stopwords
141+
$stopwordFilterKey = $this->getStopwordFilterKey( $mapping['settings']['analysis']['analyzer']['default']['filter'] );
142+
if ( null !== $stopwordFilterKey ) {
143+
$mapping['settings']['analysis']['filter'][ $stopwordFilterKey ] = [
144+
'type' => 'stop',
145+
'ignore_case' => true,
146+
'stopwords' => '_' . $languages['analyzer'] . '_',
147+
];
148+
}
126149

127-
// Define language stopwords and stemmer filters
128-
$mapping['settings']['analysis']['filter']['stop_' . $this->currentIndexLanguage] = [
129-
'type' => 'stop',
130-
'ignore_case' => true,
131-
'stopwords' => '_' . $languages['analyzer'] . '_',
132-
];
150+
// Define language stemmer
151+
$mapping['settings']['analysis']['analyzer']['default']['filter'][] = 'stemmer_' . $this->currentIndexLanguage;
133152
$mapping['settings']['analysis']['filter']['stemmer_' . $this->currentIndexLanguage] = [
134153
'type' => 'stemmer',
135154
'language' => $languages['analyzer'],

0 commit comments

Comments
 (0)