Class KeywordFallback

Simple keyword-matching search over a corpus of chunks.

Used as a degraded-mode fallback when the embedding API is unavailable. Splits the query into keywords, filters out stop words and short tokens, then scores each chunk by the number of keyword hits (heading matches receive a higher weight than content matches).

Example

const fallback = new KeywordFallback(corpusChunks);
const results = fallback.search('authentication tokens', 5);
// results: RetrievedChunk[] sorted by relevance, at most 5 entries

Constructors

Methods

Constructors

Methods

  • Searches the corpus for chunks matching the given query keywords.

    Scoring algorithm:

    • Each keyword found in the chunk heading awards HEADING_MATCH_SCORE points.
    • Each keyword found in the chunk content awards CONTENT_MATCH_SCORE point.
    • Chunks with zero total score are excluded.
    • Scores are normalized to the 0-1 range (relative to the maximum observed score).
    • Results are sorted by score descending and sliced to topK.

    Parameters

    • query: string

      The user query string to match.

    • topK: number = 5

      Maximum number of results to return. Defaults to 5.

    Returns RetrievedChunk[]

    Array of RetrievedChunk sorted by relevance, at most topK entries.