In the example below, a customer at a brewing merchant tells the counter AI agent that their beer tastes of wet cardboard. The catalogue holds the cure, which is an antioxidant that removes the oxygen from a filled bottle. A keyword search for that sentence matches nothing, because no product name or description contains the words beer, tastes, wet, or cardboard.
The reverse problem can appear in enterprise document search. The authors of a 2026 paper on a document search system for telecom network operations report that dense vector retrieval alone frequently performs poorly on queries that mix technical terminology and vendor-specific acronyms. Their system fuses a vector ranking, a keyword ranking, and a graph expansion through reciprocal rank fusion (arXiv 2609.01617). The keyword ranking matches exact terms, while the vector ranking matches paraphrases.
The mock model on this page repeats the top product that the tool returns, which means that each answer below follows from the ranking alone. With a model string in its place, the wording can change, while the ranking stays the same. Each code block below holds one whole file.
A question with none of the catalogue's words in it
createTransformersEmbedding loads Xenova/all-MiniLM-L6-v2 into this process and produces 384 numbers for each text, with no API key and no network call after the first download. embedding.fields maps the vector field to the text fields that the engine embeds into it. Each document holds a product name and a description, so the engine fills embedding from both.
In this step, the tool searches by keyword, so the engine fills the vector field while the query leaves it unused. Install the adapter and its peer alongside Narsil.
pnpm add -E @delali/narsil-embeddings-transformers @huggingface/transformersimport { createNarsil } from '@delali/narsil'
import { createTransformersEmbedding } from '@delali/narsil-embeddings-transformers'
import { ToolLoopAgent, tool } from 'ai'
import { z } from 'zod'
import { type CatalogueHit, counterModel, QUESTION } from './mock-model.ts'
const narsil = await createNarsil({
embedding: createTransformersEmbedding({ dimensions: 384 }),
})
await narsil.createIndex('brewing_catalogue', {
schema: { product: 'string', code: 'string', description: 'string', embedding: 'vector[384]' },
embedding: { fields: { embedding: ['product', 'description'] } },
})
await narsil.insertBatch('brewing_catalogue', [
{
id: 'sodium-metabisulphite',
product: 'Sodium metabisulphite',
code: 'BR-114',
description:
'An antioxidant powder that removes dissolved oxygen from the wort and from the headspace of a filled bottle, which protects the finished drink from staling on the shelf.',
},
{
id: 'irish-moss',
product: 'Irish moss finings',
code: 'BR-207',
description:
'A seaweed extract added in the last ten minutes of the boil, which settles the protein haze and leaves a brighter glass.',
},
{
id: 'three-piece-airlock',
product: 'Three-piece airlock',
code: 'BR-031',
description:
'A fermentation lock that lets carbon dioxide leave the vessel and keeps air out of the headspace above the brew.',
},
{
id: 'bench-crown-capper',
product: 'Bench crown capper',
code: 'BR-402',
description: 'A bench press that seats a 26 millimetre crown cap on a returnable bottle.',
},
{
id: 'campden-tablets',
product: 'Campden tablets',
code: 'BR-118',
description: 'Tablets that remove chlorine from the mash water, which prevents a plastic or medicinal off-flavour.',
},
])
const counterTools = {
searchCatalogue: tool({
description: 'Search the brewing catalogue by what its text says.',
inputSchema: z.object({ text: z.string() }),
execute: async ({ text }: { text: string }): Promise<CatalogueHit[]> => {
const found = await narsil.query<CatalogueHit>('brewing_catalogue', {
term: text,
document: { include: ['product', 'code', 'description'] },
limit: 3,
})
return found.hits.map((hit) => hit.document)
},
}),
}
const askCounterAgent = async (): Promise<string> => {
const agent = new ToolLoopAgent({
model: counterModel(),
instructions: 'Answer the customer from the brewing catalogue.',
tools: counterTools,
})
const reply = await agent.generate({ prompt: QUESTION })
return reply.text
}
console.log(JSON.stringify({ question: QUESTION, counterAnswered: await askCounterAgent() }, null, 2))
await narsil.shutdown(){
"question": "My beer tastes of wet cardboard. What should I order?",
"counterAnswered": "The catalogue holds nothing for that."
}A keyword search scores a document by the query terms that it contains, so with no term from the customer's sentence in any document, the search returns nothing. The merchant can lose a sale that a brewer behind the counter can make at once, because to a brewer, wet cardboard means oxidation.
A similarity search that returns the wrong tub
With mode: 'vector', the engine embeds the text in vector.text and compares that vector with each stored vector. Every query returns hits in this mode, because the engine applies no similarity floor until you set one.
import { createNarsil } from '@delali/narsil'
import { createTransformersEmbedding } from '@delali/narsil-embeddings-transformers'
import { ToolLoopAgent, tool } from 'ai'
import { z } from 'zod'
import { type CatalogueHit, counterModel, QUESTION } from './mock-model.ts'
const narsil = await createNarsil({
embedding: createTransformersEmbedding({ dimensions: 384 }),
})
await narsil.createIndex('brewing_catalogue', {
schema: { product: 'string', code: 'string', description: 'string', embedding: 'vector[384]' },
embedding: { fields: { embedding: ['product', 'description'] } },
})
await narsil.insertBatch('brewing_catalogue', [
{
id: 'sodium-metabisulphite',
product: 'Sodium metabisulphite',
code: 'BR-114',
description:
'An antioxidant powder that removes dissolved oxygen from the wort and from the headspace of a filled bottle, which protects the finished drink from staling on the shelf.',
},
{
id: 'irish-moss',
product: 'Irish moss finings',
code: 'BR-207',
description:
'A seaweed extract added in the last ten minutes of the boil, which settles the protein haze and leaves a brighter glass.',
},
{
id: 'three-piece-airlock',
product: 'Three-piece airlock',
code: 'BR-031',
description:
'A fermentation lock that lets carbon dioxide leave the vessel and keeps air out of the headspace above the brew.',
},
{
id: 'bench-crown-capper',
product: 'Bench crown capper',
code: 'BR-402',
description: 'A bench press that seats a 26 millimetre crown cap on a returnable bottle.',
},
{
id: 'campden-tablets',
product: 'Campden tablets',
code: 'BR-118',
description: 'Tablets that remove chlorine from the mash water, which prevents a plastic or medicinal off-flavour.',
},
])
const counterTools = {
searchCatalogue: tool({
description: 'Search the brewing catalogue by what its text says.',
inputSchema: z.object({ text: z.string() }),
execute: async ({ text }: { text: string }): Promise<CatalogueHit[]> => {
const found = await narsil.query<CatalogueHit>('brewing_catalogue', {
term: text,
mode: 'vector',
vector: { field: 'embedding', text },
document: { include: ['product', 'code', 'description'] },
limit: 3,
})
return found.hits.map((hit) => hit.document)
},
}),
}
const askCounterAgent = async (): Promise<string> => {
const agent = new ToolLoopAgent({
model: counterModel(),
instructions: 'Answer the customer from the brewing catalogue.',
tools: counterTools,
})
const reply = await agent.generate({ prompt: QUESTION })
return reply.text
}
console.log(JSON.stringify({ question: QUESTION, counterAnswered: await askCounterAgent() }, null, 2))
await narsil.shutdown(){
"question": "My beer tastes of wet cardboard. What should I order?",
"counterAnswered": "Order Irish moss finings, BR-207. A seaweed extract added in the last ten minutes of the boil, which settles the protein haze and leaves a brighter glass."
}In this panel, the agent recommends finings for a haze that the customer's beer lacks. The engine scores Irish moss at 0.2906 and the airlock at 0.2378, while the antioxidant comes fourth at 0.1670, so the tool leaves it out of the top three. A sentence-embedding model of this size can place a paraphrase near its source; however, it has no way to tell that a brewer means staling by 'wet cardboard', so the antioxidant's description scores below three unrelated products.
Solve this in the catalogue before you solve it in the query. A symptoms field holds the words that customers use for each fault, so the keyword search finds an exact match while the antioxidant's vector moves closer to the question.
Fuse the two rankings over a catalogue that holds both vocabularies
With mode: 'hybrid', the engine builds the keyword ranking from term and the similarity ranking from vector, then merges the two by reciprocal rank fusion. Reciprocal rank fusion scores each hit by its positions in the two rankings, so a fused score such as 0.0325 is far below a BM25 score such as 2.4399. You can compare a fused score only with the other scores in the same result set. Two fused scores can round to the same three decimals, as 0.0164 and 0.0159 do, so print at least four decimals whenever you log them.
Add symptoms to the schema, to the embedding fields, and to every document. The keyword search then matches wet cardboard in exactly one product, with a BM25 score of 2.4399, while the vector search returns all five products, ranked by similarity.
import { createNarsil } from '@delali/narsil'
import { createTransformersEmbedding } from '@delali/narsil-embeddings-transformers'
import { ToolLoopAgent, tool } from 'ai'
import { z } from 'zod'
import { type CatalogueHit, counterModel, QUESTION } from './mock-model.ts'
const narsil = await createNarsil({
embedding: createTransformersEmbedding({ dimensions: 384 }),
})
await narsil.createIndex('brewing_catalogue', {
schema: { product: 'string', code: 'string', description: 'string', embedding: 'vector[384]' },
embedding: { fields: { embedding: ['product', 'description'] } },
schema: { product: 'string', code: 'string', description: 'string', symptoms: 'string', embedding: 'vector[384]' },
embedding: { fields: { embedding: ['product', 'description', 'symptoms'] } },
})
await narsil.insertBatch('brewing_catalogue', [
{
id: 'sodium-metabisulphite',
product: 'Sodium metabisulphite',
code: 'BR-114',
description:
'An antioxidant powder that removes dissolved oxygen from the wort and from the headspace of a filled bottle, which protects the finished drink from staling on the shelf.',
symptoms: 'Wet cardboard, sherry, or papery flavours in a bottle that has stood too long.',
},
{
id: 'irish-moss',
product: 'Irish moss finings',
code: 'BR-207',
description:
'A seaweed extract added in the last ten minutes of the boil, which settles the protein haze and leaves a brighter glass.',
symptoms: 'A cloudy pour that stays hazy after a week in the cold.',
},
{
id: 'three-piece-airlock',
product: 'Three-piece airlock',
code: 'BR-031',
description:
'A fermentation lock that lets carbon dioxide leave the vessel and keeps air out of the headspace above the brew.',
symptoms: 'A vessel that draws air back in as it cools.',
},
{
id: 'bench-crown-capper',
product: 'Bench crown capper',
code: 'BR-402',
description: 'A bench press that seats a 26 millimetre crown cap on a returnable bottle.',
symptoms: 'Caps that leak or crimp unevenly by hand.',
},
{
id: 'campden-tablets',
product: 'Campden tablets',
code: 'BR-118',
description: 'Tablets that remove chlorine from the mash water, which prevents a plastic or medicinal off-flavour.',
symptoms: 'A plastic or TCP note that arrives with the first sip.',
},
])
const counterTools = {
searchCatalogue: tool({
description: 'Search the brewing catalogue by what its text says.',
inputSchema: z.object({ text: z.string() }),
execute: async ({ text }: { text: string }): Promise<CatalogueHit[]> => {
const found = await narsil.query<CatalogueHit>('brewing_catalogue', {
mode: 'vector',
mode: 'hybrid',
term: text,
vector: { field: 'embedding', text },
document: { include: ['product', 'code', 'description'] },
limit: 3,
})
return found.hits.map((hit) => hit.document)
},
}),
}
const askCounterAgent = async (): Promise<string> => {
const agent = new ToolLoopAgent({
model: counterModel(),
instructions: 'Answer the customer from the brewing catalogue.',
tools: counterTools,
})
const reply = await agent.generate({ prompt: QUESTION })
return reply.text
}
console.log(JSON.stringify({ question: QUESTION, counterAnswered: await askCounterAgent() }, null, 2))
await narsil.shutdown(){
"question": "My beer tastes of wet cardboard. What should I order?",
"counterAnswered": "Order Sodium metabisulphite, BR-114. An antioxidant powder that removes dissolved oxygen from the wort and from the headspace of a filled bottle, which protects the finished drink from staling on the shelf."
}With the fused ranking, the agent recommends the antioxidant. document.include contains three fields, so each hit contains only the fields that the agent passes to the model. The engine leaves the vector field out of a hit unless you ask for it, which keeps 384 numbers out of each hit and out of the prompt. When you ask for it, the engine returns a stored vector as a Float32Array, which JSON.stringify turns into an object keyed by index.
When one ranking matters more than the other, hybrid accepts a linear blend under a weight, alpha, which Hybrid search explains. When you call an embedding provider over a network, every query waits on that network, so a query can fail whenever the provider fails. Read Embedding adapters for how to choose an adapter and how an adapter embeds a whole batch in one call.
Where to go next
- Read Hybrid search for the fusion, the linear blend, and the two rankings behind a fused order.
- Read Vector search for the similarity floor, the graph, and the quantisation modes, which change how much memory a vector field uses.
- Read When the index has no good match for the status to return when neither search finds anything worth sending.