fix(core): better search result (#7819)

This commit is contained in:
EYHN
2024-08-09 10:37:26 +00:00
parent b993ab04df
commit a579cc7716

View File

@@ -178,11 +178,13 @@ export class FullTextInvertedIndex implements InvertedIndex {
const queryTokens = new GeneralTokenizer().tokenize(term); const queryTokens = new GeneralTokenizer().tokenize(term);
const matched = new Map< const matched = new Map<
number, number,
Map<
number, // index
{ {
score: number; score: number;
index: number;
ranges: [number, number][]; ranges: [number, number][];
} }
>
>(); >();
for (const token of queryTokens) { for (const token of queryTokens) {
const key = InvertedIndexKey.forString(this.fieldKey, token.term); const key = InvertedIndexKey.forString(this.fieldKey, token.term);
@@ -251,20 +253,42 @@ export class FullTextInvertedIndex implements InvertedIndex {
maxScore === minScore maxScore === minScore
? score ? score
: (score - minScore) / (maxScore - minScore); : (score - minScore) / (maxScore - minScore);
const match = matched.get(nid); const match =
if (!match || normalizedScore > match.score) { matched.get(nid) ??
matched.set(nid, { new Map<
score: normalizedScore, number, // index
index: position.index, {
ranges: position.ranges, score: number;
}); ranges: [number, number][];
} }
>();
const item = match.get(position.index) || {
score: 0,
ranges: [],
};
item.score += normalizedScore;
item.ranges.push(...position.ranges);
match.set(position.index, item);
matched.set(nid, match);
} }
} }
const match = new Match(); const match = new Match();
for (const [nid, { score, index, ranges }] of matched) { for (const [nid, items] of matched) {
match.addScore(nid, score); if (items.size === 0) {
match.addHighlighter(nid, this.fieldKey, index, ranges); break;
}
let highestScore = -1;
let highestIndex = -1;
let highestRanges: [number, number][] = [];
for (const [index, { score, ranges }] of items) {
if (score > highestScore) {
highestScore = score;
highestIndex = index;
highestRanges = ranges;
}
}
match.addScore(nid, highestScore);
match.addHighlighter(nid, this.fieldKey, highestIndex, highestRanges);
} }
return match; return match;
} }