From 8229a80c5722fcccabfdca2708d83269daae304e Mon Sep 17 00:00:00 2001 From: bach Date: Tue, 17 Nov 2026 12:17:09 +0100 Subject: [PATCH] materio_sapi: references as exact matches, any case (H4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit referenceQuery() runs first: nodes whose field_reference is one of the references of the phrase, uppercased (the field is case-sensitive: w0117 and t0122 found nothing, B2), first and counted as exact. The reference condition is removed from the OR query, where it restricted the results to the referenced nodes (« bois R0208 » found nothing). References are removed from the keywords; references alone run a single query (isReferenceSearch()). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM --- .../materio_sapi/src/Controller/Base.php | 76 ++++++++++++++++--- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/web/modules/custom/materio_sapi/src/Controller/Base.php b/web/modules/custom/materio_sapi/src/Controller/Base.php index c1b9d5a5..9a55f1b1 100644 --- a/web/modules/custom/materio_sapi/src/Controller/Base.php +++ b/web/modules/custom/materio_sapi/src/Controller/Base.php @@ -76,6 +76,25 @@ class Base extends ControllerBase { */ protected $phrase = ''; + /** + * Material references found in the phrase (W0117…), uppercase (H4). + * + * @var string[] + */ + protected $references = []; + + /** + * Pattern of a material reference: family letter and four digits. + */ + public const REFERENCE_PATTERN = '/\\b[WTRPCMFGSO]\\d{4}\\b/iu'; + + /** + * The reference query, run before the others. + * + * @var \Drupal\search_api\Query\QueryInterface|null + */ + protected $reference_query; + /** * Most meaningful words for which the phrase and AND queries run (H2, a). * @@ -94,14 +113,24 @@ class Base extends ControllerBase { $lang = \Drupal::languageManager()->getCurrentLanguage()->getId(); $this->index = Index::load('database'); - $words = KeywordText::words($this->phrase, $lang); + // References first, as exact matches, whatever their case (H4, B2). + preg_match_all(self::REFERENCE_PATTERN, $this->phrase, $matches); + $this->references = array_values(array_unique(array_map('mb_strtoupper', $matches[0]))); + $exact = $this->references ? $this->referenceQuery($this->references, $lang) : []; + if ($this->isReferenceSearch()) { + $this->exactematch_count = count($exact); + $this->results = ['uuids' => array_column($exact, 1), 'nids' => array_column($exact, 0)]; + return; + } + + // Keys without the references (« bois R0208 » searches « bois »). + $words = KeywordText::words(preg_replace(self::REFERENCE_PATTERN, ' ', $this->phrase) ?? $this->phrase, $lang); // Only stop words: search the phrase as typed, as before. $keys = $words ? implode(' ', $words) : implode(' ', $this->keys); - $exact = []; if (count($words) <= self::SHORT_QUERY_WORDS) { // Exact material names (like "wood-skin"), then all words. - $exact = $this->phraseQuery($keys, $lang); + $exact += $this->phraseQuery($keys, $lang); $exact += $this->andQuery($keys, $lang); } $this->exactematch_count = count($exact); @@ -113,6 +142,34 @@ class Base extends ControllerBase { ]; } + /** + * Nodes whose reference is one of the given ones. + * + * field_reference is case-sensitive: references are uppercased (B2). + * + * @param string[] $references + * Uppercase references. + * + * @return array + * [nid, uuid] keyed by nid, in result order. + */ + private function referenceQuery(array $references, string $lang): array { + $this->reference_query = $this->keywordQuery('terms', 'OR', '', $lang, 'materio_sapi_search_reference_query'); + $conditions = $this->reference_query->createConditionGroup('OR'); + foreach ($references as $reference) { + $conditions->addCondition('field_reference', $reference); + } + $this->reference_query->addConditionGroup($conditions); + return $this->collect($this->reference_query); + } + + /** + * Whether the phrase is only references: no need for the model (H4, H5). + */ + public function isReferenceSearch(): bool { + return $this->references && trim(preg_replace(self::REFERENCE_PATTERN, '', $this->phrase) ?? '', " \t,;") === ''; + } + /** * The whole keys in the title. * @@ -166,7 +223,10 @@ class Base extends ControllerBase { } /** - * Any word, in every fulltext field; references like W0117 matched exactly. + * Any word, in every fulltext field. + * + * References are no longer a condition here (H4): it restricted the OR + * results to the referenced nodes (« bois R0208 » found nothing). * * @return array * [nid, uuid] keyed by nid, in result order. @@ -177,14 +237,6 @@ class Base extends ControllerBase { // 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'); - foreach ($matches[0] as $value) { - $ref_conditions->addCondition('field_reference', $value); - } - $this->or_query->addConditionGroup($ref_conditions); - } return $this->collect($this->or_query); }