From 58ce1b05139a9a12d1816fdb33b9389b6a95c3e1 Mon Sep 17 00:00:00 2001 From: bach Date: Tue, 17 Nov 2026 12:10:00 +0100 Subject: [PATCH] materio_sapi: OR query in terms mode, Solr syntax neutralized (H3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The OR query no longer sends the input raw (direct mode): « fibre de verre \ » broke Solr, « -bois » returned every other node as exact, « verre~ » ran a fuzzy search. KeywordText turns syntax between words into a separator, drops leading + - ! and symbol-only words (« || » broke the payload boost). Hyphens are split for the OR query only, as the direct mode did. Same counts and exact matches on S4, about twice as fast. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM --- .../custom/materio_sapi/src/Controller/Base.php | 6 +++++- web/modules/custom/materio_sapi/src/KeywordText.php | 11 +++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) 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;