diff --git a/web/modules/custom/materio_sapi/src/Controller/Base.php b/web/modules/custom/materio_sapi/src/Controller/Base.php index c696d21d..0523b8b6 100644 --- a/web/modules/custom/materio_sapi/src/Controller/Base.php +++ b/web/modules/custom/materio_sapi/src/Controller/Base.php @@ -107,14 +107,23 @@ class Base extends ControllerBase { public const KNN_SIGMA = 2.0; /** - * Whether the OR query is merged with the neighbours (H3, H5). + * Best text matches merged with the neighbours (Q8). * - * Off on trial since 17/11 (H6): on a phrase it matched about 40 % of the - * catalogue. Kept: it still runs when there are no neighbours (model - * stopped, slow or switched off), so that - * the search answers as before. + * With the neighbours, the keyword query over every field (description + * included) needs every word, not one of them (on a phrase, « one of them » + * matched about 40 % of the catalogue, H6), and only its best matches are + * fused: the model misses rare words found only in a description + * (« métamorphose », 01/12). Without neighbours (model stopped, slow or + * switched off) it runs as before: any word, every match. */ - public const OR_WITH_KNN = FALSE; + public const TEXT_WITH_KNN_LIMIT = 100; + + /** + * Weight of those text matches in the fusion, the neighbours weighing 1 + * (Q8): at equal weight they pushed BiMetal into the first five of + * « textile qui change de couleur ». + */ + public const TEXT_WITH_KNN_WEIGHT = 1.0; /** * Number of neighbours found, for the response tuning of H6. @@ -323,10 +332,22 @@ class Base extends ControllerBase { } $neighbours = $this->neighbours($lang); - $or = ($neighbours === NULL || self::OR_WITH_KNN) ? $this->orQuery($keys, $lang) : []; + if ($neighbours === NULL) { + $or = $this->orQuery($keys, $lang); + } + else { + $or = count($words) <= self::SHORT_QUERY_WORDS ? $this->orQuery($keys, $lang, 'AND', self::TEXT_WITH_KNN_LIMIT) : []; + // Text matches ordered by closeness to the phrase (every node has a + // score before ScoreCut): a record that holds the words but means + // something else goes down the list (Q8). + $text_order = array_keys($or); + usort($text_order, static fn ($a, $b): int => ($neighbours[$b] ?? -INF) <=> ($neighbours[$a] ?? -INF)); + $or = array_replace(array_flip($text_order), $or); + } $neighbours = $neighbours === NULL ? [] : ScoreCut::aboveMean($neighbours, self::KNN_SIGMA); $this->knn_count += count($neighbours); - $nids = RankFusion::fuse(array_keys($exact), array_filter([array_keys($or), array_keys($neighbours)])); + $weights = $neighbours ? [0 => self::TEXT_WITH_KNN_WEIGHT] : []; + $nids = RankFusion::fuse(array_keys($exact), array_filter([array_keys($or), array_keys($neighbours)]), RankFusion::K, $weights); $uuids = array_column($exact + $or, 1, 0) + $this->uuids(array_diff(array_keys($neighbours), array_keys($exact + $or))); return [$nids, $uuids, count($exact)]; @@ -630,12 +651,13 @@ class Base extends ControllerBase { * @return array * [nid, uuid] keyed by nid, in result order. */ - private function orQuery(string $keys, string $lang): array { + private function orQuery(string $keys, string $lang, string $conjunction = 'OR', int $limit = 10000): array { // 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'); + $this->or_query = $this->keywordQuery('terms', $conjunction, preg_replace('/(?<=\p{L})-(?=\p{L})/u', ' ', $keys), $lang, 'materio_sapi_search_or_query'); + $this->or_query->range(0, $limit); $this->addCountryCondition($this->or_query); return $this->collect($this->or_query); } diff --git a/web/modules/custom/materio_sapi/src/RankFusion.php b/web/modules/custom/materio_sapi/src/RankFusion.php index 3f311aea..0c94c6ca 100644 --- a/web/modules/custom/materio_sapi/src/RankFusion.php +++ b/web/modules/custom/materio_sapi/src/RankFusion.php @@ -30,11 +30,13 @@ final class RankFusion { * Ranked lists of node IDs (keyword OR query, kNN neighbours…). * @param int $k * RRF constant. + * @param array $weights + * Weight of each ranking, same keys; 1 when missing (Q8). * * @return array * Node IDs, each once. */ - public static function fuse(array $exact, array $rankings, int $k = self::K): array { + public static function fuse(array $exact, array $rankings, int $k = self::K, array $weights = []): array { $merged = []; foreach ($exact as $nid) { $merged[$nid] ??= $nid; @@ -42,7 +44,8 @@ final class RankFusion { $scores = []; $first = []; $position = 0; - foreach ($rankings as $ranking) { + foreach ($rankings as $key => $ranking) { + $weight = $weights[$key] ?? 1.0; $rank = 0; foreach ($ranking as $nid) { $rank++; @@ -53,7 +56,7 @@ final class RankFusion { $scores[$nid] = 0.0; $first[$nid] = [$position++, $nid]; } - $scores[$nid] += 1 / ($k + $rank); + $scores[$nid] += $weight / ($k + $rank); } } uksort($scores, static fn ($a, $b): int => [$scores[$b], $first[$a][0]] <=> [$scores[$a], $first[$b][0]]); diff --git a/web/modules/custom/materio_sapi/tests/src/Unit/RankFusionTest.php b/web/modules/custom/materio_sapi/tests/src/Unit/RankFusionTest.php index 0ffd5b7f..10322bf6 100644 --- a/web/modules/custom/materio_sapi/tests/src/Unit/RankFusionTest.php +++ b/web/modules/custom/materio_sapi/tests/src/Unit/RankFusionTest.php @@ -47,6 +47,16 @@ final class RankFusionTest extends UnitTestCase { $this->assertSame([7, 8, 3, 4], RankFusion::fuse([], [[7, 3], [8, 4]])); } + /** + * A lighter list counts less: its first node falls behind (Q8). + */ + public function testWeights(): void { + // Equal weights: 7 and 8 tie, 7 seen first. + $this->assertSame([7, 8], RankFusion::fuse([], [[7], [8]])); + // Text list at 0.5: 8 (1/61) beats 7 (0.5/61). + $this->assertSame([8, 7], RankFusion::fuse([], [[7], [8]], RankFusion::K, [0 => 0.5])); + } + /** * Empty lists: the exact matches alone, or nothing. */