Search: words found only in a description come back (Q8)
The model sums up a whole record in one vector: a rare word of the description weighs too little (« métamorphose »: 5 records missing or far). With the neighbours, the keyword query over every field runs again, all words required, best 100 matches, reordered by closeness to the phrase, then fused with the neighbours. Without the model it stays the former any-word query. RankFusion takes a weight per list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -107,14 +107,23 @@ class Base extends ControllerBase {
|
|||||||
public const KNN_SIGMA = 2.0;
|
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
|
* With the neighbours, the keyword query over every field (description
|
||||||
* catalogue. Kept: it still runs when there are no neighbours (model
|
* included) needs every word, not one of them (on a phrase, « one of them »
|
||||||
* stopped, slow or switched off), so that
|
* matched about 40 % of the catalogue, H6), and only its best matches are
|
||||||
* the search answers as before.
|
* 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.
|
* Number of neighbours found, for the response tuning of H6.
|
||||||
@@ -323,10 +332,22 @@ class Base extends ControllerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$neighbours = $this->neighbours($lang);
|
$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);
|
$neighbours = $neighbours === NULL ? [] : ScoreCut::aboveMean($neighbours, self::KNN_SIGMA);
|
||||||
$this->knn_count += count($neighbours);
|
$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)));
|
$uuids = array_column($exact + $or, 1, 0) + $this->uuids(array_diff(array_keys($neighbours), array_keys($exact + $or)));
|
||||||
return [$nids, $uuids, count($exact)];
|
return [$nids, $uuids, count($exact)];
|
||||||
@@ -630,12 +651,13 @@ class Base extends ControllerBase {
|
|||||||
* @return array<int, array{0: mixed, 1: string}>
|
* @return array<int, array{0: mixed, 1: string}>
|
||||||
* [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, string $conjunction = 'OR', int $limit = 10000): array {
|
||||||
// Terms mode, not direct: Search API escapes the words, so punctuation
|
// Terms mode, not direct: Search API escapes the words, so punctuation
|
||||||
// and Solr syntax typed in a phrase (: ( " / \\ ~) are plain text (H3).
|
// and Solr syntax typed in a phrase (: ( " / \\ ~) are plain text (H3).
|
||||||
// Hyphenated words as separate words, as the direct mode did: in terms mode
|
// 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).
|
// « 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);
|
$this->addCountryCondition($this->or_query);
|
||||||
return $this->collect($this->or_query);
|
return $this->collect($this->or_query);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,11 +30,13 @@ final class RankFusion {
|
|||||||
* Ranked lists of node IDs (keyword OR query, kNN neighbours…).
|
* Ranked lists of node IDs (keyword OR query, kNN neighbours…).
|
||||||
* @param int $k
|
* @param int $k
|
||||||
* RRF constant.
|
* RRF constant.
|
||||||
|
* @param array<float> $weights
|
||||||
|
* Weight of each ranking, same keys; 1 when missing (Q8).
|
||||||
*
|
*
|
||||||
* @return array<int|string>
|
* @return array<int|string>
|
||||||
* Node IDs, each once.
|
* 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 = [];
|
$merged = [];
|
||||||
foreach ($exact as $nid) {
|
foreach ($exact as $nid) {
|
||||||
$merged[$nid] ??= $nid;
|
$merged[$nid] ??= $nid;
|
||||||
@@ -42,7 +44,8 @@ final class RankFusion {
|
|||||||
$scores = [];
|
$scores = [];
|
||||||
$first = [];
|
$first = [];
|
||||||
$position = 0;
|
$position = 0;
|
||||||
foreach ($rankings as $ranking) {
|
foreach ($rankings as $key => $ranking) {
|
||||||
|
$weight = $weights[$key] ?? 1.0;
|
||||||
$rank = 0;
|
$rank = 0;
|
||||||
foreach ($ranking as $nid) {
|
foreach ($ranking as $nid) {
|
||||||
$rank++;
|
$rank++;
|
||||||
@@ -53,7 +56,7 @@ final class RankFusion {
|
|||||||
$scores[$nid] = 0.0;
|
$scores[$nid] = 0.0;
|
||||||
$first[$nid] = [$position++, $nid];
|
$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]]);
|
uksort($scores, static fn ($a, $b): int => [$scores[$b], $first[$a][0]] <=> [$scores[$a], $first[$b][0]]);
|
||||||
|
|||||||
@@ -47,6 +47,16 @@ final class RankFusionTest extends UnitTestCase {
|
|||||||
$this->assertSame([7, 8, 3, 4], RankFusion::fuse([], [[7, 3], [8, 4]]));
|
$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.
|
* Empty lists: the exact matches alone, or nothing.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user