import { Client } from "topk-js";
import { text, semanticIndex } from "topk-js/schema";
import { select, field, fn } from "topk-js/query";
const client = new Client({
apiKey: process.env.TOPK_API_KEY,
region: "aws-us-east-1-elastica",
});
// Create a collection
await client.collections().create("books", {
title: text().index(semanticIndex()),
});
// Upsert documents
await client.collection("books").upsert([
{ _id: "gatsby", title: "The Great Gatsby" },
{ _id: "1984", title: "1984" },
]);
// Query
const results = await client.collection("books").query(
select({
title: field("title"),
score: fn.semanticSimilarity("title", "classic American novel"),
}).topk(field("score"), 10)
);