diff --git a/web/modules/custom/materio_sapi/src/Controller/Base.php b/web/modules/custom/materio_sapi/src/Controller/Base.php index 2cd210fd..c1b9d5a5 100644 --- a/web/modules/custom/materio_sapi/src/Controller/Base.php +++ b/web/modules/custom/materio_sapi/src/Controller/Base.php @@ -172,7 +172,11 @@ class Base extends ControllerBase { * [nid, uuid] keyed by nid, in result order. */ private function orQuery(string $keys, string $lang): array { - $this->or_query = $this->keywordQuery('direct', 'OR', $keys, $lang, 'materio_sapi_search_or_query'); + // Terms mode, not direct: Search API escapes the words, so punctuation + // and Solr syntax typed in a phrase (: ( " / \\ ~) are plain text (H3). + // Hyphenated words as separate words, as the direct mode did: in terms mode + // « bio-based » would be a quoted phrase (the phrase query keeps it whole). + $this->or_query = $this->keywordQuery('terms', 'OR', preg_replace('/(?<=\p{L})-(?=\p{L})/u', ' ', $keys), $lang, 'materio_sapi_search_or_query'); if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $keys, $matches)) { $ref_conditions = $this->or_query->createConditionGroup('OR'); diff --git a/web/modules/custom/materio_sapi/src/KeywordText.php b/web/modules/custom/materio_sapi/src/KeywordText.php index 992f910c..e4be8d64 100644 --- a/web/modules/custom/materio_sapi/src/KeywordText.php +++ b/web/modules/custom/materio_sapi/src/KeywordText.php @@ -45,10 +45,17 @@ final class KeywordText { public static function words(string $phrase, string $langcode): array { $skip = self::stopWords($langcode); $words = []; + // Solr and Search API syntax between words is a separator (« bois:verre », + // « bois/métal »); hyphens, apostrophes, and . , in numbers stay. + $phrase = preg_replace('#[:;/\\\\^&|()\[\]{}<>"«»~*?!=+\#@$]+#u', ' ', $phrase) ?? $phrase; foreach (preg_split('/\s+/u', mb_strtolower($phrase), -1, PREG_SPLIT_NO_EMPTY) as $token) { - $token = trim($token, ",;:!?.()[]{}\"«»'’"); + // Edge punctuation, and leading + - ! that Search API reads as operators + // (« -bois » excluded wood and returned every other node, H3). + $token = trim($token, ",;:!?.()[]{}\"«»'’<>"); + $token = ltrim($token, '+-!'); $word = preg_replace(self::ELISION, '', $token) ?? $token; - if ($word === '' || isset($skip[$word]) || isset($skip[$token])) { + // No letter or digit (« || », « & »): breaks the payload boost of Solr. + if (!preg_match('/[\p{L}\p{N}]/u', $word) || isset($skip[$word]) || isset($skip[$token])) { continue; } $words[] = $word;