materio_sapi: OR query in terms mode, Solr syntax neutralized (H3)

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
This commit is contained in:
2026-11-17 12:10:00 +01:00
co-authored by Claude Opus 5.5
parent ae69b5c03b
commit 58ce1b0513
2 changed files with 14 additions and 3 deletions
@@ -172,7 +172,11 @@ class Base extends ControllerBase {
* [nid, uuid] keyed by nid, in result order. * [nid, uuid] keyed by nid, in result order.
*/ */
private function orQuery(string $keys, string $lang): array { 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)) { if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $keys, $matches)) {
$ref_conditions = $this->or_query->createConditionGroup('OR'); $ref_conditions = $this->or_query->createConditionGroup('OR');
@@ -45,10 +45,17 @@ final class KeywordText {
public static function words(string $phrase, string $langcode): array { public static function words(string $phrase, string $langcode): array {
$skip = self::stopWords($langcode); $skip = self::stopWords($langcode);
$words = []; $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) { 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; $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; continue;
} }
$words[] = $word; $words[] = $word;