1- /**
2- * Reranker — second-stage precision scoring for search results.
3- *
4- * The pipeline:
5- * 1. Coarse search (vector / hybrid) → topK × rerankFactor candidates
6- * 2. Reranker scores each candidate against the query
7- * 3. Final sort by reranked score → topK results
8- */
9-
10- // ---------------------------------------------------------------------------
11- // Types
12- // ---------------------------------------------------------------------------
13-
14- /** A candidate document for reranking. */
15- export interface RerankCandidate {
16- id : string ;
17- content : string ;
18- /** Original score from the coarse search stage. */
19- score : number ;
20- /** Heading path as a string (e.g. "Line Chart > Tooltip"). */
21- headingPath ?: string ;
22- }
23-
24- /** A reranked result. */
25- export interface RerankResult {
26- id : string ;
27- /** Final score after reranking (higher is better). */
28- score : number ;
29- }
30-
31- /** Reranker type */
32- export type Reranker = {
33- rerank ( query : string , candidates : RerankCandidate [ ] ) : Promise < RerankResult [ ] > ;
34- } ;
35-
36- /** Configuration for reranking. */
37- export interface RerankOptions {
38- rerankFactor ?: number ;
39- minCandidates ?: number ;
40- phraseWeight ?: number ;
41- phraseRepeatBonus ?: number ;
42- termWeight ?: number ;
43- termRepeatBonus ?: number ;
44- substringWeight ?: number ;
45- headingTermBonus ?: number ;
46- headingPhraseBonus ?: number ;
47- originalScoreCarry ?: number ;
48- }
1+ import type { Reranker , RerankCandidate , RerankResult , RerankOptions } from './types' ;
492
503// ---------------------------------------------------------------------------
514// Defaults
@@ -72,7 +25,7 @@ const DEFAULT_WEIGHTS = {
7225 * All scoring weights are configurable via constructor options.
7326 * Scores are normalised to [0, 1] via min-max scaling.
7427 */
75- export class KeywordReranker {
28+ export class KeywordReranker implements Reranker {
7629 private readonly weights : Record < string , number > ;
7730
7831 constructor ( options ?: RerankOptions ) {
@@ -210,15 +163,4 @@ function isWordBoundary(
210163 const afterIdx = idx + term . length ;
211164 const after = afterIdx >= text . length || / [ \s \n . , ; : ! ? , 。 ! ? 、 : ; " ' ) ) 】 》 \- _ ] / . test ( text [ afterIdx ] ) ;
212165 return before && after ;
213- }
214-
215- // ---------------------------------------------------------------------------
216- // Factory
217- // ---------------------------------------------------------------------------
218-
219- /**
220- * Create a reranker with optional weight configuration.
221- */
222- export function createReranker ( options ?: RerankOptions ) : Reranker {
223- return new KeywordReranker ( options ) ;
224- }
166+ }
0 commit comments