import { Search, K, Knn, type Collection } from 'chromadb';
import * as readline from 'readline';
// Paginated search with field selection
async function searchWithPagination(
collection: Collection,
queryText: string,
pageSize = 20
) {
let currentPage = 0;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
while (true) {
const search = new Search()
.where(K("status").eq("published"))
.rank(Knn({ query: queryText }))
.limit(pageSize, currentPage * pageSize)
.select(K.DOCUMENT, K.SCORE, "title", "author", "date");
const results = await collection.search(search);
const rows = results.rows()[0]; // Get first (and only) search results
if (!rows || rows.length === 0) { // No more results
break;
}
console.log(`\n--- Page ${currentPage + 1} ---`);
for (const [i, row] of rows.entries()) {
console.log(`${i+1}. ${row.metadata?.title} by ${row.metadata?.author}`);
console.log(` Score: ${row.score?.toFixed(3)}, Date: ${row.metadata?.date}`);
console.log(` Preview: ${row.document?.substring(0, 100)}...`);
}
// Check if we want to continue
const userInput = await new Promise<string>(resolve => {
rl.question("\nPress Enter for next page, or 'q' to quit: ", resolve);
});
if (userInput.toLowerCase() === 'q') {
break;
}
currentPage += 1;
}
rl.close();
}