import { SolVec } from '@veclabs/solvec';
const sv = new SolVec({ network: 'devnet' });
const collection = sv.collection('search-index', {
dimensions: 1536,
metric: 'cosine'
});
// Index your content
async function indexContent(items: Array<{ id: string; title: string; body: string; url: string }>) {
const texts = items.map(item => `${item.title}\n${item.body}`);
const embeddings = await batchEmbed(texts);
await collection.upsert(
items.map((item, i) => ({
id: item.id,
values: embeddings[i],
metadata: {
title: item.title,
body: item.body.slice(0, 500), // store excerpt
url: item.url,
}
}))
);
}
// Search
async function search(query: string, topK = 10) {
const queryEmbedding = await embed(query);
const results = await collection.query({
vector: queryEmbedding,
topK,
minScore: 0.65, // tune for your use case
});
return results.map(r => ({
id: r.id,
title: r.metadata.title,
excerpt: r.metadata.body,
url: r.metadata.url,
relevanceScore: r.score,
}));
}
// Usage
const results = await search('how to reset my password');
// Returns results about password reset, account recovery, forgot password -
// even if those exact words aren't in the query