Your first spotlight
Chunk a document. Rank snippets against a probe glyph.
Spotlight compiles one document into fingerprinted chunks, then scores each chunk against a probe. Use Rank to inspect every chunk, or Query to filter by threshold.
What you will build
Section titled “What you will build”- Create a spotlight document from text.
- Rank chunks against a probe glyph.
- Query with a threshold and limit.
- Optionally return plain strings with
textOutput.
Create a document
Section titled “Create a document”import { spotlight } from "@koda.oss/glyph";
const doc = spotlight.New( "Goodbye moon under quiet stars. Pasta recipe with tomato. Goodbye moon again.",);
console.log(doc.Size()); // number of chunksRank all chunks
Section titled “Rank all chunks”import { Create, spotlight } from "@koda.oss/glyph";
const doc = spotlight.New("...");const probe = Create("Goodbye moon under quiet stars").glyph;
const results = doc.Rank(probe);
for (const hit of results) { console.log(hit.score.toFixed(2), hit.text);}Each result is a GlyphSpotlightResult with text, glyph, score, and comparison.
Query with threshold
Section titled “Query with threshold”const hits = doc.Query(probe, { threshold: 0.15, limit: 5,});Group probes
Section titled “Group probes”Pass a GlyphGroup to match multiple examples (emails, URLs, phrases):
import { CreateGroup } from "@koda.oss/glyph";
const hits = doc.Query( CreateGroup(["user@example.com", "support@example.com"]), { threshold: 0.2, limit: 10 },);Group probes use GroupResultAggregatorSum by default.
textOutput
Section titled “textOutput”Return chunk text only, sorted by score:
const snippets = doc.Query(probe, { threshold: 0.1, limit: 3, textOutput: true,});// string[]Try it in the terminal
Section titled “Try it in the terminal”npm run demo -- spotlight ./docs/core/index.md "LSH banding"